OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 //#import('../file_system.dart'); | 5 //#import('../file_system.dart'); |
6 | 6 |
7 #import("dart:dom"); | 7 #import("dart:dom"); |
8 #import('css.dart'); | 8 #import('css.dart'); |
9 #import('../../frog/lang.dart', prefix:'lang'); | 9 #import('../../frog/lang.dart', prefix:'lang'); |
10 | 10 #import('../../frog/file_system_memory.dart'); |
11 | 11 |
12 void runCss([bool debug = false, bool parseOnly = false]) { | 12 void runCss([bool debug = false, bool parseOnly = false]) { |
13 final HTMLTextAreaElement classes = document.getElementById('classes'); | 13 final HTMLTextAreaElement classes = document.getElementById('classes'); |
14 final HTMLTextAreaElement expression = document.getElementById('expression'); | 14 final HTMLTextAreaElement expression = document.getElementById('expression'); |
15 final HTMLDivElement result = document.getElementById('result'); | 15 final HTMLDivElement result = document.getElementById('result'); |
16 | 16 |
17 List<String> knownWorld = classes.value.split("\n"); | 17 List<String> knownWorld = classes.value.split("\n"); |
18 List<String> knownClasses = []; | 18 List<String> knownClasses = []; |
19 List<String> knownIds = []; | 19 List<String> knownIds = []; |
20 for (name in knownWorld) { | 20 for (var name in knownWorld) { |
21 if (name.startsWith('.')) { | 21 if (name.startsWith('.')) { |
22 knownClasses.add(name.substring(1)); | 22 knownClasses.add(name.substring(1)); |
23 } else if (name.startsWith('#')) { | 23 } else if (name.startsWith('#')) { |
24 knownIds.add(name.substring(1)); | 24 knownIds.add(name.substring(1)); |
25 } | 25 } |
26 } | 26 } |
27 | 27 |
28 CssWorld cssWorld = new CssWorld(knownClasses, knownIds); | 28 CssWorld cssWorld = new CssWorld(knownClasses, knownIds); |
29 bool templateValid = true; | 29 bool templateValid = true; |
30 String dumpTree = ""; | 30 String dumpTree = ""; |
31 | 31 |
32 String cssExpr = expression.value; | 32 String cssExpr = expression.value; |
33 if (!debug) { | 33 if (!debug) { |
34 try { | 34 try { |
35 cssParseAndValidate(cssExpr, cssWorld); | 35 cssParseAndValidate(cssExpr, cssWorld); |
36 } catch (var cssException) { | 36 } catch (var cssException) { |
37 templateValid = false; | 37 templateValid = false; |
38 dumpTree = cssException.toString(); | 38 dumpTree = cssException.toString(); |
39 } | 39 } |
40 } else if (parseOnly) { | 40 } else if (parseOnly) { |
41 try { | 41 try { |
42 Parser parser = new Parser(new lang.SourceFile( | 42 Parser parser = new Parser(new lang.SourceFile( |
43 lang.SourceFile.IN_MEMORY_FILE, cssExpr)); | 43 lang.SourceFile.IN_MEMORY_FILE, cssExpr)); |
44 List<SelectorGroup> groups = parser.preprocess(); | 44 Stylesheet stylesheet = parser.parse(); |
45 StringBuffer groupTree = new StringBuffer(); | 45 StringBuffer stylesheetTree = new StringBuffer(); |
46 for (group in groups) { | 46 String prettyStylesheet = stylesheet.toString(); |
47 String prettySelector = group.toString(); | 47 stylesheetTree.add("${prettyStylesheet}\n"); |
48 groupTree.add("${prettySelector}\n"); | 48 stylesheetTree.add("\n============>Tree Dump<============\n"); |
49 groupTree.add("-----\n"); | 49 stylesheetTree.add(stylesheet.toDebugString()); |
50 groupTree.add(group.toDebugString()); | 50 dumpTree = stylesheetTree.toString(); |
51 } | |
52 dumpTree = groupTree.toString(); | |
53 } catch (var cssParseException) { | 51 } catch (var cssParseException) { |
54 templateValid = false; | 52 templateValid = false; |
55 dumpTree = cssParseException.toString(); | 53 dumpTree = cssParseException.toString(); |
56 } | 54 } |
57 } else { | 55 } else { |
58 try { | 56 try { |
59 dumpTree = cssParseAndValidateDebug(cssExpr, cssWorld); | 57 dumpTree = cssParseAndValidateDebug(cssExpr, cssWorld); |
60 } catch (var cssException) { | 58 } catch (var cssException) { |
61 templateValid = false; | 59 templateValid = false; |
62 dumpTree = cssException.toString(); | 60 dumpTree = cssException.toString(); |
63 } | 61 } |
64 } | 62 } |
65 | 63 |
66 final var bgcolor = templateValid ? "white" : "red"; | 64 var bgcolor = templateValid ? "white" : "red"; |
67 final var color = templateValid ? "black" : "white"; | 65 var color = templateValid ? "black" : "white"; |
68 final var valid = templateValid ? "VALID" : "NOT VALID"; | 66 var valid = templateValid ? "VALID" : "NOT VALID"; |
69 String resultStyle = 'margin: 0; height: 138px; width: 100%; border: 0; border
-top: 1px solid black;'; | 67 String resultStyle = 'margin: 0; height: 138px; width: 100%; border: 0; border
-top: 1px solid black;'; |
70 result.innerHTML = ''' | 68 result.innerHTML = ''' |
71 <div style="font-weight: bold; background-color: $bgcolor; color: $color;"> | 69 <div style="font-weight: bold; background-color: $bgcolor; color: $color;"> |
72 Expression: $cssExpr is $valid | 70 Expression: $cssExpr is $valid |
73 </div> | 71 </div> |
74 <textarea style="$resultStyle">$dumpTree</textarea> | 72 <textarea style="$resultStyle">$dumpTree</textarea> |
75 '''; | 73 '''; |
76 } | 74 } |
77 | 75 |
78 void main() { | 76 void main() { |
(...skipping 11 matching lines...) Expand all Loading... |
90 <button onclick="runCss(true, true)" style="position: absolute; left: 430px;
top: 135px;">Parse</button> | 88 <button onclick="runCss(true, true)" style="position: absolute; left: 430px;
top: 135px;">Parse</button> |
91 <button onclick="runCss()" style="position: absolute; left: 500px; top: 135p
x;">Check</button> | 89 <button onclick="runCss()" style="position: absolute; left: 500px; top: 135p
x;">Check</button> |
92 <button onclick="runCss(true)" style="position: absolute; left: 570px; top:
135px;">Debug</button> | 90 <button onclick="runCss(true)" style="position: absolute; left: 570px; top:
135px;">Debug</button> |
93 <div style="top: 160px; left: 225px; position: absolute;"> | 91 <div style="top: 160px; left: 225px; position: absolute;"> |
94 <span style="font-weight:bold;">Result</span><br/> | 92 <span style="font-weight:bold;">Result</span><br/> |
95 <div id="result" style="width: 400px; height: 158px; border: black solid 1
px;"></textarea> | 93 <div id="result" style="width: 400px; height: 158px; border: black solid 1
px;"></textarea> |
96 </div> | 94 </div> |
97 '''; | 95 '''; |
98 | 96 |
99 document.body.appendChild(element); | 97 document.body.appendChild(element); |
| 98 // document.body.elements.add(element); |
100 | 99 |
101 // TODO(terry): Needed so runCss isn't shakened out. | 100 // TODO(terry): Needed so runCss isn't shakened out. |
102 if (false) { | 101 if (false) { |
103 runCss(); | 102 runCss(); |
104 } | 103 } |
105 | 104 |
106 initCssWorld(); | 105 initCssWorld(false); |
107 } | 106 } |
OLD | NEW |