public Optional<ScopedCssResult> parse(TypeElement componentTypeElement, String css) { if (css == null || css.isEmpty()) { return Optional.empty(); } final ClassName componentTypeName = ClassName.get(componentTypeElement); final String md5 = md5(componentTypeName.toString()); final String datav = "data-v-" + md5; // UTF-8 is the fallback if neither a BOM nor @charset rule is present final CascadingStyleSheet cssSheet = CSSReader .readFromString(css, StandardCharsets.UTF_8, ECSSVersion.CSS30); if (cssSheet == null) { return Optional.empty(); } CSSVisitor.visitCSS(cssSheet, new DefaultCSSVisitor() { @Override public void onBeginStyleRule(@Nonnull final CSSStyleRule aStyleRule) { ICommonsList<CSSSelector> selectors = aStyleRule.getAllSelectors(); if (selectors != null) { for (CSSSelector sel : selectors) { sel.addMember(new CSSSelectorAttribute("", datav)); } } } }); Map<String, String> mandatoryAttributes = new LinkedHashMap<>(); mandatoryAttributes.put(datav, null); return Optional.of(new ScopedCssResult(getCssAsString(cssSheet), mandatoryAttributes)); } }
/** * 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 (); }
ValueEnforcer.notNull (aSettings, "Settings"); final CascadingStyleSheet aCSS = CSSReader.readFromString (sOriginalCSS, aSettings.getVersion ()); if (aCSS != null)
private void parseCSS(String css, URL baseUrl, Set<URL> resourceUrls) { logger.verbose("enter"); final CascadingStyleSheet cascadingStyleSheet = CSSReader.readFromString(css, ECSSVersion.CSS30); if (cascadingStyleSheet == null) { logger.verbose("exit - failed to read CSS string"); return; } collectAllImportUris(cascadingStyleSheet, resourceUrls, baseUrl); collectAllFontFaceUris(cascadingStyleSheet, resourceUrls, baseUrl); collectAllBackgroundImageUris(cascadingStyleSheet, resourceUrls, baseUrl); logger.verbose("exit"); }
private void parseCSS(CssTreeNode node, String css) { if (css == null) { return; } final CascadingStyleSheet cascadingStyleSheet = CSSReader.readFromString(css, ECSSVersion.CSS30); if (cascadingStyleSheet == null) { return; } ICommonsList<ICSSTopLevelRule> allRules = cascadingStyleSheet.getAllRules(); if (allRules.isEmpty()) { return; } ICommonsList<CSSImportRule> allImportRules = cascadingStyleSheet.getAllImportRules(); node.setAllImportRules(allImportRules); node.setAllStyleRules(cascadingStyleSheet.getAllStyleRules()); }
public String process( String input ) { CascadingStyleSheet css = CSSReader.readFromString( input, CCharset.CHARSET_UTF_8_OBJ, ECSSVersion.CSS30 ); CascadingStyleSheet out = new CascadingStyleSheet(); for( ICSSTopLevelRule rule : css.getAllRules() ) { ICSSTopLevelRule outRule = filterRule( rule ); if( outRule != null ) out.addRule( outRule ); } CSSWriterSettings settings = new CSSWriterSettings( ECSSVersion.CSS30, true ); settings.setRemoveUnnecessaryCode( true ); return new CSSWriter( settings ).getCSSAsString( out ); }