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

Side by Side 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, 6 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 part of $LIBRARYNAME; 5 part of $LIBRARYNAME;
6 6
7 class _AttributeClassSet extends CssClassSetImpl { 7 class _AttributeClassSet extends CssClassSetImpl {
8 final Element _element; 8 final Element _element;
9 9
10 _AttributeClassSet(this._element); 10 _AttributeClassSet(this._element);
(...skipping 13 matching lines...) Expand all
24 } 24 }
25 return s; 25 return s;
26 } 26 }
27 27
28 void writeClasses(Set s) { 28 void writeClasses(Set s) {
29 _element.attributes['class'] = s.join(' '); 29 _element.attributes['class'] = s.join(' ');
30 } 30 }
31 } 31 }
32 32
33 $(ANNOTATIONS)class $CLASSNAME$EXTENDS$IMPLEMENTS$NATIVESPEC { 33 $(ANNOTATIONS)class $CLASSNAME$EXTENDS$IMPLEMENTS$NATIVESPEC {
34 static final _START_TAG_REGEXP = new RegExp('<(\\w+)');
35
34 factory $CLASSNAME.tag(String tag) => 36 factory $CLASSNAME.tag(String tag) =>
35 _$(CLASSNAME)FactoryProvider.create$(CLASSNAME)_tag(tag); 37 document.$dom_createElementNS("http://www.w3.org/2000/svg", tag);
36 factory $CLASSNAME.svg(String svg) => 38
37 _$(CLASSNAME)FactoryProvider.create$(CLASSNAME)_svg(svg); 39 factory SvgElement.svg(String svg,
40 {NodeValidator validator, NodeTreeSanitizer treeSanitizer}) {
41
42 if (validator == null && treeSanitizer == null) {
43 validator = new NodeValidatorBuilder.common()
44 ..allowSvg();
45 }
46
47 final match = _START_TAG_REGEXP.firstMatch(svg);
48 var parentElement;
49 if (match != null && match.group(1).toLowerCase() == 'svg') {
50 parentElement = document.body;
51 } else {
52 parentElement = new SvgSvgElement();
53 }
54 var fragment = parentElement.createFragment(svg, validator: validator,
55 treeSanitizer: treeSanitizer);
56 return fragment.nodes.where((e) => e is SvgElement).single;
57 }
38 58
39 _AttributeClassSet _cssClassSet; 59 _AttributeClassSet _cssClassSet;
40 CssClassSet get classes { 60 CssClassSet get classes {
41 if (_cssClassSet == null) { 61 if (_cssClassSet == null) {
42 _cssClassSet = new _AttributeClassSet(this); 62 _cssClassSet = new _AttributeClassSet(this);
43 } 63 }
44 return _cssClassSet; 64 return _cssClassSet;
45 } 65 }
46 66
47 List<Element> get children => new FilteredElementList<Element>(this); 67 List<Element> get children => new FilteredElementList<Element>(this);
(...skipping 11 matching lines...) Expand all
59 return container.innerHtml; 79 return container.innerHtml;
60 } 80 }
61 81
62 String get innerHtml { 82 String get innerHtml {
63 final container = new Element.tag("div"); 83 final container = new Element.tag("div");
64 final SvgElement cloned = this.clone(true); 84 final SvgElement cloned = this.clone(true);
65 container.children.addAll(cloned.children); 85 container.children.addAll(cloned.children);
66 return container.innerHtml; 86 return container.innerHtml;
67 } 87 }
68 88
69 void set innerHtml(String svg) { 89 DocumentFragment createFragment(String svg,
70 final container = new Element.tag("div"); 90 {NodeValidator validator, NodeTreeSanitizer treeSanitizer}) {
71 // Wrap the SVG string in <svg> so that SvgElements are created, rather than 91
72 // HTMLElements. 92 if (treeSanitizer == null) {
73 container.innerHtml = '<svg version="1.1">$svg</svg>'; 93 if (validator == null) {
74 this.children = container.children[0].children; 94 validator = new NodeValidatorBuilder.common()
95 ..allowSvg();
96 }
97 treeSanitizer = new NodeTreeSanitizer(validator);
98 }
99
100 // We create a fragment which will parse in the HTML parser
101 var html = '<svg version="1.1">$svg</svg>';
102 var fragment = document.body.createFragment(html,
103 treeSanitizer: treeSanitizer);
104
105 var svgFragment = new DocumentFragment();
106 // The root is the <svg/> element, need to pull out the contents.
107 var root = fragment.nodes.single;
108 while (root.$dom_firstChild != null) {
109 svgFragment.append(root.$dom_firstChild);
110 }
111 return svgFragment;
75 } 112 }
76 113
77 // Unsupported methods inherited from Element. 114 // Unsupported methods inherited from Element.
78 115
79 @DomName('Element.insertAdjacentText') 116 @DomName('Element.insertAdjacentText')
80 void insertAdjacentText(String where, String text) { 117 void insertAdjacentText(String where, String text) {
81 throw new UnsupportedError("Cannot invoke insertAdjacentText on SVG."); 118 throw new UnsupportedError("Cannot invoke insertAdjacentText on SVG.");
82 } 119 }
83 120
84 @DomName('Element.insertAdjacentHTML') 121 @DomName('Element.insertAdjacentHTML')
(...skipping 19 matching lines...) Expand all
104 * Checks to see if the SVG element type is supported by the current platform. 141 * Checks to see if the SVG element type is supported by the current platform.
105 * 142 *
106 * The tag should be a valid SVG element tag name. 143 * The tag should be a valid SVG element tag name.
107 */ 144 */
108 static bool isTagSupported(String tag) { 145 static bool isTagSupported(String tag) {
109 var e = new $CLASSNAME.tag(tag); 146 var e = new $CLASSNAME.tag(tag);
110 return e is $CLASSNAME && !(e is UnknownElement); 147 return e is $CLASSNAME && !(e is UnknownElement);
111 } 148 }
112 $!MEMBERS 149 $!MEMBERS
113 } 150 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698