OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 //#import('../file_system.dart'); | |
6 | |
7 #import("dart:dom"); | |
8 #import('css.dart'); | |
9 #import('../../frog/lang.dart', prefix:'lang'); | |
10 | |
11 | |
12 void runCss([bool debug = false, bool parseOnly = false]) { | |
13 final classes = document.getElementById('classes'); | |
14 final expression = document.getElementById('expression'); | |
15 final result = document.getElementById('result'); | |
16 | |
17 List<String> knownWorld = classes.value.split("\n"); | |
18 List<String> knownClasses = []; | |
19 List<String> knownIds = []; | |
20 for (name in knownWorld) { | |
21 if (name.startsWith('.')) { | |
22 knownClasses.add(name.substring(1)); | |
23 } else if (name.startsWith('#')) { | |
24 knownIds.add(name.substring(1)); | |
25 } | |
26 } | |
27 | |
28 CssWorld cssWorld = new CssWorld(knownClasses, knownIds); | |
29 bool templateValid = true; | |
30 String dumpTree = ""; | |
31 | |
32 String cssExpr = expression.value; | |
33 if (!debug) { | |
34 try { | |
35 cssParseAndValidate(cssExpr, cssWorld); | |
36 } catch (var cssException) { | |
37 templateValid = false; | |
38 dumpTree = cssException.toString(); | |
39 } | |
40 } else { | |
41 if (parseOnly) { | |
42 try { | |
43 Parser parser = new Parser(new lang.SourceFile(lang.SourceFile.IN_MEMORY _FILE, | |
jimhug
2011/11/10 17:58:56
Nit: line length.
terry
2011/11/16 14:00:22
Done.
| |
44 cssExpr)); | |
45 List<SelectorGroup> groups = parser.expression(); | |
46 StringBuffer groupTree = new StringBuffer(); | |
47 for (group in groups) { | |
48 String prettySelector = group.toString(); | |
49 groupTree.add("${prettySelector}\n"); | |
50 groupTree.add("-----\n"); | |
51 groupTree.add(group.toDebugString()); | |
52 } | |
53 dumpTree = groupTree.toString(); | |
54 } catch (var cssParseException) { | |
55 templateValid = false; | |
56 dumpTree = cssParseException.toString(); | |
57 } | |
58 } else { | |
59 try { | |
60 dumpTree = cssParseAndValidateDebug(cssExpr, cssWorld); | |
61 } catch (var cssException) { | |
62 templateValid = false; | |
63 dumpTree = cssException.toString(); | |
64 } | |
65 } | |
66 } | |
67 | |
68 var bgcolor; | |
jimhug
2011/11/10 17:58:56
Style suggestion. I believe it is best to either
terry
2011/11/16 14:00:22
Done.
| |
69 var color; | |
70 var valid; | |
71 if (templateValid) { | |
72 bgcolor = "white"; | |
73 color = "black"; | |
74 valid = "VALID"; | |
75 } else { | |
76 bgcolor = "red"; | |
77 color = "white"; | |
78 valid = "NOT VALID"; | |
79 } | |
80 | |
81 String resultStyle = 'margin: 0; height: 138px; width: 100%; border: 0; border -top: 1px solid black;'; | |
82 result.innerHTML = ''' | |
83 <div style="font-weight: bold; background-color: $bgcolor; color: $color;"> | |
84 Expression: $cssExpr is $valid | |
85 </div> | |
86 <textarea style="$resultStyle">$dumpTree</textarea> | |
87 '''; | |
88 } | |
89 | |
90 void main() { | |
91 var element = document.createElement('div'); | |
92 element.innerHTML = ''' | |
93 <div style="position: absolute; top: 10px; width: 200px;" align=center> | |
94 <span style="font-weight:bold;">Classes</span><br/> | |
95 <textarea id="classes" style="width: 200px; height: 310px;">.foobar\n.xyzz y\n.test\n.dummy\n#myId\n#myStory</textarea> | |
96 </div> | |
97 <div style="left: 225px; position: absolute; top: 10px;"> | |
98 <span style="font-weight:bold;">Selector Expression</span><br/> | |
99 <textarea id="expression" style="width: 400px; height: 100px;"></textarea> | |
100 <br/> | |
101 </div> | |
102 <button onclick="runCss(true, true)" style="position: absolute; left: 430px; top: 135px;">Parse</button> | |
103 <button onclick="runCss()" style="position: absolute; left: 500px; top: 135p x;">Check</button> | |
104 <button onclick="runCss(true)" style="position: absolute; left: 570px; top: 135px;">Debug</button> | |
105 <div style="top: 160px; left: 225px; position: absolute;"> | |
106 <span style="font-weight:bold;">Result</span><br/> | |
107 <div id="result" style="width: 400px; height: 158px; border: black solid 1 px;"></textarea> | |
108 </div> | |
109 '''; | |
110 | |
111 document.body.appendChild(element); | |
112 | |
113 // TODO(terry): Needed so runCss isn't shakened out. | |
jimhug
2011/11/10 17:58:56
Hmm... This is unfortunate - but matches the same
| |
114 if (false) { | |
115 runCss(); | |
116 } | |
117 | |
118 initCssWorld(); | |
119 } | |
OLD | NEW |