Search in sources :

Example 21 with CSSOMParser

use of com.gargoylesoftware.css.parser.CSSOMParser in project LoboEvolution by LoboEvolution.

the class CSSMediaRuleImpl method insertRule.

/**
 * Insert a new rule at the given index.
 *
 * @param rule the rule to be inserted
 * @param index the insert pos
 * @throws DOMException in case of error
 */
public void insertRule(final String rule, final int index) throws DOMException {
    final CSSStyleSheetImpl parentStyleSheet = getParentStyleSheet();
    try {
        final CSSOMParser parser = new CSSOMParser();
        parser.setParentStyleSheet(parentStyleSheet);
        parser.setErrorHandler(ThrowCssExceptionErrorHandler.INSTANCE);
        final AbstractCSSRuleImpl r = parser.parseRule(rule);
        // Insert the rule into the list of rules
        getCssRules().insert(r, index);
        r.setParentRule(this);
    } catch (final IndexOutOfBoundsException e) {
        throw new DOMException(DOMException.INDEX_SIZE_ERR, DOMException.INDEX_OUT_OF_BOUNDS, e.getMessage());
    } catch (final CSSException e) {
        throw new DOMException(DOMException.SYNTAX_ERR, DOMException.SYNTAX_ERROR, e.getMessage());
    } catch (final IOException e) {
        throw new DOMException(DOMException.SYNTAX_ERR, e.getMessage());
    }
}
Also used : CSSOMParser(com.gargoylesoftware.css.parser.CSSOMParser) CSSException(com.gargoylesoftware.css.parser.CSSException) IOException(java.io.IOException)

Example 22 with CSSOMParser

use of com.gargoylesoftware.css.parser.CSSOMParser in project LoboEvolution by LoboEvolution.

the class CSSStyleRuleImpl method setCssText.

/**
 * {@inheritDoc}
 */
@Override
public void setCssText(final String cssText) throws DOMException {
    try {
        final CSSOMParser parser = new CSSOMParser();
        final AbstractCSSRuleImpl r = parser.parseRule(cssText);
        // The rule must be a style rule
        if (r instanceof CSSStyleRuleImpl) {
            selectors_ = ((CSSStyleRuleImpl) r).selectors_;
            style_ = ((CSSStyleRuleImpl) r).style_;
        } else {
            throw new DOMException(DOMException.INVALID_MODIFICATION_ERR, DOMException.EXPECTING_STYLE_RULE);
        }
    } catch (final CSSException e) {
        throw new DOMException(DOMException.SYNTAX_ERR, e.getMessage());
    } catch (final IOException e) {
        throw new DOMException(DOMException.SYNTAX_ERR, e.getMessage());
    }
}
Also used : CSSOMParser(com.gargoylesoftware.css.parser.CSSOMParser) CSSException(com.gargoylesoftware.css.parser.CSSException) IOException(java.io.IOException)

Example 23 with CSSOMParser

use of com.gargoylesoftware.css.parser.CSSOMParser in project LoboEvolution by LoboEvolution.

the class HTMLStyleElementImpl method processStyle.

/**
 * <p>processStyle.</p>
 */
private void processStyle() {
    this.styleSheet = null;
    final HTMLDocumentImpl doc = (HTMLDocumentImpl) getOwnerDocument();
    if (CSSUtilities.matchesMedia(getMedia(), doc.getDefaultView())) {
        final String text = getRawInnerText(true);
        if (Strings.isNotBlank(text)) {
            final String processedText = CSSUtilities.preProcessCss(text);
            final CSSOMParser parser = new CSSOMParser(new CSS3Parser());
            final String baseURI = doc.getBaseURI();
            final InputSource is = CSSUtilities.getCssInputSourceForStyleSheet(processedText, baseURI);
            try {
                final com.gargoylesoftware.css.dom.CSSStyleSheetImpl sheet = parser.parseStyleSheet(is, null);
                sheet.setHref(baseURI);
                sheet.setDisabled(this.disabled);
                CSSStyleSheetImpl cssStyleSheet = new CSSStyleSheetImpl(sheet);
                cssStyleSheet.setOwnerNode(this);
                doc.addStyleSheet(cssStyleSheet);
                this.styleSheet = cssStyleSheet;
            } catch (final Throwable err) {
                this.warn("Unable to parse style sheet", err);
            }
        }
    }
}
Also used : InputSource(com.gargoylesoftware.css.parser.InputSource) CSSStyleSheetImpl(org.loboevolution.html.js.css.CSSStyleSheetImpl) CSSOMParser(com.gargoylesoftware.css.parser.CSSOMParser) CSS3Parser(com.gargoylesoftware.css.parser.javacc.CSS3Parser)

Example 24 with CSSOMParser

