use of com.helger.css.decl.CSSDeclaration in project ph-css by phax.
the class CSSShortHandDescriptorTest method testBackground1.
@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));
}
use of com.helger.css.decl.CSSDeclaration in project ph-css by phax.
the class CSSShortHandDescriptorTest method testBorderColor2.
@Test
public void testBorderColor2() {
final CSSShortHandDescriptor aSHD = CSSShortHandRegistry.getShortHandDescriptor(ECSSProperty.BORDER_COLOR);
assertNotNull(aSHD);
final CSSDeclaration aDecl = CSSReaderDeclarationList.readFromString("border-color: red blue", ECSSVersion.CSS30).getDeclarationAtIndex(0);
assertNotNull(aDecl);
final List<CSSDeclaration> aSplittedDecls = aSHD.getSplitIntoPieces(aDecl);
assertNotNull(aSplittedDecls);
assertEquals(4, aSplittedDecls.size());
assertEquals("border-top-color:red", aSplittedDecls.get(0).getAsCSSString(CWS));
assertEquals("border-right-color:blue", aSplittedDecls.get(1).getAsCSSString(CWS));
assertEquals("border-bottom-color:red", aSplittedDecls.get(2).getAsCSSString(CWS));
assertEquals("border-left-color:blue", aSplittedDecls.get(3).getAsCSSString(CWS));
}
use of com.helger.css.decl.CSSDeclaration in project ph-css by phax.
the class CSSVisitorDeclarationListFuncTest method testModifyingCSSUrlVisitor.
@Test
public void testModifyingCSSUrlVisitor() {
final CSSDeclarationList aCSS = CSSReaderDeclarationList.readFromString("background:url(a.gif);background:url(b.gif);", ECSSVersion.CSS30);
assertNotNull(aCSS);
// Append ".modified" to all URLs
final MockModifyingCSSUrlVisitor aVisitor2 = new MockModifyingCSSUrlVisitor();
CSSVisitor.visitAllDeclarationUrls(aCSS, aVisitor2);
// Check the result
assertEquals("background:url(a.gif.modified);background:url(b.gif.modified)", new CSSWriter(ECSSVersion.CSS30, true).getCSSAsString(aCSS));
// Re-iterate to check twice
CSSVisitor.visitAllDeclarationUrls(aCSS, new DefaultCSSUrlVisitor() {
@Override
public void onImport(@Nonnull final CSSImportRule aImportRule) {
assertTrue(aImportRule.getLocationString().endsWith(".modified"));
}
@Override
public void onUrlDeclaration(@Nullable final ICSSTopLevelRule aTopLevelRule, @Nonnull final CSSDeclaration aDeclaration, @Nonnull final CSSExpressionMemberTermURI aURITerm) {
assertTrue(aURITerm.getURIString().endsWith(".modified"));
}
});
}
use of com.helger.css.decl.CSSDeclaration in project flow by vaadin.
the class StyleAttributeHandler method parseStyles.
/**
* Parses the given style string and populates the given style object with
* the found styles.
*
* @param styleString
* the string to parse
* @return a map containing the found style rules
*/
public static LinkedHashMap<String, String> parseStyles(String styleString) {
CollectingCSSParseErrorHandler errorCollector = new CollectingCSSParseErrorHandler();
CSSDeclarationList parsed = CSSReaderDeclarationList.readFromString(styleString, ECSSVersion.LATEST, errorCollector);
if (errorCollector.hasParseErrors()) {
throw new IllegalArgumentException(String.format(ERROR_PARSING_STYLE, styleString, errorCollector.getAllParseErrors().get(0).getErrorMessage()));
}
if (parsed == null) {
// Did not find any styles
throw new IllegalArgumentException(String.format(ERROR_PARSING_STYLE, styleString, "No styles found"));
}
LinkedHashMap<String, String> parsedStyles = new LinkedHashMap<>();
for (CSSDeclaration declaration : parsed.getAllDeclarations()) {
String key = declaration.getProperty();
String value = declaration.getExpression().getAsCSSString(new CSSWriterSettings(ECSSVersion.LATEST).setOptimizedOutput(true), 0);
parsedStyles.put(StyleUtil.styleAttributeToProperty(key), value);
}
return parsedStyles;
}
use of com.helger.css.decl.CSSDeclaration in project ph-css by phax.
the class CSSReaderDeclarationListTest method testReadAndValidate.
@Test
public void testReadAndValidate() {
final CSSDeclarationList aList = CSSReaderDeclarationList.readFromString("color:red; background:fixed;", ECSSVersion.CSS30);
assertNotNull(aList);
assertEquals(2, aList.getDeclarationCount());
CSSDeclaration aDecl = aList.getDeclarationAtIndex(0);
assertNotNull(aDecl);
assertEquals("color", aDecl.getProperty());
assertEquals(1, aDecl.getExpression().getMemberCount());
assertEquals("red", ((CSSExpressionMemberTermSimple) aDecl.getExpression().getMemberAtIndex(0)).getValue());
aDecl = aList.getDeclarationAtIndex(1);
assertNotNull(aDecl);
assertEquals("background", aDecl.getProperty());
}
Aggregations