{"version":3,"file":"index.umd.js","sources":["../index.js"],"sourcesContent":["let browserSupportsTextareaTextNodes;\n\n/**\n * @param {HTMLElement} input\n * @return {boolean}\n */\nfunction canManipulateViaTextNodes(input) {\n if (input.nodeName !== \"TEXTAREA\") {\n return false;\n }\n if (typeof browserSupportsTextareaTextNodes === \"undefined\") {\n const textarea = document.createElement(\"textarea\");\n textarea.value = 1;\n browserSupportsTextareaTextNodes = !!textarea.firstChild;\n }\n return browserSupportsTextareaTextNodes;\n}\n\n/**\n * @param {HTMLTextAreaElement|HTMLInputElement} input\n * @param {string} text\n * @returns {void}\n */\nexport default function(input, text) {\n // Most of the used APIs only work with the field selected\n input.focus();\n\n // IE 8-10\n if (document.selection) {\n const ieRange = document.selection.createRange();\n ieRange.text = text;\n\n // Move cursor after the inserted text\n ieRange.collapse(false /* to the end */);\n ieRange.select();\n\n return;\n }\n\n // Webkit + Edge\n const isSuccess = document.execCommand(\"insertText\", false, text);\n if (!isSuccess) {\n const start = input.selectionStart;\n const end = input.selectionEnd;\n // Firefox (non-standard method)\n if (typeof input.setRangeText === \"function\") {\n input.setRangeText(text);\n } else {\n // To make a change we just need a Range, not a Selection\n const range = document.createRange();\n const textNode = document.createTextNode(text);\n\n if (canManipulateViaTextNodes(input)) {\n let node = input.firstChild;\n\n // If textarea is empty, just insert the text\n if (!node) {\n input.appendChild(textNode);\n } else {\n // Otherwise we need to find a nodes for start and end\n let offset = 0;\n let startNode = null;\n let endNode = null;\n\n while (node && (startNode === null || endNode === null)) {\n const nodeLength = node.nodeValue.length;\n\n // if start of the selection falls into current node\n if (start >= offset && start <= offset + nodeLength) {\n range.setStart((startNode = node), start - offset);\n }\n\n // if end of the selection falls into current node\n if (end >= offset && end <= offset + nodeLength) {\n range.setEnd((endNode = node), end - offset);\n }\n\n offset += nodeLength;\n node = node.nextSibling;\n }\n\n // If there is some text selected, remove it as we should replace it\n if (start !== end) {\n range.deleteContents();\n }\n }\n }\n\n // If the node is a textarea and the range doesn't span outside the element\n //\n // Get the commonAncestorContainer of the selected range and test its type\n // If the node is of type `#text` it means that we're still working with text nodes within our textarea element\n // otherwise, if it's of type `#document` for example it means our selection spans outside the textarea.\n if (\n canManipulateViaTextNodes(input) &&\n range.commonAncestorContainer.nodeName === \"#text\"\n ) {\n // Finally insert a new node. The browser will automatically split start and end nodes into two if necessary\n range.insertNode(textNode);\n } else {\n // If the node is not a textarea or the range spans outside a textarea the only way is to replace the whole value\n const value = input.value;\n input.value = value.slice(0, start) + text + value.slice(end);\n }\n }\n\n // Correct the cursor position to be at the end of the insertion\n input.setSelectionRange(start + text.length, start + text.length);\n\n // Notify any possible listeners of the change\n const e = document.createEvent(\"UIEvent\");\n e.initEvent(\"input\", true, false);\n input.dispatchEvent(e);\n }\n}\n"],"names":["browserSupportsTextareaTextNodes","canManipulateViaTextNodes","input","nodeName","textarea","document","createElement","value","firstChild","text","focus","selection","ieRange","createRange","collapse","select","isSuccess","execCommand","start","selectionStart","end","selectionEnd","setRangeText","range","textNode","createTextNode","node","appendChild","offset","startNode","endNode","nodeLength","nodeValue","length","setStart","setEnd","nextSibling","deleteContents","commonAncestorContainer","insertNode","slice","setSelectionRange","e","createEvent","initEvent","dispatchEvent"],"mappings":";;;;;;EAAA,IAAIA,gCAAJ;EAEA;;;;;EAIA,SAASC,yBAAT,CAAmCC,KAAnC,EAA0C;EACxC,MAAIA,KAAK,CAACC,QAAN,KAAmB,UAAvB,EAAmC;EACjC,WAAO,KAAP;EACD;;EACD,MAAI,OAAOH,gCAAP,KAA4C,WAAhD,EAA6D;EAC3D,QAAMI,QAAQ,GAAGC,QAAQ,CAACC,aAAT,CAAuB,UAAvB,CAAjB;EACAF,IAAAA,QAAQ,CAACG,KAAT,GAAiB,CAAjB;EACAP,IAAAA,gCAAgC,GAAG,CAAC,CAACI,QAAQ,CAACI,UAA9C;EACD;;EACD,SAAOR,gCAAP;EACD;EAED;;;;;;;AAKA,EAAe,gBAASE,KAAT,EAAgBO,IAAhB,EAAsB;EACnC;EAC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