com.steadystate.css.parser.CSSOMParser Java Examples

The following examples show how to use com.steadystate.css.parser.CSSOMParser. 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: StyleParser.java    From svgtoandroid with MIT License 6 votes vote down vote up
private void init() {
    if (style != null) {
        String styleContent = style.getValue().getText();
        if (styleContent != null && !styleContent.isEmpty()) {
            InputSource source = new InputSource(new StringReader(styleContent));
            CSSOMParser parser = new CSSOMParser(new SACParserCSS3());
            parser.setErrorHandler(new ParserErrorHandler());
            try {
                styleSheet = parser.parseStyleSheet(source, null, null);
                cssFormat = new CSSFormat().setRgbAsHex(true);

                CSSRuleList rules = styleSheet.getCssRules();
                for (int i = 0; i < rules.getLength(); i++) {
                    final CSSRule rule = rules.item(i);
                    if (rule instanceof CSSStyleRuleImpl) {
                        styleRuleMap.put(((CSSStyleRuleImpl) rule).getSelectorText(), (CSSStyleRuleImpl) rule);
                    }
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
 
Example #2
Source File: FeedUtils.java    From commafeed with Apache License 2.0 6 votes vote down vote up
public static String escapeIFrameCss(String orig) {
	String rule = "";
	CSSOMParser parser = new CSSOMParser();
	try {
		List<String> rules = new ArrayList<>();
		CSSStyleDeclaration decl = parser.parseStyleDeclaration(new InputSource(new StringReader(orig)));

		for (int i = 0; i < decl.getLength(); i++) {
			String property = decl.item(i);
			String value = decl.getPropertyValue(property);
			if (StringUtils.isBlank(property) || StringUtils.isBlank(value)) {
				continue;
			}

			if (ALLOWED_IFRAME_CSS_RULES.contains(property) && StringUtils.containsNone(value, FORBIDDEN_CSS_RULE_CHARACTERS)) {
				rules.add(property + ":" + decl.getPropertyValue(property) + ";");
			}
		}
		rule = StringUtils.join(rules, "");
	} catch (Exception e) {
		log.error(e.getMessage(), e);
	}
	return rule;
}
 
Example #3
Source File: FeedUtils.java    From commafeed with Apache License 2.0 6 votes vote down vote up
public static String escapeImgCss(String orig) {
	String rule = "";
	CSSOMParser parser = new CSSOMParser();
	try {
		List<String> rules = new ArrayList<>();
		CSSStyleDeclaration decl = parser.parseStyleDeclaration(new InputSource(new StringReader(orig)));

		for (int i = 0; i < decl.getLength(); i++) {
			String property = decl.item(i);
			String value = decl.getPropertyValue(property);
			if (StringUtils.isBlank(property) || StringUtils.isBlank(value)) {
				continue;
			}

			if (ALLOWED_IMG_CSS_RULES.contains(property) && StringUtils.containsNone(value, FORBIDDEN_CSS_RULE_CHARACTERS)) {
				rules.add(property + ":" + decl.getPropertyValue(property) + ";");
			}
		}
		rule = StringUtils.join(rules, "");
	} catch (Exception e) {
		log.error(e.getMessage(), e);
	}
	return rule;
}
 
Example #4
Source File: CssFormatter.java    From formatter-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Override
protected String doFormat(String code, LineEnding ending) throws IOException {

    InputSource source = new InputSource(new StringReader(code));
    CSSOMParser parser = new CSSOMParser(new SACParserCSS3());
    CSSStyleSheetImpl sheet = (CSSStyleSheetImpl) parser.parseStyleSheet(source, null, null);
    String formattedCode = sheet.getCssText(formatter);

    if (code.equals(formattedCode)) {
        return null;
    }
    return formattedCode;
}