Revision: 7774
Author: [email protected]
Date: Tue Mar 23 12:35:23 2010
Log: Add tail-globs to CssResource's @external declarations.
http://gwt-code-reviews.appspot.com/243803
Suggested by: gak
Patch by: bobv
Review by: rjrjr
http://code.google.com/p/google-web-toolkit/source/detail?r=7774
Added:
/trunk/user/test/com/google/gwt/resources/css/CssExternalTest.java
/trunk/user/test/com/google/gwt/resources/css/external.css
/trunk/user/test/com/google/gwt/resources/css/external_star.css
Modified:
/trunk/user/src/com/google/gwt/resources/css/ExternalClassesCollector.java
/trunk/user/test/com/google/gwt/resources/ResourcesSuite.java
=======================================
--- /dev/null
+++ /trunk/user/test/com/google/gwt/resources/css/CssExternalTest.java Tue
Mar 23 12:35:23 2010
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may
not
+ * use this file except in compliance with the License. You may obtain a
copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
under
+ * the License.
+ */
+package com.google.gwt.resources.css;
+
+import com.google.gwt.core.ext.TreeLogger;
+import com.google.gwt.core.ext.UnableToCompleteException;
+import com.google.gwt.resources.css.ast.CssStylesheet;
+import com.google.gwt.resources.rg.CssTestCase;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+
+/**
+ * Tests {...@link ExternalClassesCollector}.
+ */
+public class CssExternalTest extends CssTestCase {
+
+ public void testExternal() throws UnableToCompleteException {
+ CssStylesheet sheet = GenerateCssAst.exec(TreeLogger.NULL,
+ getClass().getClassLoader().getResource(
+ "com/google/gwt/resources/css/external.css"));
+ assertNotNull(sheet);
+
+ ExternalClassesCollector v = new ExternalClassesCollector();
+ v.accept(sheet);
+
+
assertEquals(Arrays.asList("a", "b", "c", "glob-a", "glob-b", "no*effect"),
+ new ArrayList<String>(v.getClasses()));
+ }
+
+ /**
+ * Make sure the short-circuit logic for <code>{...@literal @external}
*</code> works correctly.
+ */
+ public void testExternalStar() throws UnableToCompleteException {
+ CssStylesheet sheet = GenerateCssAst.exec(TreeLogger.NULL,
+ getClass().getClassLoader().getResource(
+ "com/google/gwt/resources/css/external_star.css"),
+ getClass().getClassLoader().getResource(
+ "com/google/gwt/resources/css/external.css"));
+ assertNotNull(sheet);
+
+ ExternalClassesCollector v = new ExternalClassesCollector();
+ v.accept(sheet);
+
+
assertEquals(Arrays.asList("a", "c", "d", "glob-a", "glob-b", "no-effect"),
+ new ArrayList<String>(v.getClasses()));
+ }
+}
=======================================
--- /dev/null
+++ /trunk/user/test/com/google/gwt/resources/css/external.css Tue Mar 23
12:35:23 2010
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may
not
+ * use this file except in compliance with the License. You may obtain a
copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
under
+ * the License.
+ */
+
+/* Always collected */
+...@external a .b, c;
+
+.a{}
+/* Ignore b */
+.c:content{}
+/* d should also be ignored */
+.d{}
+
+/* Will be expanded */
+...@external glob-*;
+.glob-a {}
+.glob-b {}
+
+/* No effect */
+...@external nothing-*;
+
+/* Internal stars don't do anything */
+...@external no*effect;
+.no-effect {}
=======================================
--- /dev/null
+++ /trunk/user/test/com/google/gwt/resources/css/external_star.css Tue Mar
23 12:35:23 2010
@@ -0,0 +1,18 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may
not
+ * use this file except in compliance with the License. You may obtain a
copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
under
+ * the License.
+ */
+
+ /* Collect all */
+...@external *;
=======================================
---
/trunk/user/src/com/google/gwt/resources/css/ExternalClassesCollector.java
Wed Oct 28 09:10:53 2009
+++
/trunk/user/src/com/google/gwt/resources/css/ExternalClassesCollector.java
Tue Mar 23 12:35:23 2010
@@ -17,23 +17,73 @@
import com.google.gwt.resources.css.ast.Context;
import com.google.gwt.resources.css.ast.CssExternalSelectors;
+import com.google.gwt.resources.css.ast.CssSelector;
import com.google.gwt.resources.css.ast.CssVisitor;
import java.util.HashSet;
import java.util.Set;
+import java.util.SortedSet;
+import java.util.TreeSet;
+import java.util.regex.Matcher;
/**
- * Collects all {...@code @external} declarations in the stylesheet.
+ * Collects all {...@code @external} declarations in the stylesheet. This
visitor
+ * will expand tail-globs.
*/
public class ExternalClassesCollector extends CssVisitor {
- private final Set<String> classes = new HashSet<String>();
+ public static final String GLOB_STRING = "*";
+
+ private final SortedSet<String> allClasses = new TreeSet<String>();
+ private final SortedSet<String> externalClasses = new TreeSet<String>();
+ private final Set<String> globs = new HashSet<String>();
+
+ /**
+ * This is a short-circuit for <code>{...@literal @external} *</code>.
+ */
+ private boolean matchAll;
@Override
public void endVisit(CssExternalSelectors x, Context ctx) {
- classes.addAll(x.getClasses());
+ if (matchAll) {
+ return;
+ }
+
+ for (String selector : x.getClasses()) {
+ if (selector.equals(GLOB_STRING)) {
+ matchAll = true;
+ return;
+ } else if (selector.endsWith(GLOB_STRING)) {
+ globs.add(selector.substring(0, selector.length() - 1));
+ } else {
+ externalClasses.add(selector);
+ }
+ }
}
- public Set<String> getClasses() {
- return classes;
+ @Override
+ public void endVisit(CssSelector x, Context ctx) {
+ Matcher m =
CssSelector.CLASS_SELECTOR_PATTERN.matcher(x.getSelector());
+
+ while (m.find()) {
+ allClasses.add(m.group(1));
+ }
+ }
+
+ public SortedSet<String> getClasses() {
+ if (matchAll) {
+ return allClasses;
+ }
+
+ glob : for (String glob : globs) {
+ for (String clazz : allClasses.tailSet(glob)) {
+ if (clazz.startsWith(glob)) {
+ externalClasses.add(clazz);
+ } else {
+ continue glob;
+ }
+ }
+ }
+
+ return externalClasses;
}
}
=======================================
--- /trunk/user/test/com/google/gwt/resources/ResourcesSuite.java Thu Jan
21 09:47:21 2010
+++ /trunk/user/test/com/google/gwt/resources/ResourcesSuite.java Tue Mar
23 12:35:23 2010
@@ -21,6 +21,7 @@
import com.google.gwt.resources.client.ImageResourceTest;
import com.google.gwt.resources.client.NestedBundleTest;
import com.google.gwt.resources.client.TextResourceTest;
+import com.google.gwt.resources.css.CssExternalTest;
import com.google.gwt.resources.css.CssNodeClonerTest;
import com.google.gwt.resources.css.CssReorderTest;
import com.google.gwt.resources.css.CssRtlTest;
@@ -37,6 +38,7 @@
public static Test suite() {
GWTTestSuite suite = new GWTTestSuite("Test for
com.google.gwt.resources");
+ suite.addTestSuite(CssExternalTest.class);
suite.addTestSuite(CSSResourceTest.class);
suite.addTestSuite(CssReorderTest.class);
suite.addTestSuite(CssRtlTest.class);
--
http://groups.google.com/group/Google-Web-Toolkit-Contributors
To unsubscribe from this group, send email to
google-web-toolkit-contributors+unsubscribegooglegroups.com or reply to this email with
the words "REMOVE ME" as the subject.