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

Unified Diff: sky/framework/dom-serializer.sky

Issue 922893002: Merge the Sky Engine changes from the SkyDart branch (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 10 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « sky/examples/spinning-square.sky ('k') | sky/tests/TestExpectations » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sky/framework/dom-serializer.sky
diff --git a/sky/framework/dom-serializer.sky b/sky/framework/dom-serializer.sky
index c2d55f564283533757401cb42a9f1b9d6c4d1f90..4558887fa61cc586e4737be28efcc432d5ff8e5a 100644
--- a/sky/framework/dom-serializer.sky
+++ b/sky/framework/dom-serializer.sky
@@ -1,24 +1,28 @@
<script>
-const kEntityMap = new Map([
+import "dart:sky";
+
+final kEntityMap = new Map.fromIterable([
['\u00a0', '&nbsp;'],
['&', '&amp;'],
['<', '&lt;'],
['>', '&gt;'],
['"', '&quot;'],
-]);
+], key: (item) => item[0], value: (item) => item[1]);
-const kTextEscapePattern = /&|<|>|"|\u00a0/g;
-const kAttributeEscapePattern = /&|>|"|\u00a0/g;
+final RegExp kTextEscapePattern = new RegExp(r'&|<|>|"|\u00a0');
+final RegExp kAttributeEscapePattern = new RegExp(r'&|>|"|\u00a0');
const kIndent = ' ';
-function escapeText(value, pattern) {
- return (value || '').replace(pattern, function(match) {
- return kEntityMap.get(match);
+String escapeText(String value, RegExp pattern) {
+ if (value == null)
+ value = '';
+ return value.replaceAllMapped(pattern, (Match match) {
+ return kEntityMap[match[0]];
});
}
-function serializeAttributes(element) {
- var buffer = '';
+String serializeAttributes(Element element) {
+ String buffer = '';
var attributes = element.getAttributes();
for (var i = 0; i < attributes.length; ++i) {
@@ -33,62 +37,58 @@ function serializeAttributes(element) {
return buffer;
}
-function getFirstChild(node) {
- if (node.localName === 'template')
+Node getFirstChild(Node node) {
+ if (node is HTMLTemplateElement)
return node.content.firstChild;
return node.firstChild;
}
-function getLastChild(node) {
- if (node.localName === 'template')
+Node getLastChild(Node node) {
+ if (node is HTMLTemplateElement)
return node.content.lastChild;
return node.lastChild;
}
-function serializeChildren(node, depth) {
- var buffer = '';
- var firstChild = getFirstChild(node);
- var lastChild = getLastChild(node);
- if (firstChild instanceof Element && depth)
- buffer += '\n' + kIndent.repeat(depth);
- for (var child = firstChild; child; child = child.nextSibling) {
+String serializeChildren(Node node, int depth) {
+ String buffer = '';
+ Node firstChild = getFirstChild(node);
+ Node lastChild = getLastChild(node);
+ if (firstChild is Element && depth != 0)
+ buffer += '\n' + (kIndent * depth);
+ for (Node child = firstChild; child != null; child = child.nextSibling) {
buffer += serializeNode(child, depth);
- if (child instanceof Element && child.nextSibling instanceof Element)
- buffer += '\n' + kIndent.repeat(depth);
+ if (child is Element && child.nextSibling is Element)
+ buffer += '\n' + (kIndent * depth);
}
- if (lastChild instanceof Element) {
+ if (lastChild is Element) {
buffer += '\n';
- if (depth)
- buffer += kIndent.repeat(depth - 1);
+ if (depth != 0)
+ buffer += kIndent * (depth - 1);
}
return buffer;
}
-function serializeElement(element, depth) {
- var buffer = '<' + element.tagName + serializeAttributes(element) + '>';
+String serializeElement(Element element, int depth) {
+ String buffer = '<' + element.tagName + serializeAttributes(element) + '>';
buffer += serializeChildren(element, depth + 1);
buffer += '</' + element.tagName + '>';
return buffer;
}
-function serializeText(node) {
- var parent = node.parentNode;
- if (parent && (parent.tagName == 'script' || parent.tagName == 'style'))
+String serializeText(Node node) {
+ Node parent = node.parentNode;
+ if (parent != null && (parent is HTMLScriptElement || parent is HTMLStyleElement))
return node.data;
return escapeText(node.data, kTextEscapePattern);
}
-function serializeNode(node, depth) {
- if (node instanceof Text)
+String serializeNode(Node node, [int depth = 0]) {
+ if (node is Text)
return serializeText(node);
- if (node instanceof Element)
- return serializeElement(node, depth || 0);
- if (node instanceof Document || node instanceof ShadowRoot)
- return serializeChildren(node, depth || 0);
+ if (node is Element)
+ return serializeElement(node, depth);
+ if (node is Document || node is ShadowRoot)
+ return serializeChildren(node, depth);
throw new Error('Cannot serialize node');
}
-
-module.exports = {
- serializeNode: serializeNode,
-};
</script>
« no previous file with comments | « sky/examples/spinning-square.sky ('k') | sky/tests/TestExpectations » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698