Java Code Examples for com.helger.css.ECSSVersion

The following examples show how to use com.helger.css.ECSSVersion. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: ph-css   Source File: CSSReader30SpecialFuncTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testReadWithBOM ()
{
  final String sCSSBase = "/* comment */.class{color:red}.class{color:blue}";
  for (final EUnicodeBOM eBOM : EUnicodeBOM.values ())
  {
    final Charset aDeterminedCharset = eBOM.getCharset ();
    if (aDeterminedCharset != null)
    {
      final CascadingStyleSheet aCSS = CSSReader.readFromStream (new ByteArrayInputStreamProvider (ArrayHelper.getConcatenated (eBOM.getAllBytes (),
                                                                                                                                sCSSBase.getBytes (aDeterminedCharset))),
                                                                 aDeterminedCharset,
                                                                 ECSSVersion.CSS30,
                                                                 new DoNothingCSSParseErrorHandler ());
      assertNotNull ("Failed to read with BOM " + eBOM, aCSS);
      assertEquals (".class{color:red}.class{color:blue}",
                    new CSSWriter (ECSSVersion.CSS30, true).getCSSAsString (aCSS));
    }
  }
}
 
Example 2
Source Project: ph-css   Source File: CSSShortHandDescriptorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testBackground1 ()
{
  final CSSShortHandDescriptor aSHD = CSSShortHandRegistry.getShortHandDescriptor (ECSSProperty.BACKGROUND);
  assertNotNull (aSHD);

  final CSSDeclaration aDecl = CSSReaderDeclarationList.readFromString ("background: #ff0000 url('grafik.png') left top / 180px 100px no-repeat",
                                                                        ECSSVersion.CSS30)
                                                       .getDeclarationAtIndex (0);
  assertNotNull (aDecl);

  final List <CSSDeclaration> aSplittedDecls = aSHD.getSplitIntoPieces (aDecl);
  assertNotNull (aSplittedDecls);
  assertEquals (8, aSplittedDecls.size ());

  assertEquals ("background-color:#ff0000", aSplittedDecls.get (0).getAsCSSString (CWS));
  assertEquals ("background-image:url(grafik.png)", aSplittedDecls.get (1).getAsCSSString (CWS));
  assertEquals ("background-position:left top", aSplittedDecls.get (2).getAsCSSString (CWS));
  assertEquals ("background-size:180px 100px", aSplittedDecls.get (3).getAsCSSString (CWS));
  assertEquals ("background-repeat:no-repeat", aSplittedDecls.get (4).getAsCSSString (CWS));
  assertEquals ("background-attachment:scroll", aSplittedDecls.get (5).getAsCSSString (CWS));
  assertEquals ("background-clip:border-box", aSplittedDecls.get (6).getAsCSSString (CWS));
  assertEquals ("background-origin:padding-box", aSplittedDecls.get (7).getAsCSSString (CWS));
}
 
Example 3
Source Project: ph-css   Source File: CSSVisitor30FuncTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testVisitContent30 ()
{
  for (final File aFile : new FileSystemRecursiveIterator (new File ("src/test/resources/testfiles/css30/good")).withFilter (IFileFilter.filenameEndsWith (".css")))
  {
    final String sKey = aFile.getAbsolutePath ();
    if (true)
      LOGGER.info (sKey);
    final CascadingStyleSheet aCSS = CSSReader.readFromFile (aFile,
                                                             new CSSReaderSettings ().setFallbackCharset (StandardCharsets.UTF_8)
                                                                                     .setCSSVersion (ECSSVersion.CSS30)
                                                                                     .setCustomErrorHandler (new LoggingCSSParseErrorHandler ())
                                                                                     .setBrowserCompliantMode (true));
    assertNotNull (sKey, aCSS);
    CSSVisitor.visitCSSUrl (aCSS, new MockUrlVisitor (sKey));
  }
}
 
