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

Commit

Permalink
skipHtmlEscaping
Browse files Browse the repository at this point in the history
  • Loading branch information
zavr-1 committed Oct 8, 2019
1 parent f6c5022 commit 11f7ff5
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/com/google/common/css/JobDescription.java
Expand Up @@ -72,6 +72,7 @@ public class JobDescription {
public final boolean createSourceMap;
public final SourceMapDetailLevel sourceMapLevel;
public final boolean preserveImportantComments;
public final boolean skipHtmlEscaping;

static final String CONDITION_FOR_LTR = "GSS_LTR";
static final String CONDITION_FOR_RTL = "GSS_RTL";
Expand Down Expand Up @@ -146,7 +147,7 @@ public enum SourceMapDetailLevel { ALL, DEFAULT }
boolean suppressDependencyCheck, Map<String, Integer> compileConstants,
boolean createSourceMap,
SourceMapDetailLevel sourceMapLevel,
boolean preserveImportantComments) {
boolean preserveImportantComments, boolean skipHtmlEscaping) {
this.allowUndefinedConstants = allowUndefinedConstants;
Preconditions.checkArgument(!inputs.contains(null));
Preconditions.checkNotNull(outputFormat);
Expand Down Expand Up @@ -197,6 +198,7 @@ public enum SourceMapDetailLevel { ALL, DEFAULT }
this.createSourceMap = createSourceMap;
this.sourceMapLevel = sourceMapLevel;
this.preserveImportantComments = preserveImportantComments;
this.skipHtmlEscaping = skipHtmlEscaping;
}

