flutter_flutter/framework/dom-serializer.sky
Adam Barth b6f511c6a9 Port Sky widgets demo to Dart
This CL updates sky-box, sky-button, sky-checkbox, sky-input, and sky-radio to
work in Dart. We don't have a data binding system yet, so there's a bit more
plumbing in the code.

This CL adds support for sky-element@attributes, which lets you specify which
attributes your element supports. We use this information to synthesize getters
and setters for those attributes and to dispatch to mumbleChanged methods when
the attributes change.

I've also wrapped the widgets demo itself in a sky-scrollable so the whole
thing scrolls.

R=eseidel@chromium.org

Review URL: https://codereview.chromium.org/946813005
2015-02-23 08:30:36 -08:00

100 lines
2.7 KiB
Plaintext

<!--
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-->
<script>
import "dart:sky";
final kEntityMap = new Map.fromIterable([
['\u00a0', '&nbsp;'],
['&', '&amp;'],
['<', '&lt;'],
['>', '&gt;'],
['"', '&quot;'],
], key: (item) => item[0], value: (item) => item[1]);
final RegExp kTextEscapePattern = new RegExp(r'&|<|>|"|\u00a0');
final RegExp kAttributeEscapePattern = new RegExp(r'&|>|"|\u00a0');
const kIndent = ' ';
String escapeText(String value, RegExp pattern) {
if (value == null)
value = '';
return value.replaceAllMapped(pattern, (Match match) {
return kEntityMap[match[0]];
});
}
String serializeAttributes(Element element) {
String buffer = '';
var attributes = element.getAttributes();
for (var i = 0; i < attributes.length; ++i) {
var attribute = attributes[i];
buffer += ' ';
buffer += attribute.name;
buffer += '="';
buffer += escapeText(attribute.value, kAttributeEscapePattern);
buffer += '"';
}
return buffer;
}
Node getFirstChild(Node node) {
if (node is HTMLTemplateElement)
return node.content.firstChild;
return node.firstChild;
}
Node getLastChild(Node node) {
if (node is HTMLTemplateElement)
return node.content.lastChild;
return node.lastChild;
}
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 is Element && child.nextSibling is Element)
buffer += '\n' + (kIndent * depth);
}
if (lastChild is Element) {
buffer += '\n';
if (depth != 0)
buffer += kIndent * (depth - 1);
}
return buffer;
}
String serializeElement(Element element, int depth) {
String buffer = '<' + element.tagName + serializeAttributes(element) + '>';
buffer += serializeChildren(element, depth + 1);
buffer += '</' + element.tagName + '>';
return buffer;
}
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);
}
String serializeNode(Node node, [int depth = 0]) {
if (node is Text)
return serializeText(node);
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');
}
</script>