forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdump-as-markup.js
453 lines (383 loc) · 15.1 KB
/
dump-as-markup.js
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/**
* There are three basic use cases of dumpAsMarkup
*
* 1. Dump the entire DOM when the page is loaded
* When this script is included but no method of Markup is called,
* it dumps the DOM of each frame loaded.
*
* 2. Dump the content of a specific element when the page is loaded
* When Markup.setNodeToDump is called with some element or the id of some element,
* it dumps the content of the specified element as supposed to the entire DOM tree.
*
* 3. Dump the content of a specific element multiple times while the page is loading
* Calling Markup.dump would dump the content of the element set by setNodeToDump or the entire DOM.
* Optionally specify the node to dump and the description for each call of dump.
*/
if (window.testRunner)
testRunner.dumpAsText();
// Namespace
// FIXME: Rename dump-as-markup.js to dump-dom.js and Markup to DOM.
var Markup = {};
// The description of what this test is testing. Gets prepended to the dumped markup.
Markup.description = function(description)
{
Markup._test_description = description;
}
// Dumps the markup for the given node (HTML element if no node is given).
// Over-writes the body's content with the markup in layout test mode. Appends
// a pre element when loaded manually, in order to aid debugging.
Markup.dump = function(opt_node, opt_description)
{
if (typeof opt_node == 'string')
opt_node = document.getElementById(opt_node);
var node = opt_node || document
var markup = "";
Markup._dumpCalls++;
if (Markup._dumpCalls > 1 || opt_description) {
if (!opt_description)
opt_description = "Dump of markup " + Markup._dumpCalls
if (Markup._dumpCalls > 1)
markup += '\n';
markup += '\n' + opt_description + ':\n';
} else
Markup._firstCallDidNotHaveDescription = true;
markup += Markup.get(node);
if (!Markup._container) {
Markup._container = document.createElement('pre');
Markup._container.style.width = '100%';
}
if (Markup._dumpCalls == 2 && Markup._firstCallDidNotHaveDescription) {
var wrapper = Markup._container.getElementsByClassName('dump-as-markup-span')[0];
wrapper.insertBefore(document.createTextNode('\nDump of markup 1:\n'), wrapper.firstChild);
}
// FIXME: Have this respect testRunner.dumpChildFramesAsText?
// FIXME: Should we care about framesets?
// DocumentFragment doesn't have a getElementsByTagName method.
if (node.getElementsByTagName) {
var iframes = node.getElementsByTagName('iframe');
for (var i = 0; i < iframes.length; i++) {
markup += '\n\nFRAME ' + i + ':\n'
try {
markup += Markup.get(iframes[i].contentDocument.body.parentElement);
} catch (e) {
markup += 'FIXME: Add method to layout test controller to get access to cross-origin frames.';
}
}
}
if (Markup._test_description && Markup._dumpCalls == 1)
Markup._container.appendChild(document.createTextNode(Markup._test_description + '\n'))
var wrapper = document.createElement('span');
wrapper.className = 'dump-as-markup-span';
wrapper.appendChild(document.createTextNode(markup));
Markup._container.appendChild(wrapper);
}
Markup.addRange = function(range, description)
{
if (!Markup._rangeCount) {
Markup._ranges = {};
Markup._rangeCount = 0;
}
Markup._ranges[description || `range #${Markup._rangeCount}`] = range;
++Markup._rangeCount;
}
Markup.noAutoDump = function()
{
window.removeEventListener('load', Markup.notifyDone, false);
}
Markup.waitUntilDone = function()
{
if (window.testRunner)
testRunner.waitUntilDone();
Markup.noAutoDump();
}
Markup.notifyDone = function()
{
// Need to waitUntilDone or some tests won't finish appending the markup before the text is dumped.
if (window.testRunner)
testRunner.waitUntilDone();
// If dump has already been called, don't bother to dump again
if (!Markup._dumpCalls)
Markup.dump();
// In non-layout test mode, append the results in a pre so that we don't
// clobber the test itself. But when in layout test mode, we don't want
// side effects from the test to be included in the results.
if (window.testRunner)
document.body.innerHTML = '';
document.body.appendChild(Markup._container);
if (window.testRunner)
testRunner.notifyDone();
}
Markup.useHTML5libOutputFormat = function()
{
Markup._useHTML5libOutputFormat = true;
}
Markup.get = function(node)
{
var shadowRootList = {};
var markup = Markup._getShadowHostIfPossible(node, 0, shadowRootList);
if (markup)
return markup.substring(1);
if (!node.firstChild)
return '| ';
// Don't print any markup for the root node.
var len = node.childNodes.length;
var i = 0;
for (; i < len; i++) {
markup += Markup._getSelectionAndRangeMarkersWithIdentation(node, i, 0);
markup += Markup._get(node.childNodes[i], 0, shadowRootList);
}
markup += Markup._getSelectionAndRangeMarkersWithIdentation(node, len, 0);
return markup.substring(1);
}
// Returns the markup for the given node. To be used for cases where a test needs
// to get the markup but not clobber the whole page.
Markup._get = function(node, depth, shadowRootList)
{
var str = Markup._indent(depth);
switch (node.nodeType) {
case Node.DOCUMENT_TYPE_NODE:
str += '<!DOCTYPE ' + node.nodeName;
// FIXME: The actual MarkupAccumulator in WebKit handles these quite differently.
// Should we change to match that format? Would probably need to change test results,
// but it could help us distinguish empty strings from missing properties and
// would include internalSubset, which this omits.
if (node.publicId || node.systemId) {
str += ' "' + (node.publicId || '') + '"';
str += ' "' + (node.systemId || '') + '"';
}
str += '>';
break;
case Node.COMMENT_NODE:
try {
str += '<!-- ' + node.nodeValue + ' -->';
} catch (e) {
str += '<!-- -->';
}
break;
case Node.PROCESSING_INSTRUCTION_NODE:
str += '<?' + node.nodeName + node.nodeValue + '>';
break;
case Node.CDATA_SECTION_NODE:
str += '<![CDATA[ ' + node.nodeValue + ' ]]>';
break;
case Node.TEXT_NODE:
str += '"' + Markup._getMarkupForTextNode(node) + '"';
break;
case Node.ELEMENT_NODE:
str += "<";
str += Markup._namespace(node)
if (node.localName && node.namespaceURI && node.namespaceURI != null)
str += node.localName;
else
str += Markup._toAsciiLowerCase(node.nodeName);
str += '>';
if (node.attributes) {
var attrNames = [];
var attrPos = {};
for (var j = 0; j < node.attributes.length; j += 1) {
if (node.attributes[j].specified) {
var name = Markup._namespace(node.attributes[j])
name += node.attributes[j].localName || node.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 += Markup._indent(depth + 1) + attrNames[j];
str += '="' + node.attributes[attrPos[attrNames[j]]].nodeValue + '"';
}
}
}
if (!Markup._useHTML5libOutputFormat)
if (node.nodeName == "INPUT" || node.nodeName == "TEXTAREA")
str += Markup._indent(depth + 1) + 'this.value="' + node.value + '"';
break;
case Node.DOCUMENT_FRAGMENT_NODE:
if (shadowRootList && window.internals && internals.address(node) in shadowRootList)
str += "<shadow:root>";
else
str += "content";
}
if (node.namespaceURI = 'http://www.w3.org/1999/xhtml' && node.tagName == 'TEMPLATE')
str += Markup._get(node.content, depth + 1, shadowRootList);
for (var i = 0, len = node.childNodes.length; i < len; i++) {
str += Markup._getSelectionAndRangeMarkersWithIdentation(node, i, depth + 1);
str += Markup._get(node.childNodes[i], depth + 1, shadowRootList);
}
str += Markup._getShadowHostIfPossible(node, depth, shadowRootList);
str += Markup._getSelectionAndRangeMarkersWithIdentation(node, i, depth + 1);
return str;
}
Markup._getShadowHostIfPossible = function (node, depth, shadowRootList)
{
if (!Markup._useHTML5libOutputFormat && node.nodeType == Node.ELEMENT_NODE && window.internals) {
var root = window.internals.shadowRoot(node);
if (root) {
shadowRootList[internals.address(root)] = true;
return Markup._get(root, depth + 1, shadowRootList);
}
}
return '';
}
Markup._namespace = function(node)
{
if (Markup._NAMESPACE_URI_MAP[node.namespaceURI])
return Markup._NAMESPACE_URI_MAP[node.namespaceURI] + ' ';
return '';
}
Markup._dumpCalls = 0
Markup._indent = function(depth)
{
return "\n| " + new Array(depth * 2 + 1).join(' ');
}
Markup._toAsciiLowerCase = function (str) {
var output = "";
for (var i = 0, len = this.length; i < len; ++i) {
if (str.charCodeAt(i) >= 0x41 && str.charCodeAt(i) <= 0x5A)
output += String.fromCharCode(str.charCodeAt(i) + 0x20)
else
output += str.charAt(i);
}
return output;
}
Markup._NAMESPACE_URI_MAP = {
"http://www.w3.org/2000/svg": "svg",
"http://www.w3.org/1998/Math/MathML": "math",
"http://www.w3.org/XML/1998/namespace": "xml",
"http://www.w3.org/2000/xmlns/": "xmlns",
"http://www.w3.org/1999/xlink": "xlink"
}
Markup._getSelectionFromNode = function(node)
{
return node.ownerDocument.defaultView ? node.ownerDocument.defaultView.getSelection() : null;
}
Markup._SELECTION_FOCUS = '<#selection-focus>';
Markup._SELECTION_ANCHOR = '<#selection-anchor>';
Markup._SELECTION_CARET = '<#selection-caret>';
Markup._getMarkupForTextNode = function(node)
{
innerMarkup = node.nodeValue;
var startOffset, endOffset, startText, endText;
var sel = Markup._getSelectionFromNode(node);
// Firefox doesn't have a sel in a display:none iframe.
// https://bugs.webkit.org/show_bug.cgi?id=43655
var rangeDescriptions = Object.getOwnPropertyNames(Markup._ranges || { });
var matchingRanges = [];
var identifier = 0;
for (var description of rangeDescriptions) {
var range = Markup._ranges[description];
if (range.startContainer == node)
matchingRanges.push({offset: range.startOffset, identifier, label: range.collapsed ? `[${description} collapsed]` : `[${description} start]`});
++identifier;
}
if (sel) {
if (node == sel.anchorNode && node == sel.focusNode) {
if (sel.isCollapsed)
matchingRanges.push({offset: sel.anchorOffset, identifier, label: Markup._SELECTION_CARET});
else {
matchingRanges.push({offset: sel.anchorOffset, identifier, label: Markup._SELECTION_ANCHOR});
matchingRanges.push({offset: sel.focusOffset, identifier, label: Markup._SELECTION_FOCUS});
}
} else if (node == sel.anchorNode)
matchingRanges.push({offset: sel.anchorOffset, identifier, label: Markup._SELECTION_ANCHOR});
else if (node == sel.focusNode)
matchingRanges.push({offset: sel.focusOffset, identifier, label: Markup._SELECTION_FOCUS});
++identifier;
}
for (var description of rangeDescriptions.reverse()) {
var range = Markup._ranges[description];
if (range.endContainer == node && !range.collapsed)
matchingRanges.push({offset: range.endOffset, identifier, label: `[${description} end]`});
++identifier;
}
if (matchingRanges.length) {
matchingRanges.sort(function(a, b) {
var diff = a.offset - b.offset;
if (diff)
return diff;
return a.identifier - b.identifier;
});
var tokens = [];
var lastEnd = 0;
for (var i = 0; i < matchingRanges.length; ++i) {
var item = matchingRanges[i];
if (item.offset == lastEnd)
tokens.push(item.label);
var nextOffset = (matchingRanges[i + 1] || {offset: innerMarkup.length}).offset;
if (lastEnd != item.offset)
tokens.push(innerMarkup.substring(lastEnd, item.offset));
if (item.offset != lastEnd)
tokens.push(item.label);
lastEnd = item.offset;
}
if (lastEnd < innerMarkup.length)
tokens.push(innerMarkup.substring(lastEnd));
innerMarkup = tokens.join('');
}
if (!Markup._useHTML5libOutputFormat)
innerMarkup = innerMarkup.replace(/\\/g, "\\\\").replace(/\n/g, "\\n");
return innerMarkup;
}
Markup._getSelectionAndRangeMarkersWithIdentation = function(node, index, depth)
{
var result = '';
for (var marker of Markup._getRangeStartMarkers(node, index))
result += Markup._indent(depth) + marker;
var selection = Markup._getSelectionMarker(node, index);
if (selection)
result += Markup._indent(depth) + selection;
for (var marker of Markup._getRangeEndMarkers(node, index))
result += Markup._indent(depth) + marker;
return result;
}
Markup._getSelectionMarker = function(node, index)
{
if (node.nodeType != 1)
return '';
var sel = Markup._getSelectionFromNode(node);
// Firefox doesn't have a sel in a display:none iframe.
// https://bugs.webkit.org/show_bug.cgi?id=43655
if (!sel)
return '';
if (index == sel.anchorOffset && node == sel.anchorNode) {
if (sel.isCollapsed)
return Markup._SELECTION_CARET;
else
return Markup._SELECTION_ANCHOR;
} else if (index == sel.focusOffset && node == sel.focusNode)
return Markup._SELECTION_FOCUS;
return '';
}
Markup._getRangeStartMarkers = function(node, index)
{
if (node.nodeType != 1 || !Markup._rangeCount)
return [];
var rangeDescriptions = Object.getOwnPropertyNames(Markup._ranges);
var markers = [];
for (var description of rangeDescriptions) {
var range = Markup._ranges[description];
if (index == range.startOffset && node == range.startContainer) {
if (range.collapsed)
markers.push(`[${description} collapsed]`);
else
markers.push(`[${description} start]`);
}
}
return markers;
}
Markup._getRangeEndMarkers = function(node, index)
{
if (node.nodeType != 1 || !Markup._rangeCount)
return [];
var rangeDescriptions = Object.getOwnPropertyNames(Markup._ranges).reverse();
var markers = [];
for (var description of rangeDescriptions) {
var range = Markup._ranges[description];
if (index == range.endOffset && node == range.endContainer)
markers.push(`[${description} end]`);
}
return markers;
}
window.addEventListener('load', Markup.notifyDone, false);