use of com.gargoylesoftware.css.parser.CSSOMParser in project htmlunit by HtmlUnit.

the class CSSStyleSheet method parseSelectors.

/**
 * Parses the selectors at the specified input source. If anything at all goes wrong, this
 * method returns an empty selector list.
 *
 * @param source the source from which to retrieve the selectors to be parsed
 * @return the selectors parsed from the specified input source
 */
public SelectorList parseSelectors(final String source) {
    SelectorList selectors;
    try {
        final CSSErrorHandler errorHandler = getWindow().getWebWindow().getWebClient().getCssErrorHandler();
        final CSSOMParser parser = new CSSOMParser(new CSS3Parser());
        parser.setErrorHandler(errorHandler);
        selectors = parser.parseSelectors(source);
        // in case of error parseSelectors returns null
        if (null == selectors) {
            selectors = new SelectorListImpl();
        }
    } catch (final Throwable t) {
        if (LOG.isErrorEnabled()) {
            LOG.error("Error parsing CSS selectors from '" + source + "': " + t.getMessage(), t);
        }
        selectors = new SelectorListImpl();
    }
    return selectors;
}
Also used : SelectorListImpl(com.gargoylesoftware.css.parser.selector.SelectorListImpl) CSSErrorHandler(com.gargoylesoftware.css.parser.CSSErrorHandler) SelectorList(com.gargoylesoftware.css.parser.selector.SelectorList) CSSOMParser(com.gargoylesoftware.css.parser.CSSOMParser) CSS3Parser(com.gargoylesoftware.css.parser.javacc.CSS3Parser)

Example 25 with CSSOMParser

use of com.gargoylesoftware.css.parser.CSSOMParser in project htmlunit by HtmlUnit.

the class CSSStyleSheet method parseMedia.

/**
 * Parses the given media string. If anything at all goes wrong, this
 * method returns an empty MediaList list.
 *
 * @param source the source from which to retrieve the media to be parsed
 * @return the media parsed from the specified input source
 */
static MediaListImpl parseMedia(final CSSErrorHandler errorHandler, final String mediaString) {
    MediaListImpl media = media_.get(mediaString);
    if (media != null) {
        return media;
    }
    try {
        final CSSOMParser parser = new CSSOMParser(new CSS3Parser());
        parser.setErrorHandler(errorHandler);
        media = new MediaListImpl(parser.parseMedia(mediaString));
        media_.put(mediaString, media);
        return media;
    } catch (final Exception e) {
        if (LOG.isErrorEnabled()) {
            LOG.error("Error parsing CSS media from '" + mediaString + "': " + e.getMessage(), e);
        }
    }
    media = new MediaListImpl(null);
    media_.put(mediaString, media);
    return media;
}
Also used : CSSOMParser(com.gargoylesoftware.css.parser.CSSOMParser) CSS3Parser(com.gargoylesoftware.css.parser.javacc.CSS3Parser) MediaListImpl(com.gargoylesoftware.css.dom.MediaListImpl) FailingHttpStatusCodeException(com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException) CSSException(com.gargoylesoftware.css.parser.CSSException) IOException(java.io.IOException) CSSParseException(com.gargoylesoftware.css.parser.CSSParseException) DOMException(org.w3c.dom.DOMException) MalformedURLException(java.net.MalformedURLException)

Aggregations

CSSOMParser (com.gargoylesoftware.css.parser.CSSOMParser)26 IOException (java.io.IOException)16 CSSException (com.gargoylesoftware.css.parser.CSSException)14 CSS3Parser (com.gargoylesoftware.css.parser.javacc.CSS3Parser)10 CSSErrorHandler (com.gargoylesoftware.css.parser.CSSErrorHandler)4 CSSParseException (com.gargoylesoftware.css.parser.CSSParseException)4 SelectorList (com.gargoylesoftware.css.parser.selector.SelectorList)4 CSSStyleDeclarationImpl (com.gargoylesoftware.css.dom.CSSStyleDeclarationImpl)2 DOMException (com.gargoylesoftware.css.dom.DOMException)2 InputSource (com.gargoylesoftware.css.parser.InputSource)2 MediaQueryList (com.gargoylesoftware.css.parser.media.MediaQueryList)2 HTMLDocument (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 DOMException (org.w3c.dom.DOMException)2 CSSStyleSheetImpl (com.gargoylesoftware.css.dom.CSSStyleSheetImpl)1 MediaListImpl (com.gargoylesoftware.css.dom.MediaListImpl)1 MediaQuery (com.gargoylesoftware.css.parser.media.MediaQuery)1 SelectorListImpl (com.gargoylesoftware.css.parser.selector.SelectorListImpl)1 FailingHttpStatusCodeException (com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException)1 DisabledElement (com.gargoylesoftware.htmlunit.html.DisabledElement)1