Output Formatting

Starting with version 0.9.16 CSSParser includes limited support of output formatting. At the moment the formatter uses a fixed layout.
There are options

  • to place every property from a rule in a separate line
  • to output the original string value instead of the the unescaped one
  • to always output color values as hex instead of rgb

If you need more options, please open a ticket.

InputSource source = new InputSource(new StringReader("div:after{content:'css\\200Bparser'}"));
CSSOMParser parser = new CSSOMParser(new SACParserCSS3());
CSSStyleSheetImpl sheet = (CSSStyleSheetImpl) parser.parseStyleSheet(source, null, null);

Assert.assertEquals("div:after { content: \"css\u200Bparser\" }", sheet.getCssText());

Assert.assertEquals("div:after {" + NEW_LINE + "    content: \"css\u200Bparser\"" + NEW_LINE + "}", sheet.getCssText(new CSSFormat().setPropertiesInSeparateLines(4)));

Assert.assertEquals("div:after { content: \"css\\200Bparser\" }", sheet.getCssText(new CSSFormat().setUseSourceStringValues(true)));
InputSource source = new InputSource(new StringReader("h1{background:rgb(7,42,0)}"));
CSSOMParser parser = new CSSOMParser(new SACParserCSS3());
CSSStyleSheetImpl sheet = (CSSStyleSheetImpl) parser.parseStyleSheet(source, null, null);

Assert.assertEquals("h1 { background: rgb(7, 42, 0) }", sheet.getCssText());

Assert.assertEquals("h1 { background: #072a00 }", sheet.getCssText(new CSSFormat().setRgbAsHex(true)));