| OLD | NEW |
| 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 Loading... |
| 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_MODIFIERS)class $CLASSNAME$EXTENDS$IMPLEMENTS$NATIVESPEC { | 33 $(ANNOTATIONS)$(CLASS_MODIFIERS)class $CLASSNAME$EXTENDS$IMPLEMENTS$NATIVESPEC { |
| 34 static final _START_TAG_REGEXP = new RegExp('<(\\w+)'); | |
| 35 | |
| 36 factory $CLASSNAME.tag(String tag) => | 34 factory $CLASSNAME.tag(String tag) => |
| 37 document.$dom_createElementNS("http://www.w3.org/2000/svg", tag); | 35 _$(CLASSNAME)FactoryProvider.create$(CLASSNAME)_tag(tag); |
| 38 factory $CLASSNAME.svg(String svg, | 36 factory $CLASSNAME.svg(String svg) => |
| 39 {NodeValidator validator, NodeTreeSanitizer treeSanitizer}) { | 37 _$(CLASSNAME)FactoryProvider.create$(CLASSNAME)_svg(svg); |
| 40 | |
| 41 if (validator == null && treeSanitizer == null) { | |
| 42 validator = new NodeValidatorBuilder.common()..allowSvg(); | |
| 43 } | |
| 44 | |
| 45 final match = _START_TAG_REGEXP.firstMatch(svg); | |
| 46 var parentElement; | |
| 47 if (match != null && match.group(1).toLowerCase() == 'svg') { | |
| 48 parentElement = document.body; | |
| 49 } else { | |
| 50 parentElement = new SvgSvgElement(); | |
| 51 } | |
| 52 var fragment = parentElement.createFragment(svg, validator: validator, | |
| 53 treeSanitizer: treeSanitizer); | |
| 54 return fragment.nodes.where((e) => e is SvgElement).single; | |
| 55 } | |
| 56 | 38 |
| 57 _AttributeClassSet _cssClassSet; | 39 _AttributeClassSet _cssClassSet; |
| 58 CssClassSet get classes { | 40 CssClassSet get classes { |
| 59 if (_cssClassSet == null) { | 41 if (_cssClassSet == null) { |
| 60 _cssClassSet = new _AttributeClassSet(this); | 42 _cssClassSet = new _AttributeClassSet(this); |
| 61 } | 43 } |
| 62 return _cssClassSet; | 44 return _cssClassSet; |
| 63 } | 45 } |
| 64 | 46 |
| 65 List<Element> get children => new FilteredElementList<Element>(this); | 47 List<Element> get children => new FilteredElementList<Element>(this); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 77 return container.innerHtml; | 59 return container.innerHtml; |
| 78 } | 60 } |
| 79 | 61 |
| 80 String get innerHtml { | 62 String get innerHtml { |
| 81 final container = new Element.tag("div"); | 63 final container = new Element.tag("div"); |
| 82 final SvgElement cloned = this.clone(true); | 64 final SvgElement cloned = this.clone(true); |
| 83 container.children.addAll(cloned.children); | 65 container.children.addAll(cloned.children); |
| 84 return container.innerHtml; | 66 return container.innerHtml; |
| 85 } | 67 } |
| 86 | 68 |
| 87 void set innerHtml(String value) { | 69 void set innerHtml(String svg) { |
| 88 this.setInnerHtml(value); | 70 final container = new Element.tag("div"); |
| 89 } | 71 // Wrap the SVG string in <svg> so that SvgElements are created, rather than |
| 90 | 72 // HTMLElements. |
| 91 DocumentFragment createFragment(String svg, | 73 container.innerHtml = '<svg version="1.1">$svg</svg>'; |
| 92 {NodeValidator validator, NodeTreeSanitizer treeSanitizer}) { | 74 this.children = container.children[0].children; |
| 93 | |
| 94 if (treeSanitizer == null) { | |
| 95 if (validator == null) { | |
| 96 validator = new NodeValidatorBuilder.common() | |
| 97 ..allowSvg(); | |
| 98 } | |
| 99 treeSanitizer = new NodeTreeSanitizer(validator); | |
| 100 } | |
| 101 | |
| 102 // We create a fragment which will parse in the HTML parser | |
| 103 var html = '<svg version="1.1">$svg</svg>'; | |
| 104 var fragment = document.body.createFragment(html, | |
| 105 treeSanitizer: treeSanitizer); | |
| 106 | |
| 107 var svgFragment = new DocumentFragment(); | |
| 108 // The root is the <svg/> element, need to pull out the contents. | |
| 109 var root = fragment.nodes.single; | |
| 110 while (root.firstChild != null) { | |
| 111 svgFragment.append(root.firstChild); | |
| 112 } | |
| 113 return svgFragment; | |
| 114 } | 75 } |
| 115 | 76 |
| 116 // Unsupported methods inherited from Element. | 77 // Unsupported methods inherited from Element. |
| 117 | 78 |
| 118 @DomName('Element.insertAdjacentText') | 79 @DomName('Element.insertAdjacentText') |
| 119 void insertAdjacentText(String where, String text) { | 80 void insertAdjacentText(String where, String text) { |
| 120 throw new UnsupportedError("Cannot invoke insertAdjacentText on SVG."); | 81 throw new UnsupportedError("Cannot invoke insertAdjacentText on SVG."); |
| 121 } | 82 } |
| 122 | 83 |
| 123 @DomName('Element.insertAdjacentHTML') | 84 @DomName('Element.insertAdjacentHTML') |
| (...skipping 19 matching lines...) Expand all Loading... |
| 143 * Checks to see if the SVG element type is supported by the current platform. | 104 * Checks to see if the SVG element type is supported by the current platform. |
| 144 * | 105 * |
| 145 * The tag should be a valid SVG element tag name. | 106 * The tag should be a valid SVG element tag name. |
| 146 */ | 107 */ |
| 147 static bool isTagSupported(String tag) { | 108 static bool isTagSupported(String tag) { |
| 148 var e = new $CLASSNAME.tag(tag); | 109 var e = new $CLASSNAME.tag(tag); |
| 149 return e is $CLASSNAME && !(e is UnknownElement); | 110 return e is $CLASSNAME && !(e is UnknownElement); |
| 150 } | 111 } |
| 151 $!MEMBERS | 112 $!MEMBERS |
| 152 } | 113 } |
| OLD | NEW |