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());
}
}
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());
}
}
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);
}
}
}
}
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;
}
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;
}
Aggregations