OLD | NEW |
| (Empty) |
1 <!-- | |
2 // Copyright 2015 The Chromium Authors. All rights reserved. | |
3 // Use of this source code is governed by a BSD-style license that can be | |
4 // found in the LICENSE file. | |
5 --> | |
6 <script> | |
7 import "dart:sky"; | |
8 | |
9 final kEntityMap = new Map.fromIterable([ | |
10 ['\u00a0', ' '], | |
11 ['&', '&'], | |
12 ['<', '<'], | |
13 ['>', '>'], | |
14 ['"', '"'], | |
15 ], key: (item) => item[0], value: (item) => item[1]); | |
16 | |
17 final RegExp kTextEscapePattern = new RegExp(r'&|<|>|"|\u00a0'); | |
18 final RegExp kAttributeEscapePattern = new RegExp(r'&|>|"|\u00a0'); | |
19 const kIndent = ' '; | |
20 | |
21 String escapeText(String value, RegExp pattern) { | |
22 if (value == null) | |
23 value = ''; | |
24 return value.replaceAllMapped(pattern, (Match match) { | |
25 return kEntityMap[match[0]]; | |
26 }); | |
27 } | |
28 | |
29 String serializeAttributes(Element element) { | |
30 String buffer = ''; | |
31 var attributes = element.getAttributes(); | |
32 | |
33 for (var i = 0; i < attributes.length; ++i) { | |
34 var attribute = attributes[i]; | |
35 buffer += ' '; | |
36 buffer += attribute.name; | |
37 buffer += '="'; | |
38 buffer += escapeText(attribute.value, kAttributeEscapePattern); | |
39 buffer += '"'; | |
40 } | |
41 | |
42 return buffer; | |
43 } | |
44 | |
45 Node getFirstChild(Node node) { | |
46 if (node is HTMLTemplateElement) | |
47 return node.content.firstChild; | |
48 return node.firstChild; | |
49 } | |
50 | |
51 Node getLastChild(Node node) { | |
52 if (node is HTMLTemplateElement) | |
53 return node.content.lastChild; | |
54 return node.lastChild; | |
55 } | |
56 | |
57 String serializeChildren(Node node, int depth) { | |
58 String buffer = ''; | |
59 Node firstChild = getFirstChild(node); | |
60 Node lastChild = getLastChild(node); | |
61 if (firstChild is Element && depth != 0) | |
62 buffer += '\n' + (kIndent * depth); | |
63 for (Node child = firstChild; child != null; child = child.nextSibling) { | |
64 buffer += serializeNode(child, depth); | |
65 if (child is Element && child.nextSibling is Element) | |
66 buffer += '\n' + (kIndent * depth); | |
67 } | |
68 if (lastChild is Element) { | |
69 buffer += '\n'; | |
70 if (depth != 0) | |
71 buffer += kIndent * (depth - 1); | |
72 } | |
73 return buffer; | |
74 } | |
75 | |
76 String serializeElement(Element element, int depth) { | |
77 String buffer = '<' + element.tagName + serializeAttributes(element) + '>'; | |
78 buffer += serializeChildren(element, depth + 1); | |
79 buffer += '</' + element.tagName + '>'; | |
80 return buffer; | |
81 } | |
82 | |
83 String serializeText(Node node) { | |
84 Node parent = node.parentNode; | |
85 if (parent != null && (parent is HTMLScriptElement || parent is HTMLStyleEleme
nt)) | |
86 return node.data; | |
87 return escapeText(node.data, kTextEscapePattern); | |
88 } | |
89 | |
90 String serializeNode(Node node, [int depth = 0]) { | |
91 if (node is Text) | |
92 return serializeText(node); | |
93 if (node is Element) | |
94 return serializeElement(node, depth); | |
95 if (node is Document || node is ShadowRoot) | |
96 return serializeChildren(node, depth); | |
97 throw new Error('Cannot serialize node'); | |
98 } | |
99 </script> | |
OLD | NEW |