Skip to content
This repository has been archived by the owner on Feb 23, 2022. It is now read-only.

Commit

Permalink
Merge pull request #93 from google/update-2016-08-30
Browse files Browse the repository at this point in the history
Update from internal repository 2016-08-30
  • Loading branch information
mikesamuel authored Aug 30, 2016
2 parents 0b5667c + 2939e0f commit 5a88d3c
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 73 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ java -jar closure-stylesheets.jar --help
```

[download]: https://github.com/google/closure-stylesheets/releases
[build it from source]: https://github.com/google/closure-stylesheets/wiki/Building From Source.md
[build it from source]: https://github.com/google/closure-stylesheets/wiki/Building-From-Source.md


## CSS Extensions
Expand Down
26 changes: 19 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,23 +119,35 @@
<configuration>
<jdkVersion>${java.version}</jdkVersion>
<javadocFriendlyComments>true</javadocFriendlyComments>
<packageName>org.javacc.parser</packageName>
<sourceDirectory>${project.basedir}/src</sourceDirectory>
<outputDirectory>${project.build.directory}/generated-sources</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<mainClass>com.google.common.css.compiler.commandline.ClosureCommandLineCompiler</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>args4j</groupId>
<artifactId>args4j</artifactId>
<!-- 2.0.17 is actually used,
but this is the closest thing in Maven Central Repo. -->
<version>2.0.16</version>
<version>2.0.26</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -171,14 +183,14 @@

<dependency> <!-- For com.google.debugging.sourcemap -->
<groupId>com.google.javascript</groupId>
<artifactId>closure-compiler</artifactId>
<version>v20160619</version>
<artifactId>closure-compiler-unshaded</artifactId>
<version>v20160713</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<version>4.11</version>
<scope>test</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public boolean isAbstract() {
return getType() == CssAtRuleNode.Type.ABSTRACT_COMPONENT;
}

@SuppressWarnings("StringEquality")
@SuppressWarnings("ReferenceEquality")
public boolean isImplicitlyNamed() {
// Test against the exact string instance.
return getName().getValue() == IMPLICIT_NODE_NAME;
Expand Down
1 change: 1 addition & 0 deletions src/com/google/common/css/compiler/ast/CssRulesetNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public CssRulesetNode(List<CssCommentNode> comments) {
*/
public CssRulesetNode(CssRulesetNode node) {
this(node.getDeclarations().deepCopy());
this.setSourceCodeLocation(node.getSourceCodeLocation());
this.setComments(node.getComments());
this.selectors = node.getSelectors().deepCopy();
}
Expand Down
1 change: 1 addition & 0 deletions src/com/google/common/css/compiler/ast/CssTreeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ public void onRulesetStart(CssSelectorListNode selectorList) {

Preconditions.checkState(ruleset == null);
ruleset = new CssRulesetNode(comments);
ruleset.setSourceCodeLocation(selectorList.getSourceCodeLocation());
ruleset.setSelectors(selectorList);
comments.clear();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,14 @@ private static ImmutableList<BrowserPrefixRule> buildExpansionRules() {
.addExpandPropertyValue("repeating-linear-gradient")
.build());

builder.add(new BrowserPrefixRule.Builder()
.matchPropertyName("cursor")
.matchPropertyValue("grab")
.isFunction(false)
.addExpandPropertyValue("-moz-grab")
.addExpandPropertyValue("-webkit-grab")
.build());

// Needed for Firefox 15, Chrome 25, Safari 6, iOS Safari 6.1 or less
builder.add(new BrowserPrefixRule.Builder()
.matchPropertyValue("calc")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ public final class CheckMissingRequire extends DefaultTreeVisitor implements Css
private static final Logger logger = Logger.getLogger(CheckMissingRequire.class.getName());

private static final Pattern OVERRIDE_SELECTOR_REGEX = Pattern.compile(
"/\\*\\s+@overrideSelector\\s+\\{(.*)\\}\\s+\\*/");
"/\\*\\*?\\s+@overrideSelector\\s+\\{(.*)\\}\\s+\\*/");

private static final Pattern OVERRIDE_DEF_REGEX = Pattern.compile(
"/\\*\\s+@overrideDef\\s+\\{(.*)\\}\\s+\\*/");
"/\\*\\*?\\s+@overrideDef\\s+\\{(.*)\\}\\s+\\*/");

private final VisitController visitController;
private final ErrorManager errorManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package com.google.common.css.compiler.passes;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.LinkedListMultimap;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Maps;
Expand All @@ -28,28 +26,21 @@
import com.google.common.css.compiler.ast.CssProvideNode;
import com.google.common.css.compiler.ast.CssRequireNode;
import com.google.common.css.compiler.ast.DefaultTreeVisitor;
import com.google.common.css.compiler.ast.ErrorManager;
import com.google.common.css.compiler.ast.VisitController;

import java.util.Map;
import java.util.logging.Logger;
import java.util.regex.Pattern;

/**
* A compiler pass to help find missing {@code @require} lines for def constant references
* and mixins.
* This pass simply collects namespaces that correpond to constant definitions and mixins.
* Also see the CheckMissingRequire pass that is used in conjunction with this one.
*
* NOTE: The maps in this class can only be used within the same set of pass runs.
*
*/
public final class CollectProvideNamespaces extends DefaultTreeVisitor implements CssCompilerPass {
private static final Logger logger = Logger.getLogger(CollectProvideNamespaces.class.getName());

private static final Pattern OVERRIDE_REGEX = Pattern.compile(
"/\\*\\s+@overrideSelector\\s+\\{(.*)\\}\\s+\\*/");

private final VisitController visitController;
private final ErrorManager errorManager;

// Key: filename; Value: provide namespace
private final Map<String, String> filenameProvideMap = Maps.newHashMap();
Expand All @@ -63,25 +54,23 @@ public final class CollectProvideNamespaces extends DefaultTreeVisitor implement
private final ListMultimap<String, String> defmixinProvideMap = LinkedListMultimap.create();

public Map<String, String> getFilenameProvideMap() {
return ImmutableMap.copyOf(filenameProvideMap);
return filenameProvideMap;
}

public ListMultimap<String, String> getFilenameRequireMap() {
return ImmutableListMultimap.copyOf(filenameRequireMap);
return filenameRequireMap;
}

public ListMultimap<String, String> getDefProvideMap() {
return ImmutableListMultimap.copyOf(defProvideMap);
return defProvideMap;
}

public ListMultimap<String, String> getDefmixinProvideMap() {
return ImmutableListMultimap.copyOf(defmixinProvideMap);
return defmixinProvideMap;
}

public CollectProvideNamespaces(VisitController visitController,
ErrorManager errorManager) {
public CollectProvideNamespaces(VisitController visitController) {
this.visitController = visitController;
this.errorManager = errorManager;
}

@Override
Expand Down Expand Up @@ -135,6 +124,10 @@ public boolean enterMixinDefinition(CssMixinDefinitionNode node) {

@Override
public void runPass() {
filenameProvideMap.clear();
filenameRequireMap.clear();
defProvideMap.clear();
defmixinProvideMap.clear();
visitController.startVisit(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.io.IOException;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -176,54 +177,80 @@ public void setSourceRoot(String path){
((SourceMapGeneratorV3) generator).setSourceRoot(path);
}

private void addMapping(Mapping mapping) {
CssNode node = mapping.node;
FilePosition outputStartPosition = mapping.start;
FilePosition outputEndPosition = mapping.end;
String sourceFile = getSourceFileName(node);

// If the node does not have an associated source file or source location
// is unknown, then the node does not have sufficient info for source map.
if (sourceFile == null || node.getSourceCodeLocation().isUnknown()) {
return;
}

// TODO: could pass in an optional symbol name
generator.addMapping(
sourceFile, null,
new FilePosition(getStartLineno(node), getStartCharIndex(node)),
outputStartPosition, outputEndPosition);
}

/**
* Gets the source file file for current node.
* Generates the source map by passing all mappings to {@link #generator}.
*/
private String getSourceFileName(CssNode node) {
return node.getSourceCodeLocation().getSourceCode().getFileName();
private void generateSourceMap() {
List<CompleteMapping> completeMappings = new ArrayList<>(allMappings.size());
for (Mapping mapping : allMappings) {
// If the node does not have an associated source file or source location
// is unknown, then the node does not have sufficient info for source map.
if (mapping.node.getSourceCodeLocation().isUnknown()) {
continue;
}
CompleteMapping completeMapping = new CompleteMapping(mapping);
if (completeMapping.sourceFile == null) {
continue;
}
completeMappings.add(completeMapping);
}
Collections.sort(completeMappings);
for (CompleteMapping completeMapping : completeMappings) {
// TODO: could pass in an optional symbol name
generator.addMapping(
completeMapping.sourceFile, null,
completeMapping.inputStart,
completeMapping.outputStart, completeMapping.outputEnd);
}
}

/**
* Gets the start line index in the source code of {@code node} adjusted to 0-based indices.
*
* <p>Notes: Gss compiler uses a 1-based line number and source map V3 uses a 0-based line number.
*/
private int getStartLineno(CssNode node) {
return node.getSourceCodeLocation().getLineNumber() - 1;
}

/**
* Gets the start character index in the output buffer for current {@code node}.
*/
private int getStartCharIndex(CssNode node) {
return node.getSourceCodeLocation().getCharacterIndex();
}
private static final class CompleteMapping implements Comparable<CompleteMapping> {
final String sourceFile;
final FilePosition inputStart;
final FilePosition outputStart;
final FilePosition outputEnd;

/**
* Generates the source map by passing all mappings to {@link #generator}.
*/
private void generateSourceMap(){
for (Mapping mapping : allMappings) {
addMapping(mapping);
CompleteMapping(Mapping mapping) {
CssNode node = mapping.node;
this.sourceFile = getSourceFileName(node);
this.inputStart = new FilePosition(
getStartLineno(node), getStartCharIndex(node));
this.outputStart = mapping.start;
this.outputEnd = mapping.end;
}

@Override
public int compareTo(CompleteMapping m) {
int delta = outputStart.getLine() - m.outputStart.getLine();
if (delta == 0) {
delta = outputStart.getColumn() - m.outputStart.getColumn();
}
return delta;
}

/**
* Gets the source file file for current node.
*/
private static String getSourceFileName(CssNode node) {
return node.getSourceCodeLocation().getSourceCode().getFileName();
}

/**
* Gets the start line index in the source code of {@code node} adjusted to 0-based indices.
*
* <p>
* Note: Gss compiler uses a 1-based line number and source map V3 uses a 0-based line number.
*/
private static int getStartLineno(CssNode node) {
return node.getSourceCodeLocation().getLineNumber() - 1;
}

/**
* Gets the start character index in the output buffer for current {@code node}.
*/
private static int getStartCharIndex(CssNode node) {
return node.getSourceCodeLocation().getCharacterIndex();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ private List<CssNode> transformNodes(
Set<String> constants, CssComponentNode target, CssComponentNode source) {
CssBlockNode sourceBlock = source.getBlock();
CssBlockNode copyBlock = new CssBlockNode(false, sourceBlock.deepCopy().getChildren());
copyBlock.setSourceCodeLocation(source.getBlock().getSourceCodeLocation());
CssTree tree = new CssTree(
target.getSourceCodeLocation().getSourceCode(), new CssRootNode(copyBlock));
new TransformNodes(constants, target, target != source,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public boolean enterRuleset(CssRulesetNode node) {
for (CssSelectorNode sel : selectors.childIterable()) {
for (CssNode child : declarations.childIterable()) {
CssRulesetNode ruleset = new CssRulesetNode();
ruleset.setSourceCodeLocation(node.getSourceCodeLocation());
ruleset.addDeclaration(child.deepCopy());
ruleset.addSelector(sel.deepCopy());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,9 @@ public void print(String message) {
if (exactMatch) {
assertEquals(expectedMessages[currentIndex], message);
} else {
assertTrue(message.contains(expectedMessages[currentIndex]));
assertTrue("Expected error '" + message + "' to contain '"
+ expectedMessages[currentIndex] + "'.",
message.contains(expectedMessages[currentIndex]));
}
currentIndex++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected void runPasses(TestErrorManager errorMgr) {
pass.runPass();
}
CollectProvideNamespaces collectProvides = new CollectProvideNamespaces(
tree.getVisitController(), errorMgr);
tree.getVisitController());
collectProvides.runPass();
new CheckMissingRequire(
tree.getVisitController(),
Expand Down

0 comments on commit 5a88d3c

Please sign in to comment.