Search in sources :

Example 1 with SACParserCSS3

use of com.steadystate.css.parser.SACParserCSS3 in project structr by structr.

the class ImportCssFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, Object[] sources) throws FrameworkException {
    assertArrayHasMinLengthAndAllElementsNotNull(sources, 1);
    if (sources[0] instanceof File) {
        final File file = (File) sources[0];
        if (file.getSize() == 0) {
            return "";
        }
        try (final FileReader reader = new FileReader(file.getFileOnDisk())) {
            logger.info("Parsing CSS from {}..", file.getName());
            final InputSource source = new InputSource(reader);
            final CSSOMParser parser = new CSSOMParser(new SACParserCSS3());
            final CSSStyleSheet styleSheet = parser.parseStyleSheet(source, null, null);
            final CSSRuleList rules = styleSheet.getCssRules();
            int count = 0;
            logger.info("{} rules", styleSheet.getCssRules().getLength());
            for (int i = 0; i < rules.getLength(); i++) {
                final CSSRule rule = rules.item(i);
                importCSSRule(rule);
                count++;
            }
            logger.info("{} rules imported", count);
            return true;
        } catch (final Exception e) {
            logParameterError(caller, sources, ctx.isJavaScriptContext());
            return usage(ctx.isJavaScriptContext());
        }
    }
    return usage(ctx.isJavaScriptContext());
}
Also used : SACParserCSS3(com.steadystate.css.parser.SACParserCSS3) InputSource(org.w3c.css.sac.InputSource) CSSRule(org.w3c.dom.css.CSSRule) CSSOMParser(com.steadystate.css.parser.CSSOMParser) CSSStyleSheet(org.w3c.dom.css.CSSStyleSheet) FileReader(java.io.FileReader) File(org.structr.web.entity.File) FrameworkException(org.structr.common.error.FrameworkException) CSSRuleList(org.w3c.dom.css.CSSRuleList)

Example 2 with SACParserCSS3

use of com.steadystate.css.parser.SACParserCSS3 in project formatter-maven-plugin by revelc.

the class CssFormatter method doFormat.

@Override
protected String doFormat(final String code, final LineEnding ending) throws IOException {
    final var source = new InputSource(new StringReader(code));
    final var parser = new CSSOMParser(new SACParserCSS3());
    final var sheet = (CSSStyleSheetImpl) parser.parseStyleSheet(source, null, null);
    var formattedCode = sheet.getCssText(this.formatter);
    // Patch converted 'tab' back to '\9' for IE 7,8, and 9 hack. Cssparser switches it to 'tab'.
    formattedCode = formattedCode.replace("\t;", "\\9;");
    if (code.equals(formattedCode)) {
        return null;
    }
    return formattedCode;
}
Also used : SACParserCSS3(com.steadystate.css.parser.SACParserCSS3) InputSource(org.w3c.css.sac.InputSource) CSSStyleSheetImpl(com.steadystate.css.dom.CSSStyleSheetImpl) CSSOMParser(com.steadystate.css.parser.CSSOMParser) StringReader(java.io.StringReader)

Example 3 with SACParserCSS3

use of com.steadystate.css.parser.SACParserCSS3 in project java-to-graphviz by randomnoun.

the class CommentExtractor method getStyleSheet.

// when we construct the DagNodes, automatically add classes based on AST type
// and line number, which will make colouring these things in from jacoco output that much simpler
// and whatever JVMTI uses, which is probably line numbers as well
/**
 * Return a stylesheet, composed of:
 * <ul>
 * <li>imported base CSS
 * <li>imported user CSS
 * <li>source code styles (gv-style blocks), may import other CSS
 * <li>user CSS rules
 * </ul>
 *
 * @param comments
 * @param baseCssHref
 * @param userCssHrefs
 * @param userCssRules
 * @return
 * @throws IOException
 */
