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)); } }
/** * Create a {@link CascadingStyleSheet} object from a parsed object. * * @param eVersion * The CSS version to use. May not be <code>null</code>. * @param aNode * The parsed CSS object to read. May not be <code>null</code>. * @return Never <code>null</code>. */ @Nonnull @Deprecated public static CascadingStyleSheet readCascadingStyleSheetFromNode (@Nonnull final ECSSVersion eVersion, @Nonnull final CSSNode aNode) { return readCascadingStyleSheetFromNode (eVersion, aNode, CSSReader.getDefaultInterpretErrorHandler ()); }
/** * Read the CSS from the passed File. * * @param aFile * The file containing the CSS to be parsed. May not be * <code>null</code>. * @param aFallbackCharset * The charset to be used in case neither a <code>@charset</code> rule * nor a BOM is present. May not be <code>null</code>. * @param eVersion * The CSS version to use. May not be <code>null</code>. * @return <code>null</code> if reading failed, the CSS declarations * otherwise. */ @Nullable public static CascadingStyleSheet readFromFile (@Nonnull final File aFile, @Nonnull final Charset aFallbackCharset, @Nonnull final ECSSVersion eVersion) { return readFromFile (aFile, new CSSReaderSettings ().setFallbackCharset (aFallbackCharset).setCSSVersion (eVersion)); }
aRealParseErrorHandler = getDefaultParseErrorHandler (); aRealParseExceptionHandler = getDefaultParseExceptionHandler (); final CSSNode aNode = _readStyleSheet (aCharStream, eVersion, aRealParseErrorHandler, aRealInterpretErrorHandler = getDefaultInterpretErrorHandler ();
/** * Read the CSS from the passed File. * * @param aFile * The file containing the CSS to be parsed. May not be * <code>null</code>. * @param aSettings * The settings to be used for reading the CSS. May not be * <code>null</code>. * @return <code>null</code> if reading failed, the CSS declarations * otherwise. * @since 3.8.2 */ @Nullable public static CascadingStyleSheet readFromFile (@Nonnull final File aFile, @Nonnull final CSSReaderSettings aSettings) { return readFromStream (new FileSystemResource (aFile), aSettings); }
/** * Read the CSS from the passed String using a character stream. An eventually * contained <code>@charset</code> rule is ignored. * * @param sCSS * The source string containing the CSS to be parsed. May not be * <code>null</code>. * @param eVersion * The CSS version to use. May not be <code>null</code>. * @return <code>null</code> if reading failed, the CSS declarations * otherwise. * @since 3.7.3 */ @Nullable public static CascadingStyleSheet readFromString (@Nonnull final String sCSS, @Nonnull final ECSSVersion eVersion) { return readFromStringReader (sCSS, new CSSReaderSettings ().setCSSVersion (eVersion)); }
final CSSNode aNode = _readStyleSheet (aCharStream, eVersion, getDefaultParseErrorHandler (), new DoNothingCSSParseExceptionCallback (), false);
try aDeclaredCharset = getCharsetDeclaredInCSS (aISP); aRealParseErrorHandler = getDefaultParseErrorHandler (); aRealParseExceptionHandler = getDefaultParseExceptionHandler (); final CSSNode aNode = _readStyleSheet (aCharStream, eVersion, aRealParseErrorHandler, aRealInterpretErrorHandler = getDefaultInterpretErrorHandler ();
/** * Read the CSS from the passed String using a byte stream. * * @param sCSS * The source string containing the CSS to be parsed. May not be * <code>null</code>. * @param aSettings * The settings to be used for reading the CSS. May not be * <code>null</code>. * @return <code>null</code> if reading failed, the CSS declarations * otherwise. * @since 3.8.2 */ @Nullable public static CascadingStyleSheet readFromStringStream (@Nonnull final String sCSS, @Nonnull final CSSReaderSettings aSettings) { return readFromStream (new StringInputStreamProvider (sCSS, aSettings.getFallbackCharset ()), aSettings); }
/** * Read the CSS from the passed String using a character stream. An eventually * contained <code>@charset</code> rule is ignored. * * @param sCSS * The source string containing the CSS to be parsed. May not be * <code>null</code>. * @param eVersion * The CSS version to use. May not be <code>null</code>. * @param aCustomExceptionHandler * An optional custom exception handler that can be used to collect the * unrecoverable parsing errors. May be <code>null</code>. * @return <code>null</code> if reading failed, the CSS declarations * otherwise. * @since 3.7.3 */ @Nullable public static CascadingStyleSheet readFromString (@Nonnull final String sCSS, @Nonnull final ECSSVersion eVersion, @Nullable final ICSSParseExceptionCallback aCustomExceptionHandler) { return readFromStringReader (sCSS, new CSSReaderSettings ().setCSSVersion (eVersion) .setCustomExceptionHandler (aCustomExceptionHandler)); }
/** * 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 (); }
/** * Read the CSS from the passed {@link IHasInputStream}. If the CSS contains * an explicit charset, the whole CSS is parsed again, with the charset found * inside the file, so the passed {@link IHasInputStream} must be able to * create a new input stream on second invocation! * * @param aISP * The input stream provider to use. Must be able to create new input * streams on every invocation, in case an explicit charset node was * found. May not be <code>null</code>. * @param aFallbackCharset * The charset to be used in case neither a <code>@charset</code> rule * nor a BOM is present. May not be <code>null</code>. * @param eVersion * The CSS version to use. May not be <code>null</code>. * @return <code>null</code> if reading failed, the CSS declarations * otherwise. */ @Nullable public static CascadingStyleSheet readFromStream (@Nonnull final IHasInputStream aISP, @Nonnull final Charset aFallbackCharset, @Nonnull final ECSSVersion eVersion) { return readFromStream (aISP, new CSSReaderSettings ().setFallbackCharset (aFallbackCharset).setCSSVersion (eVersion)); }
/** * Read the CSS from the passed String using a character stream. An eventually * contained <code>@charset</code> rule is ignored. * * @param sCSS * The source string containing the CSS to be parsed. May not be * <code>null</code>. * @param eVersion * The CSS version to use. May not be <code>null</code>. * @param aCustomErrorHandler * An optional custom error handler that can be used to collect the * recoverable parsing errors. May be <code>null</code>. * @return <code>null</code> if reading failed, the CSS declarations * otherwise. * @since 3.7.3 */ @Nullable public static CascadingStyleSheet readFromString (@Nonnull final String sCSS, @Nonnull final ECSSVersion eVersion, @Nullable final ICSSParseErrorHandler aCustomErrorHandler) { return readFromStringReader (sCSS, new CSSReaderSettings ().setCSSVersion (eVersion) .setCustomErrorHandler (aCustomErrorHandler)); }
/** * Read the CSS from the passed File. * * @param aFile * The file containing the CSS to be parsed. May not be * <code>null</code>. * @param aFallbackCharset * The charset to be used in case neither a <code>@charset</code> rule * nor a BOM is present. May not be <code>null</code>. * @param eVersion * The CSS version to use. May not be <code>null</code>. * @param aCustomExceptionHandler * An optional custom exception handler that can be used to collect the * unrecoverable parsing errors. May be <code>null</code>. * @return <code>null</code> if reading failed, the CSS declarations * otherwise. */ @Nullable public static CascadingStyleSheet readFromFile (@Nonnull final File aFile, @Nonnull final Charset aFallbackCharset, @Nonnull final ECSSVersion eVersion, @Nullable final ICSSParseExceptionCallback aCustomExceptionHandler) { return readFromFile (aFile, new CSSReaderSettings ().setFallbackCharset (aFallbackCharset) .setCSSVersion (eVersion) .setCustomExceptionHandler (aCustomExceptionHandler)); }
/** * Create a {@link CSSDeclarationList} object from a parsed object. * * @param eVersion * The CSS version to use. May not be <code>null</code>. * @param aNode * The parsed CSS object to read. May not be <code>null</code>. * @return Never <code>null</code>. */ @Nonnull @Deprecated public static CSSDeclarationList readDeclarationListFromNode (@Nonnull final ECSSVersion eVersion, @Nonnull final CSSNode aNode) { return readDeclarationListFromNode (eVersion, aNode, CSSReader.getDefaultInterpretErrorHandler ()); }
ValueEnforcer.notNull (aSettings, "Settings"); final CascadingStyleSheet aCSS = CSSReader.readFromString (sOriginalCSS, aSettings.getVersion ()); if (aCSS != null)
@Nullable final ICSSParseErrorHandler aCustomErrorHandler) return readFromStream (aISP, new CSSReaderSettings ().setFallbackCharset (aFallbackCharset) .setCSSVersion (eVersion)
/** * Read the CSS from the passed String using a character stream. An eventually * contained <code>@charset</code> rule is ignored. * * @param sCSS * The source string containing the CSS to be parsed. May not be * <code>null</code>. * @param eVersion * The CSS version to use. May not be <code>null</code>. * @param aCustomErrorHandler * An optional custom error handler that can be used to collect the * recoverable parsing errors. May be <code>null</code>. * @param aCustomExceptionHandler * An optional custom exception handler that can be used to collect the * unrecoverable parsing errors. May be <code>null</code>. * @return <code>null</code> if reading failed, the CSS declarations * otherwise. * @since 3.7.3 */ @Nullable public static CascadingStyleSheet readFromString (@Nonnull final String sCSS, @Nonnull final ECSSVersion eVersion, @Nullable final ICSSParseErrorHandler aCustomErrorHandler, @Nullable final ICSSParseExceptionCallback aCustomExceptionHandler) { return readFromStringReader (sCSS, new CSSReaderSettings ().setCSSVersion (eVersion) .setCustomErrorHandler (aCustomErrorHandler) .setCustomExceptionHandler (aCustomExceptionHandler)); }
/** * Read the CSS from the passed File. * * @param aFile * The file containing the CSS to be parsed. May not be * <code>null</code>. * @param aFallbackCharset * The charset to be used in case neither a <code>@charset</code> rule * nor a BOM is present. May not be <code>null</code>. * @param eVersion * The CSS version to use. May not be <code>null</code>. * @param aCustomErrorHandler * An optional custom error handler that can be used to collect the * recoverable parsing errors. May be <code>null</code>. * @return <code>null</code> if reading failed, the CSS declarations * otherwise. */ @Nullable public static CascadingStyleSheet readFromFile (@Nonnull final File aFile, @Nonnull final Charset aFallbackCharset, @Nonnull final ECSSVersion eVersion, @Nullable final ICSSParseErrorHandler aCustomErrorHandler) { return readFromFile (aFile, new CSSReaderSettings ().setFallbackCharset (aFallbackCharset) .setCSSVersion (eVersion) .setCustomErrorHandler (aCustomErrorHandler)); }
@Override public TreeContext generate(Reader r) throws IOException { LineReader lr = new LineReader(r); CSSCharStream s = new CSSCharStream(new LineReader(lr)); s.setTabSize(1); final ParserCSS30TokenManager th = new ParserCSS30TokenManager(s); final ParserCSS30 p = new ParserCSS30(th); p.setCustomErrorHandler(null); p.setBrowserCompliantMode(false); try { CascadingStyleSheet sheet = CSSHandler.readCascadingStyleSheetFromNode(ECSSVersion.LATEST, p.styleSheet(), CSSReader.getDefaultInterpretErrorHandler()); GtCssVisitor v = new GtCssVisitor(sheet, lr); CSSVisitor.visitCSS(sheet, v); return v.getTreeContext(); } catch (ParseException e) { throw new IOException(e); } } }