use of com.helger.css.decl.CascadingStyleSheet in project ph-css by phax.
the class CSSCompressor method getRewrittenCSS.
/**
* Get the rewritten version of the passed CSS code. This is done by
* interpreting the CSS and than writing it again with the passed settings.
* This can e.g. be used to create a compressed version of a CSS.
*
* @param sOriginalCSS
* The original CSS code to be compressed.
* @param aSettings
* The CSS writer settings to use. The version is used to read the
* original CSS.
* @return If compression failed because the CSS is invalid or whatsoever, the
* original CSS is returned, else the rewritten version is returned.
*/
@Nonnull
public static String getRewrittenCSS(@Nonnull final String sOriginalCSS, @Nonnull final CSSWriterSettings aSettings) {
ValueEnforcer.notNull(sOriginalCSS, "OriginalCSS");
ValueEnforcer.notNull(aSettings, "Settings");
final CascadingStyleSheet aCSS = CSSReader.readFromString(sOriginalCSS, aSettings.getVersion());
if (aCSS != null) {
try {
return new CSSWriter(aSettings).getCSSAsString(aCSS);
} catch (final Exception ex) {
s_aLogger.warn("Failed to write optimized CSS!", ex);
}
}
return sOriginalCSS;
}
use of com.helger.css.decl.CascadingStyleSheet in project ph-css by phax.
the class ParserCSS30Test method test2.
@Test
public void test2() throws ParseException {
final ParserCSS30TokenManager aTokenHdl = new ParserCSS30TokenManager(new CSSCharStream(new NonBlockingStringReader(CSS2)));
aTokenHdl.setDebugStream(System.out);
final ParserCSS30 aParser = new ParserCSS30(aTokenHdl);
aParser.disable_tracing();
final CSSNode aNode = aParser.styleSheet();
assertNotNull(aNode);
final CascadingStyleSheet aCSS = CSSHandler.readCascadingStyleSheetFromNode(ECSSVersion.CSS30, aNode, CSSReader.getDefaultInterpretErrorHandler());
assertNotNull(aCSS);
for (final ICSSTopLevelRule aTopLevelRule : aCSS.getAllFontFaceRules()) assertTrue(aCSS.removeRule(aTopLevelRule).isChanged());
}
use of com.helger.css.decl.CascadingStyleSheet in project ph-css by phax.
the class AbstractFuncTestCSSReader method testReadGood.
protected final void testReadGood(@Nonnull final String sBaseDir) {
final File aBaseDir = new File(sBaseDir);
if (!aBaseDir.exists())
throw new IllegalArgumentException("BaseDir " + sBaseDir + " does not exist!");
for (final File aFile : new FileSystemRecursiveIterator(aBaseDir).withFilter(IFileFilter.filenameEndsWith(".css"))) {
final String sKey = aFile.getAbsolutePath();
if (m_bDebug)
m_aLogger.info("Filename: " + sKey);
final CollectingCSSParseErrorHandler aErrorHdl = new CollectingCSSParseErrorHandler();
m_aReaderSettings.setCustomErrorHandler(aErrorHdl.and(new LoggingCSSParseErrorHandler()));
final CascadingStyleSheet aCSS = CSSReader.readFromFile(aFile, m_aReaderSettings);
assertNotNull(sKey, aCSS);
// May have errors or not
if (m_bDebug)
m_aLogger.info("Parse errors: " + aErrorHdl.getAllParseErrors().toString());
CommonsTestHelper.testDefaultSerialization(aCSS);
// Write optimized version and compare it
String sCSS = new CSSWriter(m_aWriterSettings.setOptimizedOutput(true)).getCSSAsString(aCSS);
assertNotNull(sKey, sCSS);
if (m_bDebug)
m_aLogger.info("Created CSS: " + sCSS);
final CascadingStyleSheet aCSSReRead = CSSReader.readFromStringReader(sCSS, m_aReaderSettings);
assertNotNull("Failed to parse " + sKey + ":\n" + sCSS, aCSSReRead);
assertEquals(sKey + "\n" + sCSS, aCSS, aCSSReRead);
// Write non-optimized version and compare it
sCSS = new CSSWriter(m_aWriterSettings.setOptimizedOutput(false)).getCSSAsString(aCSS);
assertNotNull(sKey, sCSS);
if (m_bDebug)
m_aLogger.info("Read and re-created CSS: " + sCSS);
assertEquals(sKey, aCSS, CSSReader.readFromStringReader(sCSS, m_aReaderSettings));
// Write non-optimized and code-removed version and ensure it is not
// null
sCSS = new CSSWriter(m_aWriterSettings.setOptimizedOutput(false).setRemoveUnnecessaryCode(true)).getCSSAsString(aCSS);
assertNotNull(sKey, sCSS);
assertNotNull(sKey, CSSReader.readFromStringReader(sCSS, m_aReaderSettings));
// Restore value :)
m_aWriterSettings.setRemoveUnnecessaryCode(false);
}
}
use of com.helger.css.decl.CascadingStyleSheet in project ph-css by phax.
the class CSSReader30SpecialFuncTest method testReadSpecialGood.
@Test
public void testReadSpecialGood() {
final ECSSVersion eVersion = ECSSVersion.CSS30;
final Charset aCharset = StandardCharsets.UTF_8;
final File aFile = new File("src/test/resources/testfiles/css30/good/artificial/test-postcss-cssnext.css");
final CascadingStyleSheet aCSS = CSSReader.readFromFile(aFile, aCharset, eVersion);
assertNotNull(aCSS);
final String sCSS = new CSSWriter(eVersion, false).getCSSAsString(aCSS);
assertNotNull(sCSS);
if (false)
s_aLogger.info(sCSS);
}
use of com.helger.css.decl.CascadingStyleSheet in project ph-css by phax.
the class Issue24Test method testIssue.
@Test
public void testIssue() {
final IReadableResource aRes = new ClassPathResource("testfiles/css30/bad_but_browsercompliant/issue24.css");
assertTrue(aRes.exists());
final CascadingStyleSheet aCSS = CSSReader.readFromStream(aRes, new CSSReaderSettings().setFallbackCharset(StandardCharsets.UTF_8).setBrowserCompliantMode(true).setCustomErrorHandler(new LoggingCSSParseErrorHandler()));
assertNotNull(aCSS);
if (false)
System.out.println(new CSSWriter().getCSSAsString(aCSS));
}
Aggregations