Example 4
Source Project: ph-css   Source File: CSSReaderDeclarationList.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Check if the passed CSS resource can be parsed without error
 *
 * @param aRes
 *        The resource to be parsed. May not be <code>null</code>.
 * @param aCharset
 *        The charset to be used for reading the CSS file. May not be
 *        <code>null</code>.
 * @param eVersion
 *        The CSS version to be used for scanning. May not be
 *        <code>null</code>.
 * @return <code>true</code> if the file can be parsed without error,
 *         <code>false</code> if not
 */
public static boolean isValidCSS (@Nonnull final IReadableResource aRes,
                                  @Nonnull final Charset aCharset,
                                  @Nonnull final ECSSVersion eVersion)
{
  ValueEnforcer.notNull (aRes, "Resource");
  ValueEnforcer.notNull (aCharset, "Charset");
  ValueEnforcer.notNull (eVersion, "Version");

  final Reader aReader = aRes.getReader (aCharset);
  if (aReader == null)
  {
    LOGGER.warn ("Failed to open CSS reader " + aRes);
    return false;
  }
  return isValidCSS (aReader, eVersion);
}
 
Example 5
Source Project: ph-css   Source File: CSSShortHandDescriptorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testMargin2 ()
{
  final CSSShortHandDescriptor aSHD = CSSShortHandRegistry.getShortHandDescriptor (ECSSProperty.MARGIN);
  assertNotNull (aSHD);

  final CSSDeclaration aDecl = CSSReaderDeclarationList.readFromString ("margin:1px 3px", ECSSVersion.CSS30)
                                                       .getDeclarationAtIndex (0);
  assertNotNull (aDecl);

  final List <CSSDeclaration> aSplittedDecls = aSHD.getSplitIntoPieces (aDecl);
  assertNotNull (aSplittedDecls);
  assertEquals (4, aSplittedDecls.size ());

  assertEquals ("margin-top:1px", aSplittedDecls.get (0).getAsCSSString (CWS));
  assertEquals ("margin-right:3px", aSplittedDecls.get (1).getAsCSSString (CWS));
  assertEquals ("margin-bottom:1px", aSplittedDecls.get (2).getAsCSSString (CWS));
  assertEquals ("margin-left:3px", aSplittedDecls.get (3).getAsCSSString (CWS));
}
 
Example 6
Source Project: ph-css   Source File: MediaQueryTools.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Utility method to convert a media query string to a structured list of
 * {@link CSSMediaQuery} objects.
 *
 * @param sMediaQuery
 *        The media query string to parse. May be <code>null</code>.
 * @param eVersion
 *        The CSS version to use. May not be <code>null</code>.
 * @return <code>null</code> if the passed media query is <code>null</code> or
 *         empty or not parsable.
 */
@Nullable
public static ICommonsList <CSSMediaQuery> parseToMediaQuery (@Nullable final String sMediaQuery,
                                                              @Nonnull final ECSSVersion eVersion)
{
  if (StringHelper.hasNoText (sMediaQuery))
    return null;

  final String sCSS = "@media " + sMediaQuery + " {}";
  final CascadingStyleSheet aCSS = CSSReader.readFromString (sCSS, eVersion);
  if (aCSS == null)
    return null;

  final CSSMediaRule aMediaRule = aCSS.getAllMediaRules ().get (0);
  return aMediaRule.getAllMediaQueries ();
}
 
Example 7
Source Project: ph-css   Source File: Issue8Test.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testIssue8 ()
{
  final IReadableResource aRes = new ClassPathResource ("testfiles/css30/good/issue8.css");
  assertTrue (aRes.exists ());
  final CascadingStyleSheet aCSS = CSSReader.readFromStream (aRes,
                                                             StandardCharsets.UTF_8,
                                                             ECSSVersion.CSS30,
                                                             new LoggingCSSParseErrorHandler ());
  assertNotNull (aCSS);

  assertEquals (1, aCSS.getStyleRuleCount ());
  final CSSStyleRule aStyleRule = aCSS.getStyleRuleAtIndex (0);
  assertNotNull (aStyleRule);

  assertEquals (4, aStyleRule.getDeclarationCount ());
}
 
