summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/mathml/relations/html5-tree/dynamic-childlist-002.html
blob: 099401eaccc0524d514c6203f360d6c682d2b089 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Dynamic childlist of MathML elements</title>
<script src="/mathml/support/mathml-fragments.js"></script>
<link rel="help" href="https://w3c.github.io/mathml-core/#adjust-space-around-content-mpadded">
<link rel="help" href="https://w3c.github.io/mathml-core/#dom-and-javascript">
<meta name="assert" content="Dynamically modify DOM tree of some MathML elements by adding or removing children.">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/mathml/support/feature-detection.js"></script>
<script src="/mathml/support/layout-comparison.js"></script>
<script>
  function generateMathForTag(tag, childCount) {
      let math = FragmentHelper.createElement("math");
      let element = FragmentHelper.createElement(tag);
      // Add the children with different sizes at odd positions and OOF
      // mrow at even position.
      for (let i = 0; i < childCount; i++) {
          if (i % 2) {
              let mspace = FragmentHelper.createElement("mspace");
              mspace.setAttribute("width", `10px`);
              mspace.setAttribute("height", `${10*(i+1)}px`);
              mspace.setAttribute("style", `background: black;`);
              element.appendChild(mspace);
          } else {
              let mrow = FragmentHelper.createElement("mrow");
              mrow.setAttribute("style", "position: absolute");
              element.appendChild(mrow);
          }
      }
      if (FragmentHelper.isValidChildOfMrow(tag)) {
          math.appendChild(element);
      } else if (tag === "mtd") {
          let mtr = FragmentHelper.createElement("mtr");
          mtr.appendChild(element);
          let mtable = FragmentHelper.createElement("mtable");
          mtable.appendChild(mtr);
          math.appendChild(mtable);
      } else {
          throw `Invalid argument: ${tag}`;
      }
      return math;
  }

  setup({ explicit_done: true });
  window.addEventListener("load", function() {

      for (tag in MathMLFragments) {
          if (!FragmentHelper.isValidChildOfMrow(tag) || tag === "mtd")
              continue;

          document.body.insertAdjacentHTML("beforeend", `<div style='display: none; background: pink;'>${tag}: <div></div><div></div><div></div></div>`);

          let container = document.body.lastElementChild;
          let referenceDiv = container.children[0];
          const maxChild = 10;
          const epsilon = 1;

          // Create the references for different number of children as well
          // as the element that will get the children added / removed.
          for (let i = 0; i <= maxChild; i++)
              referenceDiv.append(generateMathForTag(tag, i));
          let fullReferenceMath = referenceDiv.lastElementChild;
          let fullReferenceTag = fullReferenceMath.firstElementChild;

          let removeChildrenMath = generateMathForTag(tag, maxChild);
          container.children[1].append(removeChildrenMath);
          let removeChildrenTag = removeChildrenMath.firstElementChild;

          let appendChildrenMath = generateMathForTag(tag, 0);
          container.children[2].append(appendChildrenMath);
          let appendChildrenTag = appendChildrenMath.firstElementChild;

          // Make content visible after the DOM is ready so that the layout
          // only happens now.
          container.style.display = "block";

          test(function() {
              assert_true(MathMLFeatureDetection.has_mspace());
              assert_true(MathMLFeatureDetection[`has_${tag}`]());

              for (let i = 0; i < maxChild; i++) {
                  // append and remove children.
                  appendChildrenTag.append(fullReferenceTag.children[i].cloneNode(true));
                  removeChildrenTag.removeChild(removeChildrenTag.lastElementChild);

                  // force layout so we're sure what we're testing against
                  container.getBoundingClientRect();

                  let appendCount = appendChildrenTag.children.length;
                  let removeCount = removeChildrenTag.children.length;
                  if (tag == "mspace") {
                      compareSize(appendChildrenTag, referenceDiv.children[appendCount].firstElementChild, epsilon);
                      childrenHaveEmptyBoundingClientRects(appendChildrenTag);
                      childrenHaveEmptyBoundingClientRects(referenceDiv.children[appendCount].firstElementChild);
                      childrenHaveEmptyBoundingClientRects(removeChildrenTag);
                      childrenHaveEmptyBoundingClientRects(referenceDiv.children[removeCount].firstElementChild);
                  } else {
                      compareLayout(appendChildrenTag, referenceDiv.children[appendCount].firstElementChild, epsilon, `appending ${appendCount}-th child`);
                      compareLayout(removeChildrenTag, referenceDiv.children[removeCount].firstElementChild, epsilon, `removing ${appendCount + 1}-th child`);
                  }
              }

              // Hide back the div after successful testing.
              container.style.display = "none";

          }, `Appending and removing children to ${tag}`);
      }

      done();
  });
</script>
</head>
<body>
  <div id="log"></div>
</body>
</html>