public CSSStyleSheet getStyleSheet(List<CommentText> comments, String baseCssHref, List<String> userCssHrefs, List<String> userCssRules) throws IOException {
    String css = "";
    if (!Text.isBlank(baseCssHref) && !baseCssHref.equals("-")) {
        css = "@import \"" + baseCssHref + "\";\n";
    }
    if (userCssHrefs != null) {
        for (String s : userCssHrefs) {
            css += "@import \"" + s + "\";\n";
        }
    }
    for (CommentText c : comments) {
        // String t = c.text;
        if (c instanceof GvStyleComment) {
            // remove non-standard css comments
            String[] ss = ((GvStyleComment) c).style.split("\n");
            for (int i = 0; i < ss.length; i++) {
                String s = ss[i];
                if (s.contains("//") && !s.contains("://")) {
                    // double-slash can start a comment, but not if it might be a @import url(scheme://...) double-slash
                    s = s.substring(0, s.indexOf("//"));
                }
                css += (i == 0 ? "" : "\n") + s;
            }
        } else if (c instanceof GvComment) {
        // could check inline styles here maybe
        }
    }
    if (userCssRules != null) {
        for (String s : userCssRules) {
            css += s + "\n";
        }
    }
    if (logger.isDebugEnabled()) {
        logger.debug("here's the css: " + css);
    }
    // pre-process the // comments out of the block comments, which isn't standard CSS
    CSSOMParser parser = new CSSOMParser(new SACParserCSS3());
    InputSource source = new InputSource(new StringReader(css));
    CSSStyleSheet stylesheet = parser.parseStyleSheet(source, null, null);
    CSSStyleSheetImpl stylesheetImpl = (CSSStyleSheetImpl) stylesheet;
    RestrictedCssStyleSheetImpl restrictedStylesheetImpl = new RestrictedCssStyleSheetImpl(stylesheetImpl);
    // probably need to wrap this in something which overrides importImports
    // public void importImports(final boolean recursive) throws DOMException {
    // or provide my own URI handler and set a weird base URI
    // stylesheetImpl.importImports(true); // recursive = true
    restrictedStylesheetImpl.importImports(true);
    if (logger.isDebugEnabled()) {
        logger.debug("CSS rules: " + restrictedStylesheetImpl.getCssText());
    }
    return restrictedStylesheetImpl;
}
Also used : SACParserCSS3(com.steadystate.css.parser.SACParserCSS3) InputSource(org.w3c.css.sac.InputSource) CSSStyleSheetImpl(com.steadystate.css.dom.CSSStyleSheetImpl) CommentText(com.randomnoun.build.javaToGraphviz.comment.CommentText) CSSOMParser(com.steadystate.css.parser.CSSOMParser) RestrictedCssStyleSheetImpl(com.randomnoun.build.javaToGraphviz.dom.RestrictedCssStyleSheetImpl) GvStyleComment(com.randomnoun.build.javaToGraphviz.comment.GvStyleComment) GvComment(com.randomnoun.build.javaToGraphviz.comment.GvComment) CSSStyleSheet(org.w3c.dom.css.CSSStyleSheet) StringReader(java.io.StringReader)

Aggregations

CSSOMParser (com.steadystate.css.parser.CSSOMParser)3 SACParserCSS3 (com.steadystate.css.parser.SACParserCSS3)3 InputSource (org.w3c.css.sac.InputSource)3 CSSStyleSheetImpl (com.steadystate.css.dom.CSSStyleSheetImpl)2 StringReader (java.io.StringReader)2 CSSStyleSheet (org.w3c.dom.css.CSSStyleSheet)2 CommentText (com.randomnoun.build.javaToGraphviz.comment.CommentText)1 GvComment (com.randomnoun.build.javaToGraphviz.comment.GvComment)1 GvStyleComment (com.randomnoun.build.javaToGraphviz.comment.GvStyleComment)1 RestrictedCssStyleSheetImpl (com.randomnoun.build.javaToGraphviz.dom.RestrictedCssStyleSheetImpl)1 FileReader (java.io.FileReader)1 FrameworkException (org.structr.common.error.FrameworkException)1 File (org.structr.web.entity.File)1 CSSRule (org.w3c.dom.css.CSSRule)1 CSSRuleList (org.w3c.dom.css.CSSRuleList)1