Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(163)

Side by Side Diff: sky/framework/dom-serializer.sky

Issue 867963006: Add the <t> element and ignore whitespace outside it. (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Do it for a whole subtree. Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « sky/examples/city-list/city-list.sky ('k') | sky/tests/framework/binding-expected.txt » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 <script> 1 <script>
2 const kEntityMap = new Map([ 2 const kEntityMap = new Map([
3 ['\u00a0', '&nbsp;'], 3 ['\u00a0', '&nbsp;'],
4 ['&', '&amp;'], 4 ['&', '&amp;'],
5 ['<', '&lt;'], 5 ['<', '&lt;'],
6 ['>', '&gt;'], 6 ['>', '&gt;'],
7 ['"', '&quot;'], 7 ['"', '&quot;'],
8 ]); 8 ]);
9 9
10 const kTextEscapePattern = /&|<|>|"|\u00a0/g; 10 const kTextEscapePattern = /&|<|>|"|\u00a0/g;
11 const kAttributeEscapePattern = /&|>|"|\u00a0/g; 11 const kAttributeEscapePattern = /&|>|"|\u00a0/g;
12 const kIndent = ' ';
12 13
13 function escapeText(value, pattern) { 14 function escapeText(value, pattern) {
14 return (value || '').replace(pattern, function(match) { 15 return (value || '').replace(pattern, function(match) {
15 return kEntityMap.get(match); 16 return kEntityMap.get(match);
16 }); 17 });
17 } 18 }
18 19
19 function serializeAttributes(element) { 20 function serializeAttributes(element) {
20 var buffer = ""; 21 var buffer = '';
21 var attributes = element.getAttributes(); 22 var attributes = element.getAttributes();
22 23
23 for (var i = 0; i < attributes.length; ++i) { 24 for (var i = 0; i < attributes.length; ++i) {
24 var attribute = attributes[i]; 25 var attribute = attributes[i];
25 buffer += ' '; 26 buffer += ' ';
26 buffer += attribute.name; 27 buffer += attribute.name;
27 buffer += '="'; 28 buffer += '="';
28 buffer += escapeText(attribute.value, kAttributeEscapePattern); 29 buffer += escapeText(attribute.value, kAttributeEscapePattern);
29 buffer += '"'; 30 buffer += '"';
30 } 31 }
31 32
32 return buffer; 33 return buffer;
33 } 34 }
34 35
35 function getFirstChild(element) { 36 function getFirstChild(node) {
36 if (element.tagName === 'template') 37 if (node.localName === 'template')
37 return element.content.firstChild; 38 return node.content.firstChild;
38 return element.firstChild; 39 return node.firstChild;
39 } 40 }
40 41
41 function serializeElementChildren(element) { 42 function getLastChild(node) {
42 if (element.tagName == "script" || element.tagName == "style") 43 if (node.localName === 'template')
43 return element.textContent; 44 return node.content.lastChild;
44 return serializeChildren(getFirstChild(element)); 45 return node.lastChild;
45 } 46 }
46 47
47 function serializeChildren(firstChild) { 48 function serializeChildren(node, depth) {
48 var buffer = ""; 49 var buffer = '';
49 for (var child = firstChild; child; child = child.nextSibling) 50 var firstChild = getFirstChild(node);
50 buffer += serializeNode(child); 51 var lastChild = getLastChild(node);
52 if (firstChild instanceof Element && depth)
53 buffer += '\n' + kIndent.repeat(depth);
54 for (var child = firstChild; child; child = child.nextSibling) {
55 buffer += serializeNode(child, depth);
56 if (child instanceof Element && child.nextSibling instanceof Element)
57 buffer += '\n' + kIndent.repeat(depth);
58 }
59 if (lastChild instanceof Element) {
60 buffer += '\n';
61 if (depth)
62 buffer += kIndent.repeat(depth - 1);
63 }
51 return buffer; 64 return buffer;
52 } 65 }
53 66
54 function serializeElement(element) { 67 function serializeElement(element, depth) {
55 var buffer = '<' + element.tagName + serializeAttributes(element) + '>'; 68 var buffer = '<' + element.tagName + serializeAttributes(element) + '>';
56 buffer += serializeElementChildren(element); 69 buffer += serializeChildren(element, depth + 1);
57 buffer += '</' + element.tagName + '>'; 70 buffer += '</' + element.tagName + '>';
58 return buffer; 71 return buffer;
59 } 72 }
60 73
61 function serializeNode(node) { 74 function serializeText(node) {
75 var parent = node.parentNode;
76 if (parent && (parent.tagName == 'script' || parent.tagName == 'style'))
77 return node.data;
78 return escapeText(node.data, kTextEscapePattern);
79 }
80
81 function serializeNode(node, depth) {
62 if (node instanceof Text) 82 if (node instanceof Text)
63 return escapeText(node.data, kTextEscapePattern); 83 return serializeText(node);
64 if (node instanceof Element) 84 if (node instanceof Element)
65 return serializeElement(node); 85 return serializeElement(node, depth || 0);
66 if (node instanceof Document || node instanceof ShadowRoot) 86 if (node instanceof Document || node instanceof ShadowRoot)
67 return serializeChildren(node.firstChild); 87 return serializeChildren(node, depth || 0);
68 throw new Error("Cannot serialize node"); 88 throw new Error('Cannot serialize node');
69 } 89 }
70 90
71 module.exports = { 91 module.exports = {
72 serializeNode: serializeNode, 92 serializeNode: serializeNode,
73 }; 93 };
74 </script> 94 </script>
OLDNEW
« no previous file with comments | « sky/examples/city-list/city-list.sky ('k') | sky/tests/framework/binding-expected.txt » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698