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

Unified Diff: tools/dom/templates/html/impl/impl_SVGElement.darttemplate

Issue 16374007: First rev of Safe DOM (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 4 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
Index: tools/dom/templates/html/impl/impl_SVGElement.darttemplate
diff --git a/tools/dom/templates/html/impl/impl_SVGElement.darttemplate b/tools/dom/templates/html/impl/impl_SVGElement.darttemplate
index 9e87d5b7e98f6254b790b9249ee2504ca977223c..576854b83edfc1a55fec97ac398676ca4961742a 100644
--- a/tools/dom/templates/html/impl/impl_SVGElement.darttemplate
+++ b/tools/dom/templates/html/impl/impl_SVGElement.darttemplate
@@ -31,10 +31,28 @@ class _AttributeClassSet extends CssClassSetImpl {
}
$(ANNOTATIONS)$(CLASS_MODIFIERS)class $CLASSNAME$EXTENDS$IMPLEMENTS$NATIVESPEC {
+ static final _START_TAG_REGEXP = new RegExp('<(\\w+)');
+
factory $CLASSNAME.tag(String tag) =>
- _$(CLASSNAME)FactoryProvider.create$(CLASSNAME)_tag(tag);
- factory $CLASSNAME.svg(String svg) =>
- _$(CLASSNAME)FactoryProvider.create$(CLASSNAME)_svg(svg);
+ document.$dom_createElementNS("http://www.w3.org/2000/svg", tag);
+ factory $CLASSNAME.svg(String svg,
+ {NodeValidator validator, NodeTreeSanitizer treeSanitizer}) {
+
+ if (validator == null && treeSanitizer == null) {
+ validator = new NodeValidatorBuilder.common()..allowSvg();
+ }
+
+ final match = _START_TAG_REGEXP.firstMatch(svg);
+ var parentElement;
+ if (match != null && match.group(1).toLowerCase() == 'svg') {
+ parentElement = document.body;
+ } else {
+ parentElement = new SvgSvgElement();
+ }
+ var fragment = parentElement.createFragment(svg, validator: validator,
+ treeSanitizer: treeSanitizer);
+ return fragment.nodes.where((e) => e is SvgElement).single;
+ }
_AttributeClassSet _cssClassSet;
CssClassSet get classes {
@@ -66,12 +84,33 @@ $(ANNOTATIONS)$(CLASS_MODIFIERS)class $CLASSNAME$EXTENDS$IMPLEMENTS$NATIVESPEC {
return container.innerHtml;
}
- void set innerHtml(String svg) {
- final container = new Element.tag("div");
- // Wrap the SVG string in <svg> so that SvgElements are created, rather than
- // HTMLElements.
- container.innerHtml = '<svg version="1.1">$svg</svg>';
- this.children = container.children[0].children;
+ void set innerHtml(String value) {
+ this.setInnerHtml(value);
+ }
+
+ DocumentFragment createFragment(String svg,
+ {NodeValidator validator, NodeTreeSanitizer treeSanitizer}) {
+
+ if (treeSanitizer == null) {
+ if (validator == null) {
+ validator = new NodeValidatorBuilder.common()
+ ..allowSvg();
+ }
+ treeSanitizer = new NodeTreeSanitizer(validator);
+ }
+
+ // We create a fragment which will parse in the HTML parser
+ var html = '<svg version="1.1">$svg</svg>';
+ var fragment = document.body.createFragment(html,
+ treeSanitizer: treeSanitizer);
+
+ var svgFragment = new DocumentFragment();
+ // The root is the <svg/> element, need to pull out the contents.
+ var root = fragment.nodes.single;
+ while (root.firstChild != null) {
+ svgFragment.append(root.firstChild);
+ }
+ return svgFragment;
}
// Unsupported methods inherited from Element.

Powered by Google App Engine
This is Rietveld 408576698