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