Search in sources :

Example 51 with CascadingStyleSheet

use of com.helger.css.decl.CascadingStyleSheet in project ph-css by phax.

the class Issue4Test method testIssue4.

@Test
public void testIssue4() {
    final IReadableResource aRes = new ClassPathResource("testfiles/css30/good/issue4.css");
    assertTrue(aRes.exists());
    final CascadingStyleSheet aCSS = CSSReader.readFromStream(aRes, StandardCharsets.UTF_8, ECSSVersion.CSS30, new LoggingCSSParseErrorHandler());
    assertNotNull(aCSS);
    if (false)
        System.out.println(new CSSWriter(ECSSVersion.CSS30).getCSSAsString(aCSS));
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) IReadableResource(com.helger.commons.io.resource.IReadableResource) CSSWriter(com.helger.css.writer.CSSWriter) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) LoggingCSSParseErrorHandler(com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler) Test(org.junit.Test)

Example 52 with CascadingStyleSheet

use of com.helger.css.decl.CascadingStyleSheet in project ph-css by phax.

the class CSSVisitor30FuncTest method testVisitConstantCSS.

@Test
public void testVisitConstantCSS() {
    // CSS 1
    CascadingStyleSheet aCSS = CSSReader.readFromString(CSS1, ECSSVersion.CSS30);
    assertNotNull(aCSS);
    MockCountingUrlVisitor aVisitor = new MockCountingUrlVisitor();
    CSSVisitor.visitCSSUrl(aCSS, aVisitor);
    assertEquals(4, aVisitor.getCount());
    // CSS 2
    aCSS = CSSReader.readFromString(CSS2, ECSSVersion.CSS30);
    assertNotNull(aCSS);
    aVisitor = new MockCountingUrlVisitor();
    CSSVisitor.visitCSSUrl(aCSS, aVisitor);
    assertEquals(18, aVisitor.getCount());
    // CSS 3
    aCSS = CSSReader.readFromString(CSS3, ECSSVersion.CSS30);
    assertNotNull(aCSS);
    aVisitor = new MockCountingUrlVisitor();
    CSSVisitor.visitCSSUrl(aCSS, aVisitor);
    assertEquals(1, aVisitor.getCount());
    // CSS 4
    aCSS = CSSReader.readFromString(CSS4, ECSSVersion.CSS30);
    assertNotNull(aCSS);
    aVisitor = new MockCountingUrlVisitor();
    CSSVisitor.visitCSSUrl(aCSS, aVisitor);
    assertEquals(1, aVisitor.getCount());
    // CSS 5
    aCSS = CSSReader.readFromString(CSS5, ECSSVersion.CSS30);
    assertNotNull(aCSS);
    aVisitor = new MockCountingUrlVisitor();
    CSSVisitor.visitCSSUrl(aCSS, aVisitor);
    assertEquals(0, aVisitor.getCount());
    // CSS 6
    aCSS = CSSReader.readFromString(CSS6, ECSSVersion.CSS30);
    assertNotNull(aCSS);
    aVisitor = new MockCountingUrlVisitor();
    CSSVisitor.visitCSSUrl(aCSS, aVisitor);
    assertEquals(3, aVisitor.getCount());
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) Test(org.junit.Test)

Example 53 with CascadingStyleSheet

use of com.helger.css.decl.CascadingStyleSheet in project ph-css by phax.

the class WikiVisitSelectors method readAllSelectors.

public void readAllSelectors() {
    final String sStyle = "blockquote p,\r\n" + "blockquote ul,\r\n" + "blockquote ol {\r\n" + "  line-height:normal;\r\n" + "  font-style:italic;\r\n" + "}\r\n" + "\r\n" + "a { color:#FFEA6F; }\r\n" + "\r\n" + "a:hover { text-decoration:none; }\r\n" + "\r\n" + "img { border:none; }";
    final CascadingStyleSheet aCSS = CSSReader.readFromString(sStyle, ECSSVersion.CSS30);
    final ICommonsList<String> aAllSelectors = new CommonsArrayList<>();
    CSSVisitor.visitCSS(aCSS, new DefaultCSSVisitor() {

        @Override
        public void onStyleRuleSelector(@Nonnull final CSSSelector aSelector) {
            aAllSelectors.add(aSelector.getAsCSSString(new CSSWriterSettings(ECSSVersion.CSS30)));
        }
    });
    System.out.println(aAllSelectors);
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) CSSWriterSettings(com.helger.css.writer.CSSWriterSettings) DefaultCSSVisitor(com.helger.css.decl.visit.DefaultCSSVisitor) CSSSelector(com.helger.css.decl.CSSSelector) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList)

Example 54 with CascadingStyleSheet

use of com.helger.css.decl.CascadingStyleSheet in project ph-css by phax.

the class MainReadAllCSSOnDisc method main.