Example 8
Source Project: ph-css   Source File: Issue41Test.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testIssue3 ()
{
  final String css = "------- This is bad\r\n" +
                     "\r\n" +
                     ".someRule {\r\n" +
                     "   color:red;\r\n" +
                     "}\r\n" +
                     "\r\n" +
                     ".someOtherRule {\r\n" +
                     "   color:blue;\r\n" +
                     "}";
  final CSSReaderSettings aSettings = new CSSReaderSettings ().setCSSVersion (ECSSVersion.LATEST)
                                                              .setBrowserCompliantMode (true)
                                                              .setCustomErrorHandler (new LoggingCSSParseErrorHandler ())
                                                              .setCustomExceptionHandler (new LoggingCSSParseExceptionCallback ())
                                                              .setInterpretErrorHandler (new LoggingCSSInterpretErrorHandler ());
  final CascadingStyleSheet aCSS = CSSReader.readFromStringReader (css, aSettings);
  assertNotNull (aCSS);

  final CSSWriterSettings aCWS = new CSSWriterSettings (ECSSVersion.LATEST).setNewLineMode (ENewLineMode.WINDOWS);
  assertEquals (".someOtherRule { color:blue; }\r\n",
                new CSSWriter (aCWS).setWriteHeaderText (false).setWriteFooterText (false).getCSSAsString (aCSS));
}
 
Example 9
Source Project: ph-css   Source File: CSSShortHandDescriptorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testPadding4 ()
{
  final CSSShortHandDescriptor aSHD = CSSShortHandRegistry.getShortHandDescriptor (ECSSProperty.PADDING);
  assertNotNull (aSHD);

  final CSSDeclaration aDecl = CSSReaderDeclarationList.readFromString ("padding:1px 3px 5px 7px", ECSSVersion.CSS30)
                                                       .getDeclarationAtIndex (0);
  assertNotNull (aDecl);

  final List <CSSDeclaration> aSplittedDecls = aSHD.getSplitIntoPieces (aDecl);
  assertNotNull (aSplittedDecls);
  assertEquals (4, aSplittedDecls.size ());

  assertEquals ("padding-top:1px", aSplittedDecls.get (0).getAsCSSString (CWS));
  assertEquals ("padding-right:3px", aSplittedDecls.get (1).getAsCSSString (CWS));
  assertEquals ("padding-bottom:5px", aSplittedDecls.get (2).getAsCSSString (CWS));
  assertEquals ("padding-left:7px", aSplittedDecls.get (3).getAsCSSString (CWS));
}
 
Example 10
Source Project: ph-css   Source File: CSSShortHandDescriptorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testBorder3a ()
{
  final CSSShortHandDescriptor aSHD = CSSShortHandRegistry.getShortHandDescriptor (ECSSProperty.BORDER);
  assertNotNull (aSHD);

  final CSSDeclaration aDecl = CSSReaderDeclarationList.readFromString ("border:dashed 1px red", ECSSVersion.CSS30)
                                                       .getDeclarationAtIndex (0);
  assertNotNull (aDecl);

  final List <CSSDeclaration> aSplittedDecls = aSHD.getSplitIntoPieces (aDecl);
  assertNotNull (aSplittedDecls);
  assertEquals (3, aSplittedDecls.size ());
  assertEquals ("border-style:dashed", aSplittedDecls.get (0).getAsCSSString (CWS));
  assertEquals ("border-width:1px", aSplittedDecls.get (1).getAsCSSString (CWS));
  assertEquals ("border-color:red", aSplittedDecls.get (2).getAsCSSString (CWS));
}
 
