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