@SuppressFBWarnings("DMI_HARDCODED_ABSOLUTE_FILENAME")
public static void main(final String[] args) {
    int nFilesOK = 0;
    int nFilesError = 0;
    final ICommonsOrderedMap<File, ParseException> aErrors = new CommonsLinkedHashMap<>();
    final Wrapper<File> aCurrentFile = new Wrapper<>();
    final ICSSParseExceptionCallback aHdl = ex -> aErrors.put(aCurrentFile.get(), ex);
    for (final File aFile : new FileSystemRecursiveIterator(new File("/")).withFilter(IFileFilter.filenameEndsWith(".css"))) {
        if (false)
            s_aLogger.info(aFile.getAbsolutePath());
        aCurrentFile.set(aFile);
        final CascadingStyleSheet aCSS = CSSReader.readFromFile(aFile, StandardCharsets.UTF_8, ECSSVersion.CSS30, aHdl);
        if (aCSS == null) {
            nFilesError++;
            s_aLogger.warn("  " + aFile.getAbsolutePath() + " failed!");
        } else
            nFilesOK++;
    }
    s_aLogger.info("Done");
    for (final Map.Entry<File, ParseException> aEntry : aErrors.entrySet()) s_aLogger.info("  " + aEntry.getKey().getAbsolutePath() + ":\n" + aEntry.getValue().getMessage() + "\n");
    s_aLogger.info("OK: " + nFilesOK + "; Error: " + nFilesError);
}
Also used : Logger(org.slf4j.Logger) ICSSParseExceptionCallback(com.helger.css.handler.ICSSParseExceptionCallback) CSSReader(com.helger.css.reader.CSSReader) LoggerFactory(org.slf4j.LoggerFactory) ICommonsOrderedMap(com.helger.commons.collection.impl.ICommonsOrderedMap) ECSSVersion(com.helger.css.ECSSVersion) ParseException(com.helger.css.parser.ParseException) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) FileSystemRecursiveIterator(com.helger.commons.io.file.FileSystemRecursiveIterator) IFileFilter(com.helger.commons.io.file.IFileFilter) Wrapper(com.helger.commons.wrapper.Wrapper) Map(java.util.Map) CommonsLinkedHashMap(com.helger.commons.collection.impl.CommonsLinkedHashMap) CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) Wrapper(com.helger.commons.wrapper.Wrapper) CommonsLinkedHashMap(com.helger.commons.collection.impl.CommonsLinkedHashMap) ICSSParseExceptionCallback(com.helger.css.handler.ICSSParseExceptionCallback) CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) FileSystemRecursiveIterator(com.helger.commons.io.file.FileSystemRecursiveIterator) ParseException(com.helger.css.parser.ParseException) File(java.io.File) ICommonsOrderedMap(com.helger.commons.collection.impl.ICommonsOrderedMap) Map(java.util.Map) CommonsLinkedHashMap(com.helger.commons.collection.impl.CommonsLinkedHashMap) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 55 with CascadingStyleSheet

use of com.helger.css.decl.CascadingStyleSheet in project ph-css by phax.

the class WikiCreateFontFaceRuleFuncTest method test.

@Test
public void test() {
    final CascadingStyleSheet aCSS = WikiCreateFontFaceRule.createFontFace("Your \"typeface\"", "local font name", "folder/", "myfont");
    final String sCSS = new CSSWriter(ECSSVersion.CSS30).getCSSAsString(aCSS);
    System.out.println(sCSS);
    final CascadingStyleSheet aCSS2 = CSSReader.readFromString(sCSS, ECSSVersion.CSS30);
    assertNotNull(aCSS2);
    assertEquals(aCSS, aCSS2);
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) CSSWriter(com.helger.css.writer.CSSWriter) Test(org.junit.Test)

Aggregations

CascadingStyleSheet (com.helger.css.decl.CascadingStyleSheet)64 Test (org.junit.Test)50 CSSWriter (com.helger.css.writer.CSSWriter)25 LoggingCSSParseErrorHandler (com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler)19 CSSReaderSettings (com.helger.css.reader.CSSReaderSettings)14 File (java.io.File)14 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)13 IReadableResource (com.helger.commons.io.resource.IReadableResource)12 CSSWriterSettings (com.helger.css.writer.CSSWriterSettings)10 Charset (java.nio.charset.Charset)10 ECSSVersion (com.helger.css.ECSSVersion)8 FileSystemRecursiveIterator (com.helger.commons.io.file.FileSystemRecursiveIterator)6 CSSDeclaration (com.helger.css.decl.CSSDeclaration)5 CSSExpressionMemberTermURI (com.helger.css.decl.CSSExpressionMemberTermURI)5 ICSSTopLevelRule (com.helger.css.decl.ICSSTopLevelRule)5 CSSImportRule (com.helger.css.decl.CSSImportRule)4 CSSExpressionMemberFunction (com.helger.css.decl.CSSExpressionMemberFunction)3 CSSStyleRule (com.helger.css.decl.CSSStyleRule)3 DefaultCSSUrlVisitor (com.helger.css.decl.visit.DefaultCSSUrlVisitor)3 CollectingCSSParseErrorHandler (com.helger.css.reader.errorhandler.CollectingCSSParseErrorHandler)3