tor-browser

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

innertext-setter.html (2881B)


      1 <!DOCTYPE html>
      2 <title>innerText setter test</title>
      3 <script src="/resources/testharness.js"></script>
      4 <script src="/resources/testharnessreport.js"></script>
      5 <div id="container"></div>
      6 <script>
      7 // As of March 2017, WebKit and Blink have inconsistent results depending on
      8 // rendered or not.  setupTest() tests a rendered case, and setupTestDetached()
      9 // tests a not-rendered case.
     10 
     11 function setupTest(context, plain) {
     12  var container = document.getElementById("container");
     13  // context is either a string or an element node
     14  if (typeof context === "string") {
     15    container.innerHTML = context;
     16  } else {
     17    container.innerHTML = "";
     18    container.appendChild(context);
     19  }
     20  var e = container.firstChild;
     21  while (e && e.nodeType != Node.ELEMENT_NODE) {
     22    e = e.nextSibling;
     23  }
     24  e.offsetWidth;
     25  var oldChild = e.firstChild;
     26  e.innerText = plain;
     27  return [e, oldChild];
     28 }
     29 
     30 function setupTestDetached(context, plain) {
     31  var detachedContainer = document.createElement("div");
     32  // context is either a string or an element node
     33  if (typeof context === "string") {
     34    detachedContainer.innerHTML = context;
     35  } else {
     36    detachedContainer.innerHTML = "";
     37    detachedContainer.appendChild(context);
     38  }
     39  var e = detachedContainer.firstChild;
     40  while (e && e.nodeType != Node.ELEMENT_NODE) {
     41    e = e.nextSibling;
     42  }
     43  var oldChild = e.firstChild;
     44  e.innerText = plain;
     45  return [e, oldChild];
     46 }
     47 
     48 function assertNewSingleTextNode(newChild, expectedText, oldChild) {
     49  assert_not_equals(newChild, null, "Should have a child");
     50  assert_equals(newChild.nodeType, Node.TEXT_NODE, "Child should be a text node");
     51  assert_equals(newChild.nextSibling, null, "Should have only one child");
     52  assert_equals(newChild.data, expectedText);
     53  assert_not_equals(newChild, oldChild, "Child should be a *new* text node");
     54 }
     55 
     56 function assertNoEmptyTextChild(parent) {
     57  for (var child = parent.firstChild; child; child = child.nextSibling) {
     58    if (child.nodeType === Node.TEXT_NODE) {
     59      assert_not_equals(child.data, "", "Should not have empty text nodes");
     60    }
     61  }
     62 }
     63 
     64 function testText(context, plain, expectedText, msg) {
     65  test(function(){
     66    var arr = setupTest(context, plain);
     67    assertNewSingleTextNode(arr[0].firstChild, expectedText, arr[1]);
     68  }, msg);
     69  test(function() {
     70    var arr = setupTestDetached(context, plain);
     71    assertNewSingleTextNode(arr[0].firstChild, expectedText, arr[1]);
     72  }, msg + ", detached");
     73 }
     74 
     75 function testHTML(context, plain, expectedHTML, msg) {
     76  test(function(){
     77    var e = setupTest(context, plain)[0];
     78    assert_equals(e.innerHTML, expectedHTML);
     79    assertNoEmptyTextChild(e);
     80  }, msg);
     81  test(function() {
     82    var e = setupTestDetached(context, plain)[0];
     83    assert_equals(e.innerHTML, expectedHTML);
     84    assertNoEmptyTextChild(e);
     85  }, msg + ", detached");
     86 }
     87 </script>
     88 <script src="innertext-setter-tests.js"></script>