com.helger.css.ECSSVersion Java Examples

The following examples show how to use com.helger.css.ECSSVersion. 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 File: CSSShortHandDescriptorTest.java    From ph-css with 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 #2
Source File: CSSReader30SpecialFuncTest.java    From ph-css with 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 #3
Source File: CSSHandler.java    From ph-css with 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 #4
Source File: ParserCSS30Test.java    From ph-css with 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 #5
Source File: CSSShortHandDescriptorTest.java    From ph-css with 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 #6
Source File: CSSWriterFuncTest.java    From ph-css with 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 #7
Source File: CSSVisitor30FuncTest.java    From ph-css with 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 #8
Source File: WikiVisitFromHtml.java    From ph-css with 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 #9
Source File: CSSReaderDeclarationList.java    From ph-css with 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 #10
Source File: CSSWriterSettingsTest.java    From ph-css with 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 #11
Source File: CSSShortHandDescriptorTest.java    From ph-css with 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 #12
Source File: MediaQueryTools.java    From ph-css with 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 #13
Source File: Issue8Test.java    From ph-css with 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 #14
Source File: Issue41Test.java    From ph-css with 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 #15
Source File: CSSShortHandDescriptorTest.java    From ph-css with 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 #16
Source File: CSSWriterFuncTest.java    From ph-css with 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 #17
Source File: CSSWriterFuncTest.java    From ph-css with 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 #18
Source File: CSSShortHandDescriptorTest.java    From ph-css with 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 #19
Source File: CSSNamespaceRuleTest.java    From ph-css with Apache License 2.0 5 votes vote down vote up
@Nonnull
private static CSSNamespaceRule _parse (@Nonnull final String sCSS)
{
  final CascadingStyleSheet aCSS = CSSReader.readFromString (sCSS, ECSSVersion.CSS30);
  assertNotNull (sCSS, aCSS);
  assertTrue (aCSS.hasNamespaceRules ());
  assertEquals (1, aCSS.getNamespaceRuleCount ());
  final CSSNamespaceRule ret = aCSS.getAllNamespaceRules ().get (0);
  assertNotNull (ret);
  return ret;
}
 
Example #20
Source File: ECSSColorTest.java    From ph-css with Apache License 2.0 5 votes vote down vote up
@Test
public void testAll ()
{
  final CSSWriter aWriter = new CSSWriter (ECSSVersion.CSS30);
  for (final ECSSColor eColor : ECSSColor.values ())
  {
    assertTrue (StringHelper.hasText (eColor.getName ()));
    assertTrue (CSSColorHelper.isColorValue (eColor.getName ()));

    final String sHex = eColor.getAsHexColorValue ();
    assertTrue (sHex, CSSColorHelper.isHexColorValue (sHex));

    final String sRGB = eColor.getAsRGBColorValue ();
    assertTrue (sRGB, CSSColorHelper.isRGBColorValue (sRGB));
    assertNotNull (eColor.getAsRGB ());
    assertEquals (sRGB, aWriter.getCSSAsString (eColor.getAsRGB ()));

    final String sRGBA = eColor.getAsRGBAColorValue (1f);
    assertTrue (sRGBA, CSSColorHelper.isRGBAColorValue (sRGBA));
    assertNotNull (eColor.getAsRGBA (1));
    assertEquals (sRGBA, aWriter.getCSSAsString (eColor.getAsRGBA (1)));

    final String sHSL = eColor.getAsHSLColorValue ();
    assertTrue (sHSL, CSSColorHelper.isHSLColorValue (sHSL));
    assertNotNull (eColor.getAsHSL ());
    assertEquals (sHSL, aWriter.getCSSAsString (eColor.getAsHSL ()));

    final String sHSLA = eColor.getAsHSLAColorValue (1f);
    assertTrue (sHSLA, CSSColorHelper.isHSLAColorValue (sHSLA));
    assertNotNull (eColor.getAsHSLA (1));
    assertEquals (sHSLA, aWriter.getCSSAsString (eColor.getAsHSLA (1)));

    assertSame (eColor, ECSSColor.getFromNameCaseInsensitiveOrNull (eColor.getName ()));
    assertTrue (ECSSColor.isDefaultColorName (eColor.getName ()));
  }
}
 
