summaryrefslogtreecommitdiffstats
path: root/layout/reftests/css-ruby/dynamic-insertion.js
blob: 0bf6cd969779db94866a58ab7b534d2f53a5b99a (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
window.onload = function() {
  // Force a reflow before any changes.
  document.body.clientWidth;

  var elems = document.querySelectorAll('[data-insert]');
  Array.from(elems).forEach(function(e) {
    var parent, ref;
    switch (e.dataset.insert) {
      case 'start':
        parent = e;
        ref = e.firstChild;
        break;

      case 'end':
        parent = e;
        ref = null;
        break;

      case 'before':
        parent = e.parentNode;
        ref = e;
        break;

      case 'after':
        parent = e.parentNode;
        ref = e.nextSibling;
        break;
    }

    var elem, textnode;
    if ('text' in e.dataset) {
      textnode = document.createTextNode(e.dataset.text);
    }
    if ('tag' in e.dataset) {
      elem = document.createElement(e.dataset.tag);
      if (textnode) {
        elem.appendChild(textnode);
      }
    }
    parent.insertBefore(elem ? elem : textnode, ref);
  });
};