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