Example #21
Source File: CSSImportRuleTest.java    From ph-css with Apache License 2.0 5 votes vote down vote up
@Nonnull
private static CSSImportRule _parse (@Nonnull final String sCSS)
{
  final CascadingStyleSheet aCSS = CSSReader.readFromString (sCSS, ECSSVersion.CSS30);
  assertNotNull (sCSS, aCSS);
  assertTrue (aCSS.hasImportRules ());
  assertEquals (1, aCSS.getImportRuleCount ());
  final CSSImportRule ret = aCSS.getAllImportRules ().get (0);
  assertNotNull (sCSS, ret);
  return ret;
}
 
Example #22
Source File: CSSWriterTest.java    From ph-css with Apache License 2.0 5 votes vote down vote up
@Test
public void testNewLineModeWindows ()
{
  final CascadingStyleSheet aCSS = CSSReader.readFromString (CSS5, ECSSVersion.CSS30);
  assertNotNull (aCSS);

  // Non-optimized version
  CSSWriter aWriter = new CSSWriter (new CSSWriterSettings (ECSSVersion.CSS30).setOptimizedOutput (false)
                                                                              .setNewLineMode (ENewLineMode.WINDOWS)).setWriteHeaderText (true)
                                                                                                                     .setHeaderText ("Unit test");
  assertEquals ("/*\r\n" +
                " * Unit test\r\n" +
                " */\r\n" +
                "h1 {\r\n" +
                "  color:red;\r\n" +
                "  margin:1px;\r\n" +
                "}\r\n" +
                "\r\n" +
                "h2 {\r\n" +
                "  color:red;\r\n" +
                "  margin:1px;\r\n" +
                "}\r\n",
                aWriter.getCSSAsString (aCSS));

  // Optimized version
  aWriter = new CSSWriter (new CSSWriterSettings (ECSSVersion.CSS30).setOptimizedOutput (true)
                                                                    .setNewLineMode (ENewLineMode.WINDOWS)).setWriteHeaderText (true)
                                                                                                           .setHeaderText ("Unit test2");
  assertEquals ("/*\r\n" +
                " * Unit test2\r\n" +
                " */\r\n" +
                "h1{color:red;margin:1px}h2{color:red;margin:1px}",
                aWriter.getCSSAsString (aCSS));
}
 
Example #23
Source File: Issue36Test.java    From ph-css with Apache License 2.0 5 votes vote down vote up
@Test
public void testIssue ()
{
  final String css = "@media screen and (min-width: 768px) {.section {.\r\n" +
                     "    padding: 40px\r\n" +
                     "}\r\n" +
                     "\r\n" +
                     "}";
  final CSSReaderSettings aSettings = new CSSReaderSettings ().setCSSVersion (ECSSVersion.LATEST)
                                                              .setBrowserCompliantMode (true)
                                                              .setCustomErrorHandler (new DoNothingCSSParseErrorHandler ());
  final CascadingStyleSheet cascadingStyleSheet = CSSReader.readFromStringStream (css, aSettings);
  final CSSWriter writer = new CSSWriter (new CSSWriterSettings (ECSSVersion.LATEST, true));
  assertEquals ("@media screen and (min-width:768px){.section{}}", writer.getCSSAsString (cascadingStyleSheet));
}
 
Example #24
Source File: Issue9Test.java    From ph-css with Apache License 2.0 5 votes vote down vote up
@Test
public void testIssue9 ()
{
  // File starts (and ends) with an invalid comment
  final IReadableResource aRes = new ClassPathResource ("testfiles/css30/bad/issue9.css");
  assertTrue (aRes.exists ());
  final CascadingStyleSheet aCSS = CSSReader.readFromStream (aRes,
                                                             StandardCharsets.UTF_8,
                                                             ECSSVersion.CSS30,
                                                             new LoggingCSSParseErrorHandler ());
  assertNull (aCSS);
}
 
