summaryrefslogtreecommitdiff
path: root/libgo/go/html/testdata/webkit/dom2string.js
blob: 45897fda4d85dd004a6e22a9254a936610a0e3a1 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
String.prototype.toAsciiLowerCase = function () {
  var output = "";
  for (var i = 0, len = this.length; i < len; ++i) {
    if (this.charCodeAt(i) >= 0x41 && this.charCodeAt(i) <= 0x5A) {
      output += String.fromCharCode(this.charCodeAt(i) + 0x20)
    } else {
      output += this.charAt(i);
    }
  }
  return output;
}

function indent(ancestors) {
  var str = "";
  if (ancestors > 0) {
    while (ancestors--)
      str += "  ";
  }
  return str;
}

function dom2string(node, ancestors) {
  var str = "";
  if (typeof ancestors == "undefined")
    var ancestors = 0;
  if (!node.firstChild)
    return "| ";
  var parent = node;
  var current = node.firstChild;
  var next = null;
  var misnested = null;
  for (;;) {
    str += "\n| " + indent(ancestors);
    switch (current.nodeType) {
      case 10:
        str += '<!DOCTYPE ' + current.nodeName + '>';
        break;
      case 8:
        try {
          str += '<!-- ' + current.nodeValue + ' -->';
        } catch (e) {
          str += '<!--  -->';
        }
        if (parent != current.parentNode) {
          return str += ' (misnested... aborting)';
        }
        break;
      case 7:
        str += '<?' + current.nodeName + current.nodeValue + '>';
        break;
      case 4:
        str += '<![CDATA[ ' + current.nodeValue + ' ]]>';
        break;
      case 3:
        str += '"' + current.nodeValue + '"';
        if (parent != current.parentNode) {
          return str += ' (misnested... aborting)';
        }
        break;
      case 1:
        str += "<";
        switch (current.namespaceURI) {
          case "http://www.w3.org/2000/svg":
            str += "svg ";
            break;
          case "http://www.w3.org/1998/Math/MathML":
            str += "math ";
            break;
        }
        if (current.localName && current.namespaceURI && current.namespaceURI != null) {
          str += current.localName;
        } else {
          str += current.nodeName.toAsciiLowerCase();
        }
        str += '>';
        if (parent != current.parentNode) {
          return str += ' (misnested... aborting)';
        } else {
          if (current.attributes) {
            var attrNames = [];
            var attrPos = {};
            for (var j = 0; j < current.attributes.length; j += 1) {
              if (current.attributes[j].specified) {
                var name = "";
                switch (current.attributes[j].namespaceURI) {
                  case "http://www.w3.org/XML/1998/namespace":
                    name += "xml ";
                    break;
                  case "http://www.w3.org/2000/xmlns/":
                    name += "xmlns ";
                    break;
                  case "http://www.w3.org/1999/xlink":
                    name += "xlink ";
                    break;
                }
                if (current.attributes[j].localName) {
                  name += current.attributes[j].localName;
                } else {
                  name += current.attributes[j].nodeName;
                }
                attrNames.push(name);
                attrPos[name] = j;
              }
            }
            if (attrNames.length > 0) {
              attrNames.sort();
              for (var j = 0; j < attrNames.length; j += 1) {
                str += "\n| " + indent(1 + ancestors) + attrNames[j];
                str += '="' + current.attributes[attrPos[attrNames[j]]].nodeValue + '"';
              }
            }
          }
          if (next = current.firstChild) {
            parent = current;
            current = next;
            ancestors++;
            continue;
          }
        }
        break;
    }
    for (;;) {
      if (next = current.nextSibling) {
        current = next;
        break;
      }
      current = current.parentNode;
      parent = parent.parentNode;
      ancestors--;
      if (current == node) {
        return str.substring(1);
      }
    }
  }
}