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. |