Example 11
Source Project: ph-css   Source File: CSSWriterFuncTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testRead30Write21 () throws IOException
{
  for (final File aFile : new FileSystemRecursiveIterator (new File ("src/test/resources/testfiles/css30/good/artificial")).withFilter (IFileFilter.filenameEndsWith (".css")))
  {
    final String sKey = aFile.getAbsolutePath ();
    try
    {
      // read and interpret CSS 3.0
      final CascadingStyleSheet aCSS = CSSReader.readFromFile (aFile, StandardCharsets.UTF_8, ECSSVersion.CSS30);
      assertNotNull (sKey, aCSS);

      // write to CSS 2.1
      final NonBlockingStringWriter aSW = new NonBlockingStringWriter ();
      new CSSWriter (ECSSVersion.CSS21).writeCSS (aCSS, aSW);

      // This should throw an error
      fail (sKey + " should have thrown an exception but got: " + aSW.getAsString ());
    }
    catch (final IllegalStateException ex)
    {}
  }
}
 
Example 12
Source Project: ph-css   Source File: CSSWriterFuncTest.java    License: Apache License 2.0 6 votes vote down vote up
private void _testMe (@Nonnull final File aFile, @Nonnull final ECSSVersion eVersion)
{
  if (false)
    LOGGER.info (aFile.getAbsolutePath ());

  // read and interpret
  final CascadingStyleSheet aCSS = CSSReader.readFromFile (aFile, StandardCharsets.UTF_8, eVersion);
  assertNotNull (aFile.getAbsolutePath (), aCSS);

  // Both normal and optimized!
  for (int i = 0; i < 2; ++i)
  {
    // write to buffer
    final String sCSS = new CSSWriter (eVersion, i == 1).getCSSAsString (aCSS);
    if (false)
      System.out.println ("--" + i + "--\n" + sCSS);

    // read again from buffer
    assertEquals (aFile.getAbsolutePath () +
                  (i == 0 ? " unoptimized" : " optimized"),
                  aCSS,
                  CSSReader.readFromString (sCSS, eVersion));
  }
}
 
Example 13
Source Project: ph-css   Source File: CSSShortHandDescriptorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testBorder1 ()
{
  final CSSShortHandDescriptor aSHD = CSSShortHandRegistry.getShortHandDescriptor (ECSSProperty.BORDER);
  assertNotNull (aSHD);

  final CSSDeclaration aDecl = CSSReaderDeclarationList.readFromString ("border:1px", ECSSVersion.CSS30)
                                                       .getDeclarationAtIndex (0);
  assertNotNull (aDecl);

  final List <CSSDeclaration> aSplittedDecls = aSHD.getSplitIntoPieces (aDecl);
  assertNotNull (aSplittedDecls);
  assertEquals (3, aSplittedDecls.size ());
  assertEquals ("border-width:1px", aSplittedDecls.get (0).getAsCSSString (CWS));
  assertEquals ("border-style:solid", aSplittedDecls.get (1).getAsCSSString (CWS));
  assertEquals ("border-color:black", aSplittedDecls.get (2).getAsCSSString (CWS));
}
 
