tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

editing-on-newline-should-not-crash.html (1606B)


      1 <!DOCTYPE html>
      2 <html lang="en">
      3  <head>
      4    <meta charset="UTF-8" />
      5    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
      6    <title>Pasting in selection with newline character should not crash</title>
      7    <style>
      8      #text {
      9        white-space: pre-line;
     10        border: 1px solid #ccc;
     11        padding: 10px;
     12        width: 300px;
     13        height: 100px;
     14        overflow: auto;
     15      }
     16    </style>
     17  </head>
     18  <body>
     19    <div id="text" contenteditable="true">This is a sample text.<br>
     20    </div>
     21    <script>
     22      const div = document.getElementById("text");
     23      function setSelectionToCopy() {
     24        const range = document.createRange();
     25        const selection = window.getSelection();
     26        const textNode = div.firstChild;
     27        // Set the Selection on first word "This".
     28        range.setStart(textNode, 0);
     29        range.setEnd(textNode, 4);
     30        selection.removeAllRanges();
     31        selection.addRange(range);
     32      }
     33      function setSelectionOnNewLine() {
     34        const range = document.createRange();
     35        const selection = window.getSelection();
     36        const textNode = div.lastChild;
     37        const newlineIndex = textNode.textContent.indexOf("\n");
     38 
     39        if (newlineIndex !== -1) {
     40          range.setStart(textNode, newlineIndex);
     41          range.setEnd(textNode, newlineIndex + 1);
     42          selection.removeAllRanges();
     43          selection.addRange(range);
     44        }
     45      }
     46      setSelectionToCopy();
     47      document.execCommand("copy");
     48      setSelectionOnNewLine();
     49      document.execCommand("paste");
     50    </script>
     51  </body>
     52 </html>