| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
| 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. | |
| 4 | |
| 5 class _XMLClassSet extends _CssClassSet { | |
| 6 _XMLClassSet(element) : super(element); | |
| 7 | |
| 8 String _className() { | |
| 9 final classStr = _element.getAttribute('class'); | |
| 10 return classStr == null ? '' : classStr; | |
| 11 } | |
| 12 | |
| 13 void _write(Set s) => _element.setAttribute('class', _formatSet(s)); | |
| 14 } | |
| 15 | |
| 16 class XMLElementWrappingImplementation extends ElementWrappingImplementation | |
| 17 implements XMLElement { | |
| 18 XMLElementWrappingImplementation._wrap(ptr) : super._wrap(ptr); | |
| 19 | |
| 20 factory XMLElementWrappingImplementation.tag(String tag) => | |
| 21 LevelDom.wrapElement(dom.document.createElementNS(null, tag)); | |
| 22 | |
| 23 factory XMLElementWrappingImplementation.xml(String xml) { | |
| 24 XMLElement parentTag = new XMLElement.tag('xml'); | |
| 25 parentTag.innerHTML = xml; | |
| 26 if (parentTag.nodes.length == 1) return parentTag.nodes.removeLast(); | |
| 27 | |
| 28 throw new IllegalArgumentException( | |
| 29 'XML had ${parentTag.nodes.length} top-level nodes but 1 expected'); | |
| 30 } | |
| 31 | |
| 32 CSSClassSet get classes() { | |
| 33 if (_cssClassSet === null) { | |
| 34 _cssClassSet = new _XMLClassSet(_ptr); | |
| 35 } | |
| 36 return _cssClassSet; | |
| 37 } | |
| 38 | |
| 39 ElementList get elements() { | |
| 40 if (_elements == null) { | |
| 41 _elements = new FilteredElementList(this); | |
| 42 } | |
| 43 return _elements; | |
| 44 } | |
| 45 | |
| 46 void set elements(Collection<Element> value) { | |
| 47 final elements = this.elements; | |
| 48 elements.clear(); | |
| 49 elements.addAll(value); | |
| 50 } | |
| 51 | |
| 52 String get outerHTML() { | |
| 53 final container = new Element.tag("div"); | |
| 54 // Safari requires that the clone be removed from its owner document before | |
| 55 // being inserted into the HTML document. | |
| 56 container.elements.add(this.clone(true).remove()); | |
| 57 return container.innerHTML; | |
| 58 } | |
| 59 | |
| 60 String get innerHTML() { | |
| 61 final container = new Element.tag("div"); | |
| 62 // Safari requires that the clone be removed from its owner document before | |
| 63 // being inserted into the HTML document. | |
| 64 container.nodes.addAll(this.clone(true).remove().nodes); | |
| 65 return container.innerHTML; | |
| 66 } | |
| 67 | |
| 68 void set innerHTML(String xml) { | |
| 69 final xmlDoc = new XMLDocument.xml('<xml>$xml</xml>'); | |
| 70 // Safari requires that the root node be removed from the document before | |
| 71 // being inserted into the HTML document. | |
| 72 this.nodes = xmlDoc.remove().nodes; | |
| 73 } | |
| 74 | |
| 75 Node _insertAdjacentNode(String where, Node node) { | |
| 76 switch (where.toLowerCase()) { | |
| 77 case "beforebegin": | |
| 78 if (parent == null) return null; | |
| 79 parent.insertBefore(node, this); | |
| 80 return node; | |
| 81 case "afterend": | |
| 82 if (parent == null) return null; | |
| 83 if (nextNode == null) { | |
| 84 parent.nodes.add(node); | |
| 85 } else { | |
| 86 parent.insertBefore(node, nextNode); | |
| 87 } | |
| 88 return node; | |
| 89 case "afterbegin": | |
| 90 this.insertBefore(node, nodes.first); | |
| 91 return node; | |
| 92 case "beforeend": | |
| 93 this.nodes.add(node); | |
| 94 return node; | |
| 95 default: | |
| 96 throw new IllegalArgumentException("Invalid position ${where}"); | |
| 97 } | |
| 98 } | |
| 99 | |
| 100 XMLElement insertAdjacentElement([String where = null, | |
| 101 XMLElement element = null]) => this._insertAdjacentNode(where, element); | |
| 102 | |
| 103 void insertAdjacentText([String where = null, String text = null]) { | |
| 104 this._insertAdjacentNode(where, new Text(text)); | |
| 105 } | |
| 106 | |
| 107 void insertAdjacentHTML( | |
| 108 [String position_OR_where = null, String text = null]) { | |
| 109 this._insertAdjacentNode( | |
| 110 position_OR_where, new DocumentFragment.xml(text)); | |
| 111 } | |
| 112 | |
| 113 Future<ElementRect> get rect() { | |
| 114 return _createMeasurementFuture(() => const EmptyElementRect(), | |
| 115 new Completer<ElementRect>()); | |
| 116 } | |
| 117 | |
| 118 // For HTML elemens, the default value of "contentEditable" is "inherit", so | |
| 119 // we'll use that here as well even though it doesn't really make sense. | |
| 120 String get contentEditable() => _attr('contentEditable', 'inherit'); | |
| 121 | |
| 122 void set contentEditable(String value) { | |
| 123 attributes['contentEditable'] = value; | |
| 124 } | |
| 125 | |
| 126 void blur() {} | |
| 127 void focus() {} | |
| 128 void scrollByLines([int lines = null]) {} | |
| 129 void scrollByPages([int pages = null]) {} | |
| 130 void scrollIntoView([bool centerIfNeeded = null]) {} | |
| 131 | |
| 132 // Parentless HTML elements return false regardless of the value of their | |
| 133 // contentEditable attribute, so XML elements do the same since they're never | |
| 134 // actually editable. | |
| 135 bool get isContentEditable() => false; | |
| 136 | |
| 137 bool get draggable() => attributes['draggable'] == 'true'; | |
| 138 | |
| 139 void set draggable(bool value) { attributes['draggable'] = value.toString(); } | |
| 140 | |
| 141 bool get spellcheck() => attributes['spellcheck'] == 'true'; | |
| 142 | |
| 143 void set spellcheck(bool value) { | |
| 144 attributes['spellcheck'] = value.toString(); | |
| 145 } | |
| 146 | |
| 147 bool get hidden() => attributes.containsKey('hidden'); | |
| 148 | |
| 149 void set hidden(bool value) { | |
| 150 if (value) { | |
| 151 attributes['hidden'] = ''; | |
| 152 } else { | |
| 153 attributes.remove('hidden'); | |
| 154 } | |
| 155 } | |
| 156 | |
| 157 int get tabIndex() { | |
| 158 try { | |
| 159 return Math.parseInt(_attr('tabIndex')); | |
| 160 } on FormatException catch (e) { | |
| 161 return 0; | |
| 162 } | |
| 163 } | |
| 164 | |
| 165 void set tabIndex(int value) { attributes['tabIndex'] = value.toString(); } | |
| 166 | |
| 167 String get id() => _attr('id'); | |
| 168 | |
| 169 void set id(String value) { attributes['id'] = value; } | |
| 170 | |
| 171 String get title() => _attr('title'); | |
| 172 | |
| 173 void set title(String value) { attributes['title'] = value; } | |
| 174 | |
| 175 String get webkitdropzone() => _attr('webkitdropzone'); | |
| 176 | |
| 177 void set webkitdropzone(String value) { | |
| 178 attributes['webkitdropzone'] = value; | |
| 179 } | |
| 180 | |
| 181 String get lang() => _attr('lang'); | |
| 182 | |
| 183 void set lang(String value) { attributes['lang'] = value; } | |
| 184 | |
| 185 String get dir() => _attr('dir'); | |
| 186 | |
| 187 void set dir(String value) { attributes['dir'] = value; } | |
| 188 | |
| 189 String _attr(String name, [String def = '']) => | |
| 190 attributes.containsKey(name) ? attributes[name] : def; | |
| 191 } | |
| OLD | NEW |