Example 14
Source Project: ph-css   Source File: CSSWriterSettingsTest.java    License: Apache License 2.0 6 votes vote down vote up
private static void _checkDefault (@Nonnull final ICSSWriterSettings aSettings)
{
  assertSame (ECSSVersion.CSS30, aSettings.getCSSVersion ());
  assertTrue (CSSWriterSettings.DEFAULT_OPTIMIZED_OUTPUT == aSettings.isOptimizedOutput ());
  assertTrue (CSSWriterSettings.DEFAULT_REMOVE_UNNECESSARY_CODE == aSettings.isRemoveUnnecessaryCode ());
  assertSame (CSSWriterSettings.DEFAULT_NEW_LINE_MODE, aSettings.getNewLineMode ());
  assertEquals (CSSWriterSettings.DEFAULT_INDENT, aSettings.getIndent (1));
  assertTrue (CSSWriterSettings.DEFAULT_QUOTE_URLS == aSettings.isQuoteURLs ());
  assertTrue (CSSWriterSettings.DEFAULT_WRITE_NAMESPACE_RULES == aSettings.isWriteNamespaceRules ());
  assertTrue (CSSWriterSettings.DEFAULT_WRITE_FONT_FACE_RULES == aSettings.isWriteFontFaceRules ());
  assertTrue (CSSWriterSettings.DEFAULT_WRITE_KEYFRAMES_RULES == aSettings.isWriteKeyframesRules ());
  assertTrue (CSSWriterSettings.DEFAULT_WRITE_MEDIA_RULES == aSettings.isWriteMediaRules ());
  assertTrue (CSSWriterSettings.DEFAULT_WRITE_PAGE_RULES == aSettings.isWritePageRules ());
  assertTrue (CSSWriterSettings.DEFAULT_WRITE_VIEWPORT_RULES == aSettings.isWriteViewportRules ());
  assertTrue (CSSWriterSettings.DEFAULT_WRITE_SUPPORTS_RULES == aSettings.isWriteSupportsRules ());
  assertTrue (CSSWriterSettings.DEFAULT_WRITE_UNKNOWN_RULES == aSettings.isWriteUnknownRules ());
}
 
Example 15
Source Project: ph-css   Source File: WikiVisitFromHtml.java    License: Apache License 2.0 6 votes vote down vote up
public static void readFromStyleAttributeWithVisitor ()
{
  final String sStyle = "color:red; background:fixed !important";
  final CSSDeclarationList aDeclList = CSSReaderDeclarationList.readFromString (sStyle, ECSSVersion.CSS30);
  if (aDeclList == null)
    throw new IllegalStateException ("Failed to parse CSS: " + sStyle);
  // Create a custom visitor
  final ICSSVisitor aVisitor = new DefaultCSSVisitor ()
  {
    @Override
    public void onDeclaration (@Nonnull final CSSDeclaration aDeclaration)
    {
      System.out.println (aDeclaration.getProperty () +
                          ": " +
                          aDeclaration.getExpression ().getAsCSSString (new CSSWriterSettings (ECSSVersion.CSS30)) +
                          (aDeclaration.isImportant () ? " (important)" : " (not important)"));
    }
  };
  CSSVisitor.visitAllDeclarations (aDeclList, aVisitor);
}
 
Example 16
Source Project: ph-css   Source File: CSSHandler.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Create a {@link CSSDeclarationList} object from a parsed object.
 *
 * @param eVersion
 *        The CSS version to use. May not be <code>null</code>.
 * @param aErrorHandler
 *        The error handler to be used. May not be <code>null</code>.
 * @param bUseSourceLocation
 *        <code>true</code> to keep the source location, <code>false</code> to
 *        ignore the source location. Disabling the source location may be a
 *        performance improvement.
 * @param aNode
 *        The parsed CSS object to read. May not be <code>null</code>.
 * @return Never <code>null</code>.
 * @since 6.1.3
 */
@Nonnull
public static CSSDeclarationList readDeclarationListFromNode (@Nonnull final ECSSVersion eVersion,
                                                              @Nonnull final ICSSInterpretErrorHandler aErrorHandler,
                                                              final boolean bUseSourceLocation,
                                                              @Nonnull final CSSNode aNode)
{
  ValueEnforcer.notNull (eVersion, "Version");
  ValueEnforcer.notNull (aNode, "Node");
  if (!ECSSNodeType.STYLEDECLARATIONLIST.isNode (aNode, eVersion))
    throw new CSSHandlingException (aNode, "Passed node is not a style declaration node!");
  ValueEnforcer.notNull (aErrorHandler, "ErrorHandler");

  return new CSSNodeToDomainObject (eVersion,
                                    aErrorHandler,
                                    bUseSourceLocation).createDeclarationListFromNode (aNode);
}
 
