Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(211)

Unified Diff: frog/css/test.dart

Issue 8498020: Beginning of CSS parser using frog parsering infrastructure. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: frog/css/test.dart
diff --git a/frog/css/test.dart b/frog/css/test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..370f265343e290ff03e30dd15b2b37112a2ad78c
--- /dev/null
+++ b/frog/css/test.dart
@@ -0,0 +1,113 @@
+// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+#import("dart:dom");
+#import('css.dart');
+
+void runCss([bool debug = false, bool parseOnly = false]) {
+ final classes = document.getElementById('classes');
+ final expression = document.getElementById('expression');
+ final result = document.getElementById('result');
+
+ List<String> knownWorld = classes.value.split("\n");
+ List<String> knownClasses = [];
+ List<String> knownIds = [];
+ for (name in knownWorld) {
+ if (name.startsWith('.')) {
+ knownClasses.add(name.substring(1));
+ } else if (name.startsWith('#')) {
+ knownIds.add(name.substring(1));
+ }
+ }
+
+ CssWorld cssWorld = new CssWorld(knownClasses, knownIds);
+ bool templateValid = true;
+ String dumpTree = "";
+
+ String cssExpr = expression.value;
+ if (!debug) {
+ try {
+ CssTemplate.xlate(cssExpr, cssWorld);
+ } catch (var cssException) {
+ templateValid = false;
+ dumpTree = cssException.toString();
+ }
+ } else {
+ if (parseOnly) {
nweiz 2011/11/10 00:04:39 } else if (parseOnly) {
terry 2011/11/22 16:40:47 Done.
+ try {
+ Parser parser = new Parser(new SourceFile(SourceFile.IN_MEMORY_FILE,
+ cssExpr));
+ List<SelectorGroup> groups = parser.expression(true);
+ StringBuffer groupTree = new StringBuffer();
+ for (group in groups) {
+ String prettySelector = group.toString();
+ groupTree.add("${prettySelector}\n");
+ groupTree.add("-----\n");
+ groupTree.add(group.toDebugString());
+ }
+ dumpTree = groupTree.toString();
+ } catch (var cssParseException) {
+ templateValid = false;
+ dumpTree = cssParseException.toString();
+ }
+ } else {
+ try {
+ dumpTree = CssTemplate.xlateDebug(cssExpr, cssWorld);
+ } catch (var cssException) {
+ templateValid = false;
+ dumpTree = cssException.toString();
+ }
+ }
+ }
+
+ var bgcolor;
+ var color;
+ var valid;
+ if (templateValid) {
+ bgcolor = "white";
+ color = "black";
+ valid = "VALID";
+ } else {
+ bgcolor = "red";
+ color = "white";
+ valid = "NOT VALID";
+ }
+
+ String resultStyle = 'margin: 0; height: 138px; width: 100%; border: 0; border-top: 1px solid black;';
+ result.innerHTML = '''
+ <div style="font-weight: bold; background-color: $bgcolor; color: $color;">
+ Expression: $cssExpr is $valid
+ </div>
+ <textarea style="$resultStyle">$dumpTree</textarea>
+ ''';
+}
+
+void main() {
+ var element = document.createElement('div');
+ element.innerHTML = '''
+ <div style="position: absolute; top: 10px; width: 200px;" align=center>
+ <span style="font-weight:bold;">Classes</span><br/>
+ <textarea id="classes" style="width: 200px; height: 310px;">.foobar\n.xyzzy\n.test\n.dummy\n#myId\n#myStory</textarea>
+ </div>
+ <div style="left: 225px; position: absolute; top: 10px;">
+ <span style="font-weight:bold;">Selector Expression</span><br/>
+ <textarea id="expression" style="width: 400px; height: 100px;"></textarea>
+ <br/>
+ </div>
+ <button onclick="runCss(true, true)" style="position: absolute; left: 430px; top: 135px;">Parse</button>
+ <button onclick="runCss()" style="position: absolute; left: 500px; top: 135px;">Check</button>
+ <button onclick="runCss(true)" style="position: absolute; left: 570px; top: 135px;">Debug</button>
+ <div style="top: 160px; left: 225px; position: absolute;">
+ <span style="font-weight:bold;">Result</span><br/>
+ <div id="result" style="width: 400px; height: 158px; border: black solid 1px;"></textarea>
+ </div>
+ ''';
+
+ document.body.appendChild(element);
+
+ // TODO(terry): Needed so runCss isn't shakened out.
+ if (false) {
+ runCss();
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698