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

Unified Diff: packages/csslib/example/call_parser.dart

Issue 1400473008: Roll Observatory packages and add a roll script (Closed) Base URL: git@github.com:dart-lang/observatory_pub_packages.git@master
Patch Set: Created 5 years, 2 months 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
« no previous file with comments | « packages/csslib/codereview.settings ('k') | packages/csslib/example/call_parser.html » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: packages/csslib/example/call_parser.dart
diff --git a/packages/csslib/example/call_parser.dart b/packages/csslib/example/call_parser.dart
new file mode 100644
index 0000000000000000000000000000000000000000..7179a170f83697017689de04253dfa474588416d
--- /dev/null
+++ b/packages/csslib/example/call_parser.dart
@@ -0,0 +1,96 @@
+// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+
+import 'package:csslib/parser.dart' as css;
+import 'package:csslib/visitor.dart';
+
+const _default = const css.PreprocessorOptions(
+ useColors: false,
+ checked: true,
+ warningsAsErrors: true,
+ inputFile: 'memory');
+
+/**
+ * Spin-up CSS parser in checked mode to detect any problematic CSS. Normally,
+ * CSS will allow any property/value pairs regardless of validity; all of our
+ * tests (by default) will ensure that the CSS is really valid.
+ */
+StyleSheet parseCss(String cssInput,
+ {List errors, css.PreprocessorOptions opts}) {
+ return css.parse(cssInput,
+ errors: errors, options: opts == null ? _default : opts);
+}
+
+// Pretty printer for CSS.
+var emitCss = new CssPrinter();
+String prettyPrint(StyleSheet ss) =>
+ (emitCss..visitTree(ss, pretty: true)).toString();
+
+main() {
+ var errors = [];
+
+ // Parse a simple stylesheet.
+ print('1. Good CSS, parsed CSS emitted:');
+ print(' =============================');
+ var stylesheet = parseCss(
+ '@import "support/at-charset-019.css"; div { color: red; }'
+ 'button[type] { background-color: red; }'
+ '.foo { '
+ 'color: red; left: 20px; top: 20px; width: 100px; height:200px'
+ '}'
+ '#div {'
+ 'color : #00F578; border-color: #878787;'
+ '}', errors: errors);
+
+ if (!errors.isEmpty) {
+ print("Got ${errors.length} errors.\n");
+ for (var error in errors) {
+ print(error);
+ }
+ } else {
+ print(prettyPrint(stylesheet));
+ }
+
+ // Parse a stylesheet with errors
+ print('2. Catch severe syntax errors:');
+ print(' ===========================');
+ var stylesheetError = parseCss('.foo #%^&*asdf{ '
+ 'color: red; left: 20px; top: 20px; width: 100px; height:200px'
+ '}', errors: errors);
+
+ if (!errors.isEmpty) {
+ print("Got ${errors.length} errors.\n");
+ for (var error in errors) {
+ print(error);
+ }
+ } else {
+ print(stylesheetError.toString());
+ }
+
+ // Parse a stylesheet that warns (checks) problematic CSS.
+ print('3. Detect CSS problem with checking on:');
+ print(' ===================================');
+ stylesheetError = parseCss('# div1 { color: red; }', errors: errors);
+
+ if (!errors.isEmpty) {
+ print("Detected ${errors.length} problem in checked mode.\n");
+ for (var error in errors) {
+ print(error);
+ }
+ } else {
+ print(stylesheetError.toString());
+ }
+
+ // Parse a CSS selector.
+ print('4. Parse a selector only:');
+ print(' ======================');
+ var selectorAst = css.selector('#div .foo', errors: errors);
+ if (!errors.isEmpty) {
+ print("Got ${errors.length} errors.\n");
+ for (var error in errors) {
+ print(error);
+ }
+ } else {
+ print(prettyPrint(selectorAst));
+ }
+}
« no previous file with comments | « packages/csslib/codereview.settings ('k') | packages/csslib/example/call_parser.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698