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