| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 class Validate { | |
| 6 static int _classNameCheck(var selector, int matches) { | |
| 7 if (selector.isCombinatorDescendant() || | |
| 8 (selector.isCombinatorNone() && matches == 0)) { | |
| 9 if (matches < 0) { | |
| 10 String tooMany = selector.simpleSelector.toString(); | |
| 11 throw new CssSelectorException( | |
| 12 'Can not mix Id selector with class selector(s). Id ' + | |
| 13 'selector must be singleton too many starting at $tooMany'); | |
| 14 } | |
| 15 | |
| 16 return matches + 1; | |
| 17 } else { | |
| 18 String error = selector.toString(); | |
| 19 throw new CssSelectorException( | |
| 20 'Selectors can not have combinators (>, +, or ~) before $error'); | |
| 21 } | |
| 22 } | |
| 23 | |
| 24 static int _elementIdCheck(var selector, int matches) { | |
| 25 if (selector.isCombinatorNone() && matches == 0) { | |
| 26 // Perfect just one element id returns matches of -1. | |
| 27 return -1; | |
| 28 } else if (selector.isCombinatorDescendant()) { | |
| 29 String tooMany = selector.simpleSelector.toString(); | |
| 30 throw new CssSelectorException( | |
| 31 'Use of Id selector must be singleton starting at $tooMany'); | |
| 32 } else { | |
| 33 String error = selector.simpleSelector.toString(); | |
| 34 throw new CssSelectorException( | |
| 35 'Selectors can not have combinators (>, +, or ~) before $error'); | |
| 36 } | |
| 37 } | |
| 38 | |
| 39 // Validate the @{css expression} only .class and #elementId are valid inside | |
| 40 // of @{...}. | |
| 41 static template(List<ASTNode> selectors, CssWorld cssWorld) { | |
| 42 var errorSelector; // signal which selector didn't match. | |
| 43 bool found = false; // signal if a selector is matched. | |
| 44 int matches = 0; // < 0 IdSelectors, > 0 ClassSelector | |
| 45 | |
| 46 // At most one selector group (any number of simple selector sequences). | |
| 47 assert(selectors.length <= 1); | |
| 48 | |
| 49 for (final sels in selectors) { | |
| 50 for (final selector in sels.simpleSelectorSequences) { | |
| 51 found = false; | |
| 52 var simpleSelector = selector.simpleSelector; | |
| 53 if (simpleSelector is ClassSelector) { | |
| 54 // Any class name starting with an underscore is a private class name | |
| 55 // that doesn't have to match the world of known classes. | |
| 56 if (!simpleSelector.name.startsWith('_')) { | |
| 57 // TODO(terry): For now iterate through all classes look for faster | |
| 58 // mechanism hash map, etc. | |
| 59 for (final className in cssWorld.classes) { | |
| 60 if (selector.simpleSelector.name == className) { | |
| 61 matches = _classNameCheck(selector, matches); | |
| 62 found = true; // .class found. | |
| 63 break; | |
| 64 } | |
| 65 for (final className2 in cssWorld.classes) { | |
| 66 print(className2); | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 } else { | |
| 71 // Don't check any class name that is prefixed with an underscore. | |
| 72 // However, signal as found and bump up matches; it's a valid class | |
| 73 // name. | |
| 74 matches = _classNameCheck(selector, matches); | |
| 75 found = true; // ._class are always okay. | |
| 76 } | |
| 77 } else if (simpleSelector is IdSelector) { | |
| 78 // Any element id starting with an underscore is a private element id | |
| 79 // that doesn't have to match the world of known elemtn ids. | |
| 80 if (!simpleSelector.name.startsWith('_')) { | |
| 81 for (final id in cssWorld.ids) { | |
| 82 if (simpleSelector.name == id) { | |
| 83 matches = _elementIdCheck(selector, matches); | |
| 84 found = true; // #id found. | |
| 85 break; | |
| 86 } | |
| 87 } | |
| 88 } else { | |
| 89 // Don't check any element ID that is prefixed with an underscore. | |
| 90 // Signal as found and bump up matches; it's a valid element ID. | |
| 91 matches = _elementIdCheck(selector, matches); | |
| 92 found = true; // #_id are always okay | |
| 93 } | |
| 94 } else { | |
| 95 String badSelector = simpleSelector.toString(); | |
| 96 throw new CssSelectorException( | |
| 97 'Invalid template selector $badSelector'); | |
| 98 } | |
| 99 | |
| 100 if (!found) { | |
| 101 String unknownName = simpleSelector.toString(); | |
| 102 throw new CssSelectorException('Unknown selector name $unknownName'); | |
| 103 } | |
| 104 } | |
| 105 } | |
| 106 | |
| 107 // Every selector must match. | |
| 108 var selector = selectors[0]; | |
| 109 assert((matches >= 0 ? matches : -matches) == | |
| 110 selector.simpleSelectorSequences.length); | |
| 111 } | |
| 112 } | |
| 113 | |
| OLD | NEW |