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

Unified Diff: sdk/lib/svg/dartium/svg_dartium.dart

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
« no previous file with comments | « sdk/lib/svg/dart2js/svg_dart2js.dart ('k') | tests/html/cssstyledeclaration_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/svg/dartium/svg_dartium.dart
diff --git a/sdk/lib/svg/dartium/svg_dartium.dart b/sdk/lib/svg/dartium/svg_dartium.dart
index 8859092bd2f58835076f32f52949cbf573715243..e9d3b1e6e0a43b4b360db4922c7d682905ba6b94 100644
--- a/sdk/lib/svg/dartium/svg_dartium.dart
+++ b/sdk/lib/svg/dartium/svg_dartium.dart
@@ -18,40 +18,12 @@ import 'dart:nativewrappers';
// BSD-style license that can be found in the LICENSE file.
-final _START_TAG_REGEXP = new RegExp('<(\\w+)');
-
class _SvgElementFactoryProvider {
static SvgElement createSvgElement_tag(String tag) {
final Element temp =
document.$dom_createElementNS("http://www.w3.org/2000/svg", tag);
return temp;
}
-
- static SvgElement createSvgElement_svg(String svg) {
- Element parentTag;
- final match = _START_TAG_REGEXP.firstMatch(svg);
- if (match != null && match.group(1).toLowerCase() == 'svg') {
- parentTag = new Element.tag('div');
- } else {
- parentTag = new SvgSvgElement();
- }
-
- parentTag.innerHtml = svg;
- if (parentTag.children.length == 1) return parentTag.children.removeLast();
-
- throw new ArgumentError(
- 'SVG had ${parentTag.children.length} '
- 'top-level children but 1 expected');
- }
-}
-
-class _SvgSvgElementFactoryProvider {
- static SvgSvgElement createSvgSvgElement() {
- final el = new SvgElement.tag("svg");
- // The SVG spec requires the version attribute to match the spec version
- el.attributes['version'] = "1.1";
- return el;
- }
}
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
@@ -5343,10 +5315,28 @@ class _AttributeClassSet extends CssClassSetImpl {
@DomName('SVGElement')
@Unstable()
class SvgElement extends Element {
+ static final _START_TAG_REGEXP = new RegExp('<(\\w+)');
+
factory SvgElement.tag(String tag) =>
- _SvgElementFactoryProvider.createSvgElement_tag(tag);
- factory SvgElement.svg(String svg) =>
- _SvgElementFactoryProvider.createSvgElement_svg(svg);
+ document.$dom_createElementNS("http://www.w3.org/2000/svg", tag);
+ factory SvgElement.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 {
@@ -5378,12 +5368,33 @@ class SvgElement extends Element {
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.
@@ -5479,7 +5490,12 @@ class SvgElement extends Element {
@DomName('SVGSVGElement')
@Unstable()
class SvgSvgElement extends GraphicsElement implements FitToViewBox, ExternalResourcesRequired, ZoomAndPan {
- factory SvgSvgElement() => _SvgSvgElementFactoryProvider.createSvgSvgElement();
+ factory SvgSvgElement() {
+ final el = new SvgElement.tag("svg");
+ // The SVG spec requires the version attribute to match the spec version
+ el.attributes['version'] = "1.1";
+ return el;
+ }
// To suppress missing implicit constructor warnings.
factory SvgSvgElement._() { throw new UnsupportedError("Not supported"); }
« no previous file with comments | « sdk/lib/svg/dart2js/svg_dart2js.dart ('k') | tests/html/cssstyledeclaration_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698