| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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 /** | 7 /** |
| 8 * The base class for all documents. | 8 * The base class for all documents. |
| 9 * | 9 * |
| 10 * Each web page loaded in the browser has its own [Document] object, which is | 10 * Each web page loaded in the browser has its own [Document] object, which is |
| 11 * typically an [HtmlDocument]. | 11 * typically an [HtmlDocument]. |
| 12 * | 12 * |
| 13 * If you aren't comfortable with DOM concepts, see the Dart tutorial | 13 * If you aren't comfortable with DOM concepts, see the Dart tutorial |
| 14 * [Target 2: Connect Dart & HTML](http://www.dartlang.org/docs/tutorials/connec
t-dart-html/). | 14 * [Target 2: Connect Dart & HTML](http://www.dartlang.org/docs/tutorials/connec
t-dart-html/). |
| 15 */ | 15 */ |
| 16 $(ANNOTATIONS)class $CLASSNAME extends Node $NATIVESPEC | 16 $(ANNOTATIONS)class $CLASSNAME extends Node $NATIVESPEC |
| 17 { | 17 { |
| 18 | 18 |
| 19 $!MEMBERS | 19 $!MEMBERS |
| 20 | 20 |
| 21 /** | 21 /** |
| 22 * Finds the first descendant element of this document that matches the | |
| 23 * specified group of selectors. | |
| 24 * | |
| 25 * Unless your webpage contains multiple documents, the top-level query | |
| 26 * method behaves the same as this method, so you should use it instead to | |
| 27 * save typing a few characters. | |
| 28 * | |
| 29 * [selectors] should be a string using CSS selector syntax. | |
| 30 * var element1 = document.query('.className'); | |
| 31 * var element2 = document.query('#id'); | |
| 32 * | |
| 33 * For details about CSS selector syntax, see the | |
| 34 * [CSS selector specification](http://www.w3.org/TR/css3-selectors/). | |
| 35 */ | |
| 36 Element query(String selectors) { | |
| 37 // It is fine for our RegExp to detect element id query selectors to have | |
| 38 // false negatives but not false positives. | |
| 39 if (new RegExp("^#[_a-zA-Z]\\w*\$").hasMatch(selectors)) { | |
| 40 return $dom_getElementById(selectors.substring(1)); | |
| 41 } | |
| 42 return $dom_querySelector(selectors); | |
| 43 } | |
| 44 | |
| 45 /** | |
| 46 * Finds all descendant elements of this document that match the specified | 22 * Finds all descendant elements of this document that match the specified |
| 47 * group of selectors. | 23 * group of selectors. |
| 48 * | 24 * |
| 49 * Unless your webpage contains multiple documents, the top-level queryAll | 25 * Unless your webpage contains multiple documents, the top-level queryAll |
| 50 * method behaves the same as this method, so you should use it instead to | 26 * method behaves the same as this method, so you should use it instead to |
| 51 * save typing a few characters. | 27 * save typing a few characters. |
| 52 * | 28 * |
| 53 * [selectors] should be a string using CSS selector syntax. | 29 * [selectors] should be a string using CSS selector syntax. |
| 54 * var items = document.queryAll('.itemClassName'); | 30 * var items = document.queryAll('.itemClassName'); |
| 55 * | 31 * |
| 56 * For details about CSS selector syntax, see the | 32 * For details about CSS selector syntax, see the |
| 57 * [CSS selector specification](http://www.w3.org/TR/css3-selectors/). | 33 * [CSS selector specification](http://www.w3.org/TR/css3-selectors/). |
| 58 */ | 34 */ |
| 59 List<Element> queryAll(String selectors) { | 35 List<Element> queryAll(String selectors) { |
| 60 if (new RegExp("""^\\[name=["'][^'"]+['"]\\]\$""").hasMatch(selectors)) { | 36 return new _FrozenElementList._wrap($dom_querySelectorAll(selectors)); |
| 61 final mutableMatches = $dom_getElementsByName( | |
| 62 selectors.substring(7,selectors.length - 2)); | |
| 63 int len = mutableMatches.length; | |
| 64 final copyOfMatches = new List<Element>(len); | |
| 65 for (int i = 0; i < len; ++i) { | |
| 66 copyOfMatches[i] = mutableMatches[i]; | |
| 67 } | |
| 68 return new _FrozenElementList._wrap(copyOfMatches); | |
| 69 } else if (new RegExp("^[*a-zA-Z0-9]+\$").hasMatch(selectors)) { | |
| 70 final mutableMatches = $dom_getElementsByTagName(selectors); | |
| 71 int len = mutableMatches.length; | |
| 72 final copyOfMatches = new List<Element>(len); | |
| 73 for (int i = 0; i < len; ++i) { | |
| 74 copyOfMatches[i] = mutableMatches[i]; | |
| 75 } | |
| 76 return new _FrozenElementList._wrap(copyOfMatches); | |
| 77 } else { | |
| 78 return new _FrozenElementList._wrap($dom_querySelectorAll(selectors)); | |
| 79 } | |
| 80 } | 37 } |
| 81 } | 38 } |
| OLD | NEW |