UNPKG

input-format

Version:

Formatting user's text input on-the-fly

1 lines 4.19 kB
{"version":3,"file":"dom.js","names":["isReadOnly","element","hasAttribute","getSelection","selectionStart","selectionEnd","start","end","Keys","Backspace","Delete","getOperation","event","keyCode","getCaretPosition","setCaretPosition","caret_position","undefined","isAndroid","setTimeout","setSelectionRange","navigator","ANDROID_USER_AGENT_REG_EXP","test","userAgent"],"sources":["../source/dom.js"],"sourcesContent":["export function isReadOnly(element)\r\n{\r\n\treturn element.hasAttribute('readonly')\r\n}\r\n\r\n// Gets <input/> selection bounds\r\nexport function getSelection(element)\r\n{\r\n\t// If no selection, return nothing\r\n\tif (element.selectionStart === element.selectionEnd)\r\n\t{\r\n\t\treturn\r\n\t}\r\n\r\n\treturn { start: element.selectionStart, end: element.selectionEnd }\r\n}\r\n\r\n// Key codes\r\nexport const Keys =\r\n{\r\n\tBackspace : 8,\r\n\tDelete : 46\r\n}\r\n\r\n// Finds out the operation to be intercepted and performed\r\n// based on the key down event `keyCode`.\r\nexport function getOperation(event)\r\n{\r\n\tswitch (event.keyCode)\r\n\t{\r\n\t\tcase Keys.Backspace:\r\n\t\t\treturn 'Backspace'\r\n\r\n\t\tcase Keys.Delete:\r\n\t\t\treturn 'Delete'\r\n\t}\r\n}\r\n\r\n// Gets <input/> caret position\r\nexport function getCaretPosition(element)\r\n{\r\n\treturn element.selectionStart\r\n}\r\n\r\n// Sets <input/> caret position\r\nexport function setCaretPosition(element, caret_position)\r\n{\r\n\t// Sanity check\r\n\tif (caret_position === undefined)\r\n\t{\r\n\t\treturn\r\n\t}\r\n\r\n\t// Set caret position.\r\n\t// There has been an issue with caret positioning on Android devices.\r\n\t// https://github.com/catamphetamine/input-format/issues/2\r\n\t// I was revisiting this issue and looked for similar issues in other libraries.\r\n\t// For example, there's [`text-mask`](https://github.com/text-mask/text-mask) library.\r\n\t// They've had exactly the same issue when the caret seemingly refused to be repositioned programmatically.\r\n\t// The symptoms were the same: whenever the caret passed through a non-digit character of a mask (a whitespace, a bracket, a dash, etc), it looked as if it placed itself one character before its correct position.\r\n\t// https://github.com/text-mask/text-mask/issues/300\r\n\t// They seem to have found a basic fix for it: calling `input.setSelectionRange()` in a timeout rather than instantly for Android devices.\r\n\t// https://github.com/text-mask/text-mask/pull/400/files\r\n\t// I've implemented the same workaround here.\r\n\tif (isAndroid()) {\r\n setTimeout(() => element.setSelectionRange(caret_position, caret_position), 0)\r\n\t} else {\r\n\t\telement.setSelectionRange(caret_position, caret_position)\r\n\t}\r\n}\r\n\r\nfunction isAndroid() {\r\n\t// `navigator` is not defined when running mocha tests.\r\n\tif (typeof navigator !== 'undefined') {\r\n\t\treturn ANDROID_USER_AGENT_REG_EXP.test(navigator.userAgent)\r\n\t}\r\n}\r\n\r\nconst ANDROID_USER_AGENT_REG_EXP = /Android/i"],"mappings":"AAAA,OAAO,SAASA,UAAT,CAAoBC,OAApB,EACP;EACC,OAAOA,OAAO,CAACC,YAAR,CAAqB,UAArB,CAAP;AACA,C,CAED;;AACA,OAAO,SAASC,YAAT,CAAsBF,OAAtB,EACP;EACC;EACA,IAAIA,OAAO,CAACG,cAAR,KAA2BH,OAAO,CAACI,YAAvC,EACA;IACC;EACA;;EAED,OAAO;IAAEC,KAAK,EAAEL,OAAO,CAACG,cAAjB;IAAiCG,GAAG,EAAEN,OAAO,CAACI;EAA9C,CAAP;AACA,C,CAED;;AACA,OAAO,IAAMG,IAAI,GACjB;EACCC,SAAS,EAAG,CADb;EAECC,MAAM,EAAM;AAFb,CADO,C,CAMP;AACA;;AACA,OAAO,SAASC,YAAT,CAAsBC,KAAtB,EACP;EACC,QAAQA,KAAK,CAACC,OAAd;IAEC,KAAKL,IAAI,CAACC,SAAV;MACC,OAAO,WAAP;;IAED,KAAKD,IAAI,CAACE,MAAV;MACC,OAAO,QAAP;EANF;AAQA,C,CAED;;AACA,OAAO,SAASI,gBAAT,CAA0Bb,OAA1B,EACP;EACC,OAAOA,OAAO,CAACG,cAAf;AACA,C,CAED;;AACA,OAAO,SAASW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