tor-browser

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

insertHTML.html (5173B)


      1 <!doctype html>
      2 <html>
      3 <head>
      4 <meta charset="utf-8">
      5 <meta name="timeout" content="long">
      6 <meta name="variant" content="?white-space=normal">
      7 <meta name="variant" content="?white-space=pre">
      8 <meta name="variant" content="?white-space=pre-line">
      9 <meta name="variant" content="?white-space=pre-wrap">
     10 <title>Pasting rich text into contenteditable=plaintext-only</title>
     11 <script src="/resources/testharness.js"></script>
     12 <script src="/resources/testharnessreport.js"></script>
     13 <script src="../include/editor-test-utils.js"></script>
     14 <script>
     15 "use strict";
     16 
     17 const searchParams = new URLSearchParams(document.location.search);
     18 const whiteSpace = searchParams.get("white-space");
     19 const useBR = whiteSpace == "normal";
     20 const collapseWhiteSpaces = whiteSpace == "normal" || whiteSpace == "pre-line";
     21 
     22 addEventListener("load", () => {
     23  const editingHost = document.createElement("div");
     24  editingHost.style.whiteSpace = whiteSpace;
     25  editingHost.setAttribute("contenteditable", "plaintext-only");
     26  document.body.appendChild(editingHost);
     27  editingHost.focus();
     28  editingHost.getBoundingClientRect();
     29  const utils = new EditorTestUtils(editingHost);
     30 
     31  for (const data of [
     32    {
     33      insertHTML: "plaintext",
     34      expected: "plaintext",
     35    },
     36    {
     37      // line breaks should not be preformatted
     38      insertHTML: "1st line\n2nd line",
     39      expected: "1st line 2nd line",
     40    },
     41    {
     42      // preformatted line breaks should appear as-is
     43      insertHTML: "<pre>1st line\n2nd line</pre>",
     44      expected: useBR
     45        ? "1st line<br>2nd line"
     46        : "1st line\n2nd line",
     47    },
     48    {
     49      // text should be inserted into the <b>
     50      initialInnerHTML: "<b>{}</b>",
     51      insertHTML: "plaintext",
     52      expected: "<b>plaintext</b>",
     53    },
     54    {
     55      // text should be inserted into the <b>
     56      initialInnerHTML: "<b>{}<br></b>",
     57      insertHTML: "plaintext",
     58      expected: ["<b>plaintext</b>", "<b>plaintext<br></b>"],
     59    },
     60    {
     61      // text should be inserted into the <b>
     62      initialInnerHTML: "<b>A[]B</b>",
     63      insertHTML: "plaintext",
     64      expected: "<b>AplaintextB</b>",
     65    },
     66    {
     67      // text should be inserted into the <span> even if it's meaningless
     68      initialInnerHTML: "<span>A[]B</span>",
     69      insertHTML: "plaintext",
     70      expected: "<span>AplaintextB</span>",
     71    },
     72    {
     73      // inserting one paragraph should cause inserting only its contents.
     74      // (but it's okay other serialized text.)
     75      insertHTML: "<div>abc</div>",
     76      expected: "abc",
     77    },
     78    {
     79      // inserting one paragraph should cause inserting only its contents.
     80      // (but it's okay other serialized text.)
     81      insertHTML: "<div>abc<br>def</div>",
     82      expected: useBR ? "abc<br>def" : "abc\ndef",
     83    },
     84    {
     85      // inserting 2 or more paragraphs should be handled as multiple lines
     86      insertHTML: "<div>abc</div><div>def</div>",
     87      expected: useBR ? "abc<br>def" : "abc\ndef",
     88    },
     89    {
     90      // inserting 2 or more paragraphs should be handled as multiple lines
     91      insertHTML: "<div>abc<br>def</div><div>ghi<br>jkl</div>",
     92      expected: useBR
     93        ? "abc<br>def<br>ghi<br>jkl"
     94        : "abc\ndef\nghi\njkl",
     95    },
     96    {
     97      // <noscript> content should not be inserted
     98      insertHTML: "<noscript>no script</noscript>",
     99      expected: "",
    100    },
    101    {
    102      // <noframes> content should not be inserted
    103      insertHTML: "<noframes>no frames</noframes>",
    104      expected: "",
    105    },
    106    {
    107      // <script> content should not be inserted
    108      insertHTML: `<script>script</${"script"}>`,
    109      expected: "",
    110    },
    111    {
    112      // <style> content should not be inserted
    113      insertHTML: "<style>style</style>",
    114      expected: "",
    115    },
    116    {
    117      // <head> content should not be inserted
    118      insertHTML: "<html><head><title>title</title></head><body>body</body></html>",
    119      expected: "body",
    120    },
    121    {
    122      // white-spaces should be collapsed
    123      insertHTML: "plain  text",
    124      expected: "plain text",
    125    },
    126    {
    127      // white-spaces should be collapsed
    128      insertHTML: "<span>plain  text</span>",
    129      expected: "plain text",
    130    },
    131    {
    132      // preformatted white-spaces should not be collapsed
    133      insertHTML: "<pre>plain  text</pre>",
    134      expected: !collapseWhiteSpaces
    135        ? "plain  text"
    136        : ["plain &nbsp;text", "plain&nbsp; text", "plain&nbsp;&nbsp;text",
    137           "plain \u00A0text", "plain\u00A0 text", "plain\u00A0\u00A0text"],
    138    },
    139    {
    140      // even if inserting HTML is empty, selected text should be deleted
    141      initialInnerHTML: "A[B]C",
    142      insertHTML: "",
    143      expected: "AC",
    144    },
    145  ]) {
    146    test(() => {
    147      utils.setupEditingHost(data.initialInnerHTML ? data.initialInnerHTML : "");
    148      document.execCommand("insertHTML", false, data.insertHTML);
    149      if (Array.isArray(data.expected)) {
    150        assert_in_array(editingHost.innerHTML, data.expected);
    151      } else {
    152        assert_equals(editingHost.innerHTML, data.expected);
    153      }
    154    }, `execCommand("insertHTML", false, "${data.insertHTML}") when "${
    155      data.initialInnerHTML ? data.initialInnerHTML : ""
    156    }"`);
    157  }
    158 }, {once: true});
    159 </script>
    160 </head>
    161 <body></body>
    162 </html>