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