Example 17
Source Project: ph-css   Source File: ParserCSS30Test.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void test2 () throws ParseException
{
  final ParserCSS30TokenManager aTokenHdl = new ParserCSS30TokenManager (new CSSCharStream (new NonBlockingStringReader (CSS2)));
  final ParserCSS30 aParser = new ParserCSS30 (aTokenHdl);
  aParser.disable_tracing ();
  final CSSNode aNode = aParser.styleSheet ();
  assertNotNull (aNode);

  final CascadingStyleSheet aCSS = CSSHandler.readCascadingStyleSheetFromNode (ECSSVersion.CSS30,
                                                                               CSSReader.getDefaultInterpretErrorHandler (),
                                                                               true,
                                                                               aNode);
  assertNotNull (aCSS);

  for (final ICSSTopLevelRule aTopLevelRule : aCSS.getAllFontFaceRules ())
    assertTrue (aCSS.removeRule (aTopLevelRule).isChanged ());
}
 
Example 18
Source Project: ph-css   Source File: CSSWriterFuncTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testCompressCSS_Size ()
{
  final CascadingStyleSheet aCSS = CSSReader.readFromStream (new ClassPathResource ("/testfiles/css21/good/phloc/test/content.css"),
                                                             StandardCharsets.UTF_8,
                                                             ECSSVersion.CSS30);
  assertNotNull (aCSS);

  // Only whitespace optimization
  final CSSWriterSettings aSettings = new CSSWriterSettings (ECSSVersion.CSS21, true);
  String sContent = new CSSWriter (aSettings).getCSSAsString (aCSS);
  assertEquals (2846, sContent.length ());

  // Also remove empty declarations
  aSettings.setRemoveUnnecessaryCode (true);
  sContent = new CSSWriter (aSettings).getCSSAsString (aCSS);
  assertEquals (2839, sContent.length ());
}
 
Example 19
Source Project: ph-css   Source File: CSSImportRuleTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testCreate ()
{
  final CSSImportRule aImportRule = new CSSImportRule ("a.gif");
  final CSSWriterSettings aSettings = new CSSWriterSettings (ECSSVersion.CSS30, false);
  assertEquals ("@import url(a.gif);\n", aImportRule.getAsCSSString (aSettings));
  aSettings.setQuoteURLs (true);
  assertEquals ("@import url('a.gif');\n", aImportRule.getAsCSSString (aSettings));
}
 
Example 20
Source Project: ph-css   Source File: CSSRGBATest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testBasic ()
{
  final CSSWriterSettings aSettings = new CSSWriterSettings (ECSSVersion.CSS30, false);
  final CSSRGBA aColor = new CSSRGBA (1, 2, 3, 0.5f);
  assertEquals ("rgba(1,2,3,0.5)", aColor.getAsCSSString (aSettings));

  CommonsTestHelper.testDefaultImplementationWithEqualContentObject (aColor, new CSSRGBA (aColor));
  CommonsTestHelper.testDefaultImplementationWithEqualContentObject (aColor, new CSSRGBA (1, 2, 3, 0.5f));
  CommonsTestHelper.testDefaultImplementationWithDifferentContentObject (aColor, new CSSRGBA (0, 2, 3, 0.5f));
  CommonsTestHelper.testDefaultImplementationWithDifferentContentObject (aColor, new CSSRGBA (1, 0, 3, 0.5f));
  CommonsTestHelper.testDefaultImplementationWithDifferentContentObject (aColor, new CSSRGBA (1, 2, 0, 0.5f));
  CommonsTestHelper.testDefaultImplementationWithDifferentContentObject (aColor, new CSSRGBA (1, 2, 3, 0f));
}
 
