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('XML had ${parentTag.nodes.length} ' + |
| 29 'top-level nodes but 1 expected'); |
| 30 } |
| 31 |
| 32 Set<String> 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 // TODO: The type of value should be Collection<Element>. See http://b/5392897 |
| 47 void set elements(value) { |
| 48 final elements = this.elements; |
| 49 elements.clear(); |
| 50 elements.addAll(value); |
| 51 } |
| 52 |
| 53 String get outerHTML() { |
| 54 final container = new Element.tag("div"); |
| 55 container.elements.add(this.clone(true)); |
| 56 return container.innerHTML; |
| 57 } |
| 58 |
| 59 String get innerHTML() { |
| 60 final container = new Element.tag("div"); |
| 61 container.nodes.addAll(this.clone(true).nodes); |
| 62 return container.innerHTML; |
| 63 } |
| 64 |
| 65 void set innerHTML(String xml) { |
| 66 final xmlDoc = new XMLDocument.xml('<xml>$xml</xml>'); |
| 67 this.nodes = xmlDoc.nodes; |
| 68 } |
| 69 |
| 70 Node _insertAdjacentNode(String where, Node node) { |
| 71 switch (where.toLowerCase()) { |
| 72 case "beforebegin": |
| 73 if (parent == null) return null; |
| 74 parent.insertBefore(node, this); |
| 75 return node; |
| 76 case "afterend": |
| 77 if (parent == null) return null; |
| 78 if (nextNode == null) { |
| 79 parent.nodes.add(node); |
| 80 } else { |
| 81 parent.insertBefore(node, nextNode); |
| 82 } |
| 83 return node; |
| 84 case "afterbegin": |
| 85 this.insertBefore(node, nodes.first); |
| 86 return node; |
| 87 case "beforeend": |
| 88 this.nodes.add(node); |
| 89 return node; |
| 90 default: |
| 91 throw new IllegalArgumentException("Invalid position ${where}"); |
| 92 } |
| 93 } |
| 94 |
| 95 XMLElement insertAdjacentElement([String where = null, |
| 96 XMLElement element = null]) => this._insertAdjacentNode(where, element); |
| 97 |
| 98 void insertAdjacentText([String where = null, String text = null]) { |
| 99 this._insertAdjacentNode(where, new Text(text)); |
| 100 } |
| 101 |
| 102 void insertAdjacentHTML( |
| 103 [String position_OR_where = null, String text = null]) { |
| 104 this._insertAdjacentNode( |
| 105 position_OR_where, new DocumentFragment.xml(text)); |
| 106 } |
| 107 |
| 108 Future<ElementRect> get rect() { |
| 109 return _createMeasurementFuture(() => const EmptyElementRect(), |
| 110 new Completer<ElementRect>()); |
| 111 } |
| 112 |
| 113 // For HTML elemens, the default value of "contentEditable" is "inherit", so |
| 114 // we'll use that here as well even though it doesn't really make sense. |
| 115 String get contentEditable() => _attr('contentEditable', 'inherit'); |
| 116 |
| 117 void set contentEditable(String value) { |
| 118 attributes['contentEditable'] = value; |
| 119 } |
| 120 |
| 121 void blur() {} |
| 122 void focus() {} |
| 123 void scrollByLines([int lines = null]) {} |
| 124 void scrollByPages([int pages = null]) {} |
| 125 void scrollIntoView([bool centerIfNeeded = null]) {} |
| 126 |
| 127 // Parentless HTML elements return false regardless of the value of their |
| 128 // contentEditable attribute, so XML elements do the same since they're never |
| 129 // actually editable. |
| 130 bool get isContentEditable() => false; |
| 131 |
| 132 bool get draggable() => attributes['draggable'] == 'true'; |
| 133 |
| 134 void set draggable(bool value) { attributes['draggable'] = value.toString(); } |
| 135 |
| 136 bool get spellcheck() => attributes['spellcheck'] == 'true'; |
| 137 |
| 138 void set spellcheck(bool value) { |
| 139 attributes['spellcheck'] = value.toString(); |
| 140 } |
| 141 |
| 142 bool get hidden() => attributes.containsKey('hidden'); |
| 143 |
| 144 void set hidden(bool value) { |
| 145 if (value) { |
| 146 attributes['hidden'] = ''; |
| 147 } else { |
| 148 attributes.remove('hidden'); |
| 149 } |
| 150 } |
| 151 |
| 152 int get tabIndex() { |
| 153 try { |
| 154 return Math.parseInt(_attr('tabIndex')); |
| 155 } catch (BadNumberFormatException e) { |
| 156 return 0; |
| 157 } |
| 158 } |
| 159 |
| 160 void set tabIndex(int value) { attributes['tabIndex'] = value.toString(); } |
| 161 |
| 162 String get id() => _attr('id'); |
| 163 |
| 164 void set id(String value) { attributes['id'] = value; } |
| 165 |
| 166 String get title() => _attr('title'); |
| 167 |
| 168 void set title(String value) { attributes['title'] = value; } |
| 169 |
| 170 String get webkitdropzone() => _attr('webkitdropzone'); |
| 171 |
| 172 void set webkitdropzone(String value) { |
| 173 attributes['webkitdropzone'] = value; |
| 174 } |
| 175 |
| 176 String get lang() => _attr('lang'); |
| 177 |
| 178 void set lang(String value) { attributes['lang'] = value; } |
| 179 |
| 180 String get dir() => _attr('dir'); |
| 181 |
| 182 void set dir(String value) { attributes['dir'] = value; } |
| 183 |
| 184 String _attr(String name, [String def = '']) => |
| 185 attributes.containsKey(name) ? attributes[name] : def; |
| 186 } |
OLD | NEW |