OLD | NEW |
---|---|
1 /** | 1 /** |
2 * A simple tree API that results from parsing html. Intended to be compatible | 2 * A simple tree API that results from parsing html. Intended to be compatible |
3 * with dart:html, but right now it resembles the classic JS DOM. | 3 * with dart:html, but right now it resembles the classic JS DOM. |
4 */ | 4 */ |
5 library dom; | 5 library dom; |
6 | 6 |
7 import 'dart:collection'; | 7 import 'dart:collection'; |
8 import 'package:source_maps/span.dart' show FileSpan; | 8 import 'package:source_maps/span.dart' show FileSpan; |
9 | 9 |
10 import 'src/constants.dart'; | 10 import 'src/constants.dart'; |
11 import 'src/list_proxy.dart'; | 11 import 'src/list_proxy.dart'; |
12 import 'src/token.dart'; | 12 import 'src/token.dart'; |
13 import 'src/tokenizer.dart'; | 13 import 'src/tokenizer.dart'; |
14 import 'src/treebuilder.dart'; | |
15 import 'src/utils.dart'; | 14 import 'src/utils.dart'; |
16 import 'dom_parsing.dart'; | 15 import 'dom_parsing.dart'; |
17 import 'parser.dart'; | 16 import 'parser.dart'; |
18 | 17 |
19 // TODO(jmesserly): this needs to be replaced by an AttributeMap for attributes | 18 // TODO(jmesserly): this needs to be replaced by an AttributeMap for attributes |
20 // that exposes namespace info. | 19 // that exposes namespace info. |
21 class AttributeName implements Comparable { | 20 class AttributeName implements Comparable { |
22 /** The namespace prefix, e.g. `xlink`. */ | 21 /** The namespace prefix, e.g. `xlink`. */ |
23 final String prefix; | 22 final String prefix; |
24 | 23 |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
234 /** | 233 /** |
235 * Move all the children of the current node to [newParent]. | 234 * Move all the children of the current node to [newParent]. |
236 * This is needed so that trees that don't store text as nodes move the | 235 * This is needed so that trees that don't store text as nodes move the |
237 * text in the correct way. | 236 * text in the correct way. |
238 */ | 237 */ |
239 void reparentChildren(Node newParent) { | 238 void reparentChildren(Node newParent) { |
240 newParent.nodes.addAll(nodes); | 239 newParent.nodes.addAll(nodes); |
241 nodes.clear(); | 240 nodes.clear(); |
242 } | 241 } |
243 | 242 |
243 /** *Deprecated* use [querySelector] instead. */ | |
244 @deprecated | |
245 Element query(String selectors) => querySelector(selectors); | |
246 | |
247 /** *Deprecated* use [querySelectorAll] instead. */ | |
248 @deprecated | |
249 List<Element> queryAll(String selectors) => querySelectorAll(selectors); | |
250 | |
244 /** | 251 /** |
245 * Seaches for the first descendant node matching the given selectors, using a | 252 * Seaches for the first descendant node matching the given selectors, using a |
246 * preorder traversal. NOTE: right now, this supports only a single type | 253 * preorder traversal. NOTE: right now, this supports only a single type |
247 * selectors, e.g. `node.query('div')`. | 254 * selectors, e.g. `node.query('div')`. |
248 */ | 255 */ |
249 Element query(String selectors) => _queryType(_typeSelector(selectors)); | 256 |
257 Element querySelector(String selectors) => | |
Jennifer Messerly
2014/02/08 01:13:29
I used the new names accidentally, so I figured I'
| |
258 _queryType(_typeSelector(selectors)); | |
250 | 259 |
251 /** | 260 /** |
252 * Returns all descendant nodes matching the given selectors, using a | 261 * Returns all descendant nodes matching the given selectors, using a |
253 * preorder traversal. NOTE: right now, this supports only a single type | 262 * preorder traversal. NOTE: right now, this supports only a single type |
254 * selectors, e.g. `node.queryAll('div')`. | 263 * selectors, e.g. `node.queryAll('div')`. |
255 */ | 264 */ |
256 List<Element> queryAll(String selectors) { | 265 List<Element> querySelectorAll(String selectors) { |
257 var results = new List<Element>(); | 266 var results = new List<Element>(); |
258 _queryAllType(_typeSelector(selectors), results); | 267 _queryAllType(_typeSelector(selectors), results); |
259 return results; | 268 return results; |
260 } | 269 } |
261 | 270 |
262 bool hasChildNodes() => !nodes.isEmpty; | 271 bool hasChildNodes() => !nodes.isEmpty; |
263 | 272 |
264 bool contains(Node node) => nodes.contains(node); | 273 bool contains(Node node) => nodes.contains(node); |
265 | 274 |
266 String _typeSelector(String selectors) { | 275 String _typeSelector(String selectors) { |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
360 } | 369 } |
361 } | 370 } |
362 | 371 |
363 class Document extends Node { | 372 class Document extends Node { |
364 Document() : super(null); | 373 Document() : super(null); |
365 factory Document.html(String html) => parse(html); | 374 factory Document.html(String html) => parse(html); |
366 | 375 |
367 int get nodeType => Node.DOCUMENT_NODE; | 376 int get nodeType => Node.DOCUMENT_NODE; |
368 | 377 |
369 // TODO(jmesserly): optmize this if needed | 378 // TODO(jmesserly): optmize this if needed |
370 Element get head => query('html').query('head'); | 379 Element get documentElement => querySelector('html'); |
371 Element get body => query('html').query('body'); | 380 Element get head => documentElement.querySelector('head'); |
381 Element get body => documentElement.querySelector('body'); | |
372 | 382 |
373 String toString() => "#document"; | 383 String toString() => "#document"; |
374 | 384 |
375 void _addOuterHtml(StringBuffer str) => _addInnerHtml(str); | 385 void _addOuterHtml(StringBuffer str) => _addInnerHtml(str); |
376 | 386 |
377 Document clone() => new Document(); | 387 Document clone() => new Document(); |
378 } | 388 } |
379 | 389 |
380 class DocumentFragment extends Document { | 390 class DocumentFragment extends Document { |
381 DocumentFragment(); | 391 DocumentFragment(); |
(...skipping 482 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
864 if (start == null) start = length - 1; | 874 if (start == null) start = length - 1; |
865 return _filtered.lastIndexOf(element, start); | 875 return _filtered.lastIndexOf(element, start); |
866 } | 876 } |
867 | 877 |
868 Element get first => _filtered.first; | 878 Element get first => _filtered.first; |
869 | 879 |
870 Element get last => _filtered.last; | 880 Element get last => _filtered.last; |
871 | 881 |
872 Element get single => _filtered.single; | 882 Element get single => _filtered.single; |
873 } | 883 } |
OLD | NEW |