Example 21
Source Project: ph-css   Source File: Issue34Test.java    License: Apache License 2.0 5 votes vote down vote up
@Test
@Ignore ("TODO")
public void testIssue ()
{
  final String css = ".pen {background-color:red} {* some incorrect block *} .pen {background-color: blue}";
  final CSSReaderSettings aSettings = new CSSReaderSettings ().setCSSVersion (ECSSVersion.LATEST)
                                                              .setBrowserCompliantMode (true)
                                                              .setCustomErrorHandler (new LoggingCSSParseErrorHandler ())
                                                              .setCustomExceptionHandler (new LoggingCSSParseExceptionCallback ());
  final CascadingStyleSheet cascadingStyleSheet = CSSReader.readFromStringStream (css, aSettings);
  assertNotNull (cascadingStyleSheet);
  final CSSWriter writer = new CSSWriter (new CSSWriterSettings (ECSSVersion.LATEST, true));
  LOGGER.info (writer.getCSSAsString (cascadingStyleSheet));
}
 
Example 22
Source Project: ph-css   Source File: CSSWriterSettings.java    License: Apache License 2.0 5 votes vote down vote up
public void checkVersionRequirements (@Nonnull final ICSSVersionAware aCSSObject)
{
  final ECSSVersion eMinCSSVersion = aCSSObject.getMinimumCSSVersion ();
  if (m_eCSSVersion.compareTo (eMinCSSVersion) < 0)
    throw new IllegalStateException ("This object cannot be serialized to CSS version " +
                                     m_eCSSVersion.getVersion ().getAsString () +
                                     " but requires at least " +
                                     eMinCSSVersion.getVersion ().getAsString ());
}
 
Example 23
Source Project: ph-css   Source File: WikiVisitFromHtml.java    License: Apache License 2.0 5 votes vote down vote up
public static void readFromStyleAttributeWithAPI ()
{
  final String sStyle = "color:red; background:fixed !important";
  final CSSDeclarationList aDeclList = CSSReaderDeclarationList.readFromString (sStyle, ECSSVersion.CSS30);
  if (aDeclList == null)
    throw new IllegalStateException ("Failed to parse CSS: " + sStyle);
  // For all contained declarations
  for (final CSSDeclaration aDeclaration : aDeclList.getAllDeclarations ())
    System.out.println (aDeclaration.getProperty () +
                        ": " +
                        aDeclaration.getExpression ().getAsCSSString (new CSSWriterSettings (ECSSVersion.CSS30)) +
                        (aDeclaration.isImportant () ? " (important)" : " (not important)"));
}
 
Example 24
Source Project: ph-css   Source File: Issue33Test.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testIssue ()
{
  // No log message may be issued in this test!

  final String css = "@media \\0screen\\,screen\\9 {.test {margin-left: 0px}}";
  final CSSReaderSettings aSettings = new CSSReaderSettings ().setCSSVersion (ECSSVersion.LATEST)
                                                              .setBrowserCompliantMode (true)
                                                              .setInterpretErrorHandler (new DoNothingCSSInterpretErrorHandler ());
  final CascadingStyleSheet cascadingStyleSheet = CSSReader.readFromStringStream (css, aSettings);
  final CSSWriter writer = new CSSWriter (new CSSWriterSettings (ECSSVersion.LATEST, true));
  assertEquals ("@media \\0screen\\,screen\\9 {.test{margin-left:0}}", writer.getCSSAsString (cascadingStyleSheet));
}
 