Example #25
Source File: CSSReader30FuncTest.java    From ph-css with Apache License 2.0 5 votes vote down vote up
@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/hacks2.css");
  final CascadingStyleSheet aCSS = CSSReader.readFromFile (aFile, aCharset, eVersion);
  assertNotNull (aCSS);

  final String sCSS = new CSSWriter (eVersion, false).getCSSAsString (aCSS);
  assertNotNull (sCSS);
  if (false)
    m_aLogger.info (sCSS);
}
 
Example #26
Source File: Issue22Test.java    From ph-css with Apache License 2.0 5 votes vote down vote up
@Test
public void testIssue ()
{
  // Multiple errors contained
  final IReadableResource aRes = new ClassPathResource ("testfiles/css30/good/issue22.css");
  assertTrue (aRes.exists ());
  final CascadingStyleSheet aCSS = CSSReader.readFromStream (aRes,
                                                             new CSSReaderSettings ().setFallbackCharset (StandardCharsets.UTF_8)
                                                                                     .setCustomErrorHandler (new LoggingCSSParseErrorHandler ()));
  assertNotNull (aCSS);
  if (false)
    System.out.println (new CSSWriter (ECSSVersion.CSS30).getCSSAsString (aCSS));
}
 
Example #27
Source File: CSSWriterTest.java    From ph-css with Apache License 2.0 5 votes vote down vote up
@Test
public void testNewLineModeMac ()
{
  final CascadingStyleSheet aCSS = CSSReader.readFromString (CSS5, ECSSVersion.CSS30);
  assertNotNull (aCSS);

  // Non-optimized version
  CSSWriter aWriter = new CSSWriter (new CSSWriterSettings (ECSSVersion.CSS30).setOptimizedOutput (false)
                                                                              .setNewLineMode (ENewLineMode.MAC)).setWriteHeaderText (true)
                                                                                                                 .setHeaderText ("Unit test");
  assertEquals ("/*\r" +
                " * Unit test\r" +
                " */\r" +
                "h1 {\r" +
                "  color:red;\r" +
                "  margin:1px;\r" +
                "}\r" +
                "\r" +
                "h2 {\r" +
                "  color:red;\r" +
                "  margin:1px;\r" +
                "}\r",
                aWriter.getCSSAsString (aCSS));

  // Optimized version
  aWriter = new CSSWriter (new CSSWriterSettings (ECSSVersion.CSS30).setOptimizedOutput (true)
                                                                    .setNewLineMode (ENewLineMode.MAC)).setWriteHeaderText (true)
                                                                                                       .setHeaderText ("Unit test2");
  assertEquals ("/*\r" +
                " * Unit test2\r" +
                " */\r" +
                "h1{color:red;margin:1px}h2{color:red;margin:1px}",
                aWriter.getCSSAsString (aCSS));
}
 
Example #28
Source File: WikiCreateFontFaceRuleFuncTest.java    From ph-css with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #29
Source File: Issue34Test.java    From ph-css with 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 #30
Source File: Issue3Test.java    From ph-css with Apache License 2.0 5 votes vote down vote up
@Nonnull
private static CascadingStyleSheet _parse (@Nonnull final String sCSS, final boolean bBrowserCompliantMode)
{
  if (true)
    LOGGER.info ("[Parsing] " + sCSS);
  return CSSReader.readFromStringReader (sCSS,
                                         new CSSReaderSettings ().setCSSVersion (ECSSVersion.CSS30)
                                                                 .setCustomErrorHandler (new LoggingCSSParseErrorHandler ())
                                                                 .setBrowserCompliantMode (bBrowserCompliantMode));
}