/**
Expand Down
11 changes: 10 additions & 1 deletion src/com/google/common/css/JobDescriptionBuilder.java
Expand Up @@ -73,6 +73,7 @@ public class JobDescriptionBuilder {
boolean suppressDependencyCheck;
Map<String, Integer> compileConstants;
boolean preserveImportantComments;
boolean skipHtmlEscaping;

JobDescription job = null;
boolean createSourceMap;
Expand Down Expand Up @@ -116,6 +117,7 @@ public JobDescriptionBuilder() {
this.createSourceMap = false;
this.sourceMapLevel = SourceMapDetailLevel.DEFAULT;
this.preserveImportantComments = false;
this.skipHtmlEscaping = false;
}

public JobDescriptionBuilder copyFrom(JobDescription jobToCopy) {
Expand All @@ -134,6 +136,7 @@ public JobDescriptionBuilder copyFrom(JobDescription jobToCopy) {
this.allowDefPropagation = jobToCopy.allowDefPropagation;
this.allowUnrecognizedFunctions = jobToCopy.allowUnrecognizedFunctions;
this.allowDuplicateDeclarations = jobToCopy.allowDuplicateDeclarations;
this.skipHtmlEscaping = jobToCopy.skipHtmlEscaping;
this.expandBrowserPrefix = jobToCopy.expandBrowserPrefix;
this.allowedNonStandardFunctions =
ImmutableSet.copyOf(jobToCopy.allowedNonStandardFunctions);
Expand Down Expand Up @@ -226,6 +229,12 @@ public JobDescriptionBuilder setExcludedClassesFromRenaming(
return this;
}

public JobDescriptionBuilder setskipHtmlEscaping(boolean skip) {
checkJobIsNotAlreadyCreated();
this.skipHtmlEscaping = skip;
return this;
}

public JobDescriptionBuilder setCssRenamingPrefix(String cssRenamingPrefix) {
checkJobIsNotAlreadyCreated();
Preconditions.checkNotNull(cssRenamingPrefix);
Expand Down Expand Up @@ -523,7 +532,7 @@ public JobDescription getJobDescription() {
gssFunctionMapProvider, cssSubstitutionMapProvider,
outputRenamingMapFormat, inputRenamingMap, preserveComments,
suppressDependencyCheck, compileConstants,
createSourceMap, sourceMapLevel, preserveImportantComments);
createSourceMap, sourceMapLevel, preserveImportantComments, skipHtmlEscaping);
return job;
}

Expand Down
Expand Up @@ -189,9 +189,15 @@ static class Flags {
private Vendor vendor = null;

@Option(name = "--excluded-classes-from-renaming", usage =
"Pass the compiler a list of CSS class names that shoudn't be renamed.")
"Pass the compiler a list of CSS class names that shouldn't be renamed.")
private List<String> excludedClassesFromRenaming = Lists.newArrayList();

@Option(name = "--skip-html-escaping", usage =
"By default, the compiler will escape [<>\"&'] from output " +
"to make it suitable for safe embedding in HTML tags and attributes. " +
"When standalone CSS is generated, this is not necessary and can be skipped.")
private boolean skipHtmlEscaping = false;

// For enum values, args4j automatically lists all possible values when it
// prints the usage information for the flag, so including them in the usage
// message defined here would be redundant.
Expand Down Expand Up @@ -255,6 +261,7 @@ JobDescription createJobDescription() {
builder.setAllowWebkitKeyframes(true);
builder.setProcessDependencies(true);
builder.setExcludedClassesFromRenaming(excludedClassesFromRenaming);
builder.setskipHtmlEscaping(skipHtmlEscaping);
builder.setSimplifyCss(true);
builder.setEliminateDeadStyles(true);
builder.setCssSubstitutionMapProvider(renamingType
Expand Down
Expand Up @@ -129,7 +129,7 @@ private void parseAndPrint(StringBuilder result, GssParser parser)
}

if (job.outputFormat == OutputFormat.COMPRESSED) {
CompactPrinter compactPrinterPass = new CompactPrinter(cssTree, gssSourceMapGenerator);
CompactPrinter compactPrinterPass = new CompactPrinter(cssTree, gssSourceMapGenerator, job.skipHtmlEscaping);
compactPrinterPass.setPreserveMarkedComments(job.preserveImportantComments);
compactPrinterPass.runPass();
result.append(compactPrinterPass.getCompactPrintedString());
Expand Down
Expand Up @@ -24,6 +24,7 @@

/** A compact-printer for {@link CssTree} instances. */
public class CompactPrinter extends CodePrinter {
private boolean skipHtmlEscaping = false;

private String compactedPrintedString = null;

Expand Down Expand Up @@ -53,13 +54,18 @@ public CompactPrinter(CssTree tree, GssSourceMapGenerator generator) {
this(tree, null /* buffer */, generator);
}

public CompactPrinter(CssTree tree, GssSourceMapGenerator generator, boolean skipHtmlEscaping) {
this(tree, null /* buffer */, generator);
this.skipHtmlEscaping = skipHtmlEscaping;
}

public CompactPrinter(CssTree tree) {
this(tree, null /* buffer */, null /* generator */);
}

@Override
protected CssTreeVisitor createVisitor(VisitController visitController, CodeBuffer buffer) {
return new CompactPrintingVisitor(visitController, buffer);
return new CompactPrintingVisitor(visitController, buffer, this.skipHtmlEscaping);
}

/** Returns the CSS compacted printed output. */
Expand Down
Expand Up @@ -74,12 +74,19 @@ public class CompactPrintingVisitor extends DefaultTreeVisitor {
protected final CodeBuffer buffer;

private String compactedPrintedString = null;
private boolean skipHtmlEscaping = false;

public CompactPrintingVisitor(VisitController visitController, CodeBuffer buffer) {
this.visitController = checkNotNull(visitController);
this.buffer = checkNotNull(buffer);
}

public CompactPrintingVisitor(VisitController visitController, CodeBuffer buffer, boolean skipHtmlEscaping) {
this.visitController = checkNotNull(visitController);
this.buffer = checkNotNull(buffer);
this.skipHtmlEscaping = skipHtmlEscaping;
}

@Override
public boolean enterDefinition(CssDefinitionNode node) {
return false;
Expand Down Expand Up @@ -471,7 +478,9 @@ protected void appendValueNode(CssValueNode node) {
}
if (node instanceof CssStringNode) {
CssStringNode s = (CssStringNode) node;
buffer.append(s.toString(CssStringNode.HTML_ESCAPER));
buffer.append(
this.skipHtmlEscaping ? s.toString() : s.toString(CssStringNode.HTML_ESCAPER)
);
return;
}
if (node instanceof CssNumericNode) {
Expand Down

0 comments on commit 11f7ff5

Please sign in to comment.