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