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