Skip to content

Commit

Permalink
TAP5-2708/2753: CssCompressor updates and CSS variable fixes (#40)
Browse files Browse the repository at this point in the history
The CssCompressor in tapestry-webresources is now based on the latest YUI commit cf0497e.

To make the original code work it needed some error fixing:
- Duplicate variable declaration of "oldCss"
- Faulty non-escaped regex
- zeros.css test failed

I'm not 100% sure the CssCompressor behaves 100% as before, but it's a necessary trade-off so it can be improved further, as the original YUI project is dead.
All the pre-existing tests from YUI are now run directly in Tapestry, and we can add our own tests easily.

Furthermore, the code was cleaned up with pre-compiled Patterns, and no longer converting between Strings/Readers/Writers.

Co-authored-by: Volker Lamp <vlamp@apache.org>
  • Loading branch information
benweidig and vjlamp committed Jun 23, 2023
1 parent 815390f commit 4ffc309
Show file tree
Hide file tree
Showing 140 changed files with 8,442 additions and 233 deletions.
2 changes: 1 addition & 1 deletion tapestry-webresources/NOTICE.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
This product includes software developed by
The Apache Software Foundation (http://www.apache.org/).

This product includes source from the YUI Compressor library, available under a BSD License.
This product includes source and test files from the YUI Compressor library, available under a BSD License.
http://yui.github.io/yuicompressor/
2 changes: 1 addition & 1 deletion tapestry-webresources/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jar.manifest {


test {
useJUnit()
useJUnitPlatform()

systemProperties("geb.build.reportsDir": "$reporting.baseDir/geb",
"tapestry.compiled-asset-cache-dir": "$buildDir/compiled-asset-cache",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import java.io.*;

/**
* A wrapper around YUI Compressor. This module does not have a dependency on YUICompressor;
* isntead a local copy of the YUICompressor CSS minimizer is kept (because the reset of YUICompressor
* A wrapper around a customized YUI Compressor. This module does not have a dependency on YUICompressor;
* instead a local copy of the YUICompressor CSS minimizer is kept (because the reset of YUICompressor
* is painful to mix due to how it attempts to patch Rhino).
*/
public class CSSMinimizer extends AbstractMinimizer
Expand All @@ -38,20 +38,15 @@ public CSSMinimizer(Logger logger, OperationTracker tracker, AssetChecksumGenera
@Override
protected InputStream doMinimize(StreamableResource resource) throws IOException
{
StringWriter writer = new StringWriter(1000);
Reader reader = new InputStreamReader(resource.openStream());
InputStream is = resource.openStream();

try
{
new CssCompressor(reader).compress(writer, -1);

writer.flush();

return IOUtils.toInputStream(writer.getBuffer());
String compressed = CssCompressor.compress(is);
return IOUtils.toInputStream(compressed);
} finally
{
InternalUtils.close(reader);
InternalUtils.close(writer);
InternalUtils.close(is);
}
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,29 +1,132 @@
package t5.webresources.tests

import geb.spock.GebReportingSpec

import org.apache.tapestry5.internal.webresources.CssCompressor
import org.apache.tapestry5.test.JettyRunner

import spock.lang.Issue;
import spock.lang.Shared
import spock.lang.Specification;

class CssCompressorSpec extends Specification {

@Issue('TAP5-2524')
def "minify CSS with keyframes "() {
setup:
given:
def css = '''@keyframes anim {
0% { opacity: 0; }
100% { opacity: 1; }
}'''
StringWriter writer = new StringWriter()
def expected = "@keyframes anim{0%{opacity:0}100%{opacity:1}}"

when:
def result = CssCompressor.compress(css)

then:
result == expected
}

@Issue('TAP5-2753')
def "preserve space for calc operators"() {
given:
def is = CssCompressorSpec.class.getResourceAsStream("/t5/webresources/css/tap5-2753.css")
def t = CssCompressorSpec.class.getResourceAsStream("/t5/webresources/css/tap5-2753.css.min").text
def expected = CssCompressorSpec.class.getResourceAsStream("/t5/webresources/css/tap5-2753.css.min").text.trim()

when:
def result = CssCompressor.compress(is)

then:
result == expected
}

def "bootstrap.css integry check"() {
given:
def is = CssCompressorSpec.class.getResourceAsStream("/t5/webresources/css/bootstrap.css")
def expected = CssCompressorSpec.class.getResourceAsStream("/t5/webresources/css/bootstrap.css.min").text.trim()

when:
def result = CssCompressor.compress(is)

then:
result == expected
}

def "yui compressor test '#rawFile'"() {
given:
def is = CssCompressorSpec.class.getResourceAsStream("/t5/webresources/css/yui/$rawFile")
def expected = CssCompressorSpec.class.getResourceAsStream("/t5/webresources/css/yui/${rawFile}.min").text.trim()

when:
new CssCompressor(new StringReader(css)).compress(writer, -1)
def result = CssCompressor.compress(is)

then:
writer.toString() == '''@keyframes anim{0%{opacity:0}100%{opacity:1}}'''
result == expected

where:
rawFile << [
"background-position.css",
"border-none.css",
"box-model-hack.css",
"bug2527974.css",
"bug2527991.css",
"bug2527998.css",
"bug2528034.css",
"bug-flex.css",
"bug-nested-pseudoclass.css",
"bug-preservetoken-calc.css",
"charset-media.css",
"color.css",
"color-keyword.css",
"color-simple.css",
"comment.css",
"concat-charset.css",
"dataurl-base64-doublequotes.css",
"dataurl-base64-eof.css",
"dataurl-base64-linebreakindata.css",
"dataurl-base64-noquotes.css",
"dataurl-base64-singlequotes.css",
"dataurl-base64-twourls.css",
"dataurl-dbquote-font.css",
"dataurl-nonbase64-doublequotes.css",
"dataurl-nonbase64-noquotes.css",
"dataurl-nonbase64-singlequotes.css",
"dataurl-noquote-multiline-font.css",
"dataurl-realdata-doublequotes.css",
"dataurl-realdata-noquotes.css",
"dataurl-realdata-singlequotes.css",
"dataurl-realdata-yuiapp.css",
"dataurl-singlequote-font.css",
"decimals.css",
"dollar-header.css",
"font-face.css",
// "hsla-issue81.css.FAIL",
"ie5mac.css",
"ie-backslash9-hack.css",
"issue151.css",
// "issue172.css.FAIL",
"issue180.css",
"issue205.css",
"issue221.css",
"issue222.css",
"issue-59.css",
"lowercasing.css",
"media-empty-class.css",
"media-multi.css",
"media-test.css",
"old-ie-filter-matrix.css",
"opacity-filter.css",
"opera-pixel-ratio.css",
"pointzeros.css",
"preserve-case.css",
"preserve-important.css",
"preserve-new-line.css",
"preserve-strings.css",
"pseudo.css",
"pseudo-first.css",
// "rgb-issue81.css.FAIL",
"special-comments.css",
"star-underscore-hacks.css",
"string-in-comment.css",
"webkit-transform.css",
"zeros.css"
]
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package t5.webresources.tests

import geb.spock.GebReportingSpec
import geb.spock.GebSpec
import org.apache.tapestry5.test.JettyRunner
import spock.lang.Shared

class WebResourcesSpec extends GebReportingSpec {
class WebResourcesSpec extends GebSpec {

@Shared
def runner;
Expand Down

0 comments on commit 4ffc309

Please sign in to comment.