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 |
| 4 import 'package:csslib/parser.dart' as css; |
| 5 import 'package:csslib/visitor.dart'; |
| 6 |
| 7 /** |
| 8 * Spin-up CSS parser in checked mode to detect any problematic CSS. Normally, |
| 9 * CSS will allow any property/value pairs regardless of validity; all of our |
| 10 * tests (by default) will ensure that the CSS is really valid. |
| 11 */ |
| 12 StyleSheet parseCss(String cssInput, {List errors, List opts}) => |
| 13 css.parse(cssInput, errors: errors, options: opts == null ? |
| 14 ['--no-colors', '--checked', '--warnings_as_errors', 'memory'] : opts); |
| 15 |
| 16 // Pretty printer for CSS. |
| 17 var emitCss = new CssPrinter(); |
| 18 String prettyPrint(StyleSheet ss) => |
| 19 (emitCss..visitTree(ss, pretty: true)).toString(); |
| 20 |
| 21 main() { |
| 22 var errors = []; |
| 23 |
| 24 // Parse a simple stylesheet. |
| 25 print('1. Good CSS, parsed CSS emitted:'); |
| 26 print(' ============================='); |
| 27 var stylesheet = parseCss( |
| 28 '@import "support/at-charset-019.css"; div { color: red; }' |
| 29 'button[type] { background-color: red; }' |
| 30 '.foo { ' |
| 31 'color: red; left: 20px; top: 20px; width: 100px; height:200px' |
| 32 '}' |
| 33 '#div {' |
| 34 'color : #00F578; border-color: #878787;' |
| 35 '}', errors: errors); |
| 36 |
| 37 if (!errors.isEmpty) { |
| 38 print("Got ${errors.length} errors.\n"); |
| 39 for (var error in errors) { |
| 40 print(error); |
| 41 } |
| 42 } else { |
| 43 print(prettyPrint(stylesheet)); |
| 44 } |
| 45 |
| 46 // Parse a stylesheet with errors |
| 47 print('2. Catch severe syntax errors:'); |
| 48 print(' ==========================='); |
| 49 var stylesheetError = parseCss( |
| 50 '.foo #%^&*asdf{ ' |
| 51 'color: red; left: 20px; top: 20px; width: 100px; height:200px' |
| 52 '}', errors: errors); |
| 53 |
| 54 if (!errors.isEmpty) { |
| 55 print("Got ${errors.length} errors.\n"); |
| 56 for (var error in errors) { |
| 57 print(error); |
| 58 } |
| 59 } else { |
| 60 print(stylesheetError.toString()); |
| 61 } |
| 62 |
| 63 // Parse a stylesheet that warns (checks) problematic CSS. |
| 64 print('3. Detect CSS problem with checking on:'); |
| 65 print(' ==================================='); |
| 66 stylesheetError = parseCss( '# div1 { color: red; }', errors: errors); |
| 67 |
| 68 if (!errors.isEmpty) { |
| 69 print("Detected ${errors.length} problem in checked mode.\n"); |
| 70 for (var error in errors) { |
| 71 print(error); |
| 72 } |
| 73 } else { |
| 74 print(stylesheetError.toString()); |
| 75 } |
| 76 |
| 77 // Parse a CSS selector. |
| 78 print('4. Parse a selector only:'); |
| 79 print(' ======================'); |
| 80 var selectorAst = css.selector('#div .foo', errors: errors); |
| 81 if (!errors.isEmpty) { |
| 82 print("Got ${errors.length} errors.\n"); |
| 83 for (var error in errors) { |
| 84 print(error); |
| 85 } |
| 86 } else { |
| 87 print(prettyPrint(selectorAst)); |
| 88 } |
| 89 |
| 90 } |
OLD | NEW |