Example 25
Source Project: ph-css   Source File: WikiVisitUrls.java    License: Apache License 2.0 5 votes vote down vote up
public void readFromStyleAttributeWithAPI ()
{
  final String sStyle = "@import 'foobar.css';\n" +
                        "div{background:fixed url(a.gif) !important;}\n" +
                        "span { background-image:url('/my/folder/b.gif');}";
  final CascadingStyleSheet aCSS = CSSReader.readFromString (sStyle, ECSSVersion.CSS30);
  CSSVisitor.visitCSSUrl (aCSS, new DefaultCSSUrlVisitor ()
  {
    // Called for each import
    @Override
    public void onImport (@Nonnull final CSSImportRule aImportRule)
    {
      System.out.println ("Import: " +
                          aImportRule.getLocationString () +
                          " - " +
                          getSourceLocationString (aImportRule.getSourceLocation ()));
    }

    // Call for URLs outside of URLs
    @Override
    public void onUrlDeclaration (@Nullable final ICSSTopLevelRule aTopLevelRule,
                                  @Nonnull final CSSDeclaration aDeclaration,
                                  @Nonnull final CSSExpressionMemberTermURI aURITerm)
    {
      System.out.println (aDeclaration.getProperty () +
                          " - references: " +
                          aURITerm.getURIString () +
                          " - " +
                          getSourceLocationString (aURITerm.getSourceLocation ()));
    }
  });
}
 
Example 26
Source Project: ph-css   Source File: CSSReaderDeclarationList.java    License: Apache License 2.0 5 votes vote down vote up
@Nullable
public static CSSDeclarationList readFromString (@Nonnull final String sCSS,
                                                 @Nonnull final ECSSVersion eVersion,
                                                 @Nullable final ICSSParseErrorHandler aCustomErrorHandler)
{
  return readFromReader (new NonBlockingStringReader (sCSS),
                         new CSSReaderSettings ().setCSSVersion (eVersion)
                                                 .setCustomErrorHandler (aCustomErrorHandler));
}
 
Example 27
Source Project: ph-css   Source File: CSSReaderDeclarationList.java    License: Apache License 2.0 5 votes vote down vote up
@Nullable
public static CSSDeclarationList readFromString (@Nonnull final String sCSS,
                                                 @Nonnull final ECSSVersion eVersion,
                                                 @Nullable final ICSSParseExceptionCallback aCustomExceptionHandler)
{
  return readFromReader (new NonBlockingStringReader (sCSS),
                         new CSSReaderSettings ().setCSSVersion (eVersion)
                                                 .setCustomExceptionHandler (aCustomExceptionHandler));
}
 
Example 28
Source Project: ph-css   Source File: CSSReaderDeclarationList.java    License: Apache License 2.0 5 votes vote down vote up
@Nullable
public static CSSDeclarationList readFromFile (@Nonnull final File aFile,
                                               @Nonnull final Charset aCharset,
                                               @Nonnull final ECSSVersion eVersion)
{
  return readFromReader (new FileSystemResource (aFile).getReader (aCharset),
                         new CSSReaderSettings ().setCSSVersion (eVersion));
}
 
Example 29
Source Project: ph-css   Source File: CSSSelectorTest.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
private static CSSSelector _parse (@Nonnull final String sCSS)
{
  final CascadingStyleSheet aCSS = CSSReader.readFromString (sCSS, ECSSVersion.CSS30);
  assertNotNull (sCSS, aCSS);
  assertTrue (aCSS.hasStyleRules ());
  assertEquals (1, aCSS.getStyleRuleCount ());
  final CSSStyleRule aStyle = aCSS.getAllStyleRules ().get (0);
  assertNotNull (sCSS, aStyle);
  assertTrue (aStyle.hasSelectors ());
  assertEquals (1, aStyle.getSelectorCount ());
  final CSSSelector aSel = aStyle.getSelectorAtIndex (0);
  assertNotNull (sCSS, aSel);
  return aSel;
}
 
Example 30
Source Project: ph-css   Source File: CSSUnknownRuleTest.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
private static CSSUnknownRule _parse (@Nonnull final String sCSS)
{
  final CascadingStyleSheet aCSS = CSSReader.readFromString (sCSS, ECSSVersion.CSS30);
  assertNotNull (sCSS, aCSS);
  assertTrue (aCSS.hasUnknownRules ());
  assertEquals (1, aCSS.getUnknownRuleCount ());
  final CSSUnknownRule ret = aCSS.getAllUnknownRules ().get (0);
  assertNotNull (ret);
  return ret;
}