use of com.helger.css.decl.ICSSTopLevelRule in project jmeter by apache.
the class CssParserCacheLoader method load.
@Override
public URLCollection load(Triple<String, URL, Charset> triple) throws Exception {
final String cssContent = triple.getLeft();
final URL baseUrl = triple.getMiddle();
final Charset charset = triple.getRight();
final CSSReaderSettings readerSettings = new CSSReaderSettings().setBrowserCompliantMode(true).setFallbackCharset(charset).setCSSVersion(ECSSVersion.CSS30).setCustomErrorHandler(new LoggingCSSParseErrorHandler()).setCustomExceptionHandler(new CSSParseExceptionCallback(baseUrl));
if (IGNORE_ALL_CSS_ERRORS) {
readerSettings.setInterpretErrorHandler(new DoNothingCSSInterpretErrorHandler());
}
final CascadingStyleSheet aCSS = CSSReader.readFromStringReader(cssContent, readerSettings);
final URLCollection urls = new URLCollection(new ArrayList<>());
if (aCSS == null) {
LOG.warn("Failed parsing CSS: " + baseUrl + ", got null CascadingStyleSheet");
return urls;
}
CSSVisitor.visitCSSUrl(aCSS, new DefaultCSSUrlVisitor() {
@Override
public void onImport(CSSImportRule rule) {
final String location = rule.getLocationString();
if (!StringUtils.isEmpty(location)) {
urls.addURL(location, baseUrl);
}
}
// Call for URLs outside of URLs
@Override
public void onUrlDeclaration(final ICSSTopLevelRule aTopLevelRule, final CSSDeclaration aDeclaration, final CSSExpressionMemberTermURI aURITerm) {
// NOOP
// Browser fetch such urls only when CSS rule matches
// so we disable this code
}
});
return urls;
}
use of com.helger.css.decl.ICSSTopLevelRule in project ph-css by phax.
the class CSSVisitorForUrl method onDeclaration.
public void onDeclaration(@Nonnull final CSSDeclaration aDeclaration) {
final ICSSTopLevelRule aTopLevelRule = m_aTopLevelRule.isEmpty() ? null : m_aTopLevelRule.peek();
final CSSExpression aExpr = aDeclaration.getExpression();
for (final ICSSExpressionMember aMember : aExpr.getAllMembers()) if (aMember instanceof CSSExpressionMemberTermURI) {
final CSSExpressionMemberTermURI aExprTerm = (CSSExpressionMemberTermURI) aMember;
m_aVisitor.onUrlDeclaration(aTopLevelRule, aDeclaration, aExprTerm);
}
}
use of com.helger.css.decl.ICSSTopLevelRule in project ph-css by phax.
the class WikiVisitDataUrls method readFromStyleAttributeWithAPI.
public void readFromStyleAttributeWithAPI() {
final String sStyle = "@import '/folder/foobar.css';\n" + "div{background:fixed url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAALEgAACxIB0t1+/AAAAAd0SU1FB9EFBAoYMhVvMQIAAAAtSURBVHicY/z//z8DHoBH+v///yy4FDEyMjIwMDDhM3lgpaEuh7gTEzDiDxYA9HEPDF90e5YAAAAASUVORK5CYII=) !important;}\n" + "span { background-image:url('/my/folder/b.gif');}";
final CascadingStyleSheet aCSS = CSSReader.readFromString(sStyle, ECSSVersion.CSS30);
CSSVisitor.visitCSSUrl(aCSS, new DefaultCSSUrlVisitor() {
// Called for each import
@Override
public void onImport(@Nonnull final CSSImportRule aImportRule) {
System.out.println("Import: " + aImportRule.getLocationString());
}
// Call for URLs outside of URLs
@Override
public void onUrlDeclaration(@Nullable final ICSSTopLevelRule aTopLevelRule, @Nonnull final CSSDeclaration aDeclaration, @Nonnull final CSSExpressionMemberTermURI aURITerm) {
final CSSURI aURI = aURITerm.getURI();
if (aURI.isDataURL()) {
final CSSDataURL aDataURL = aURI.getAsDataURL();
System.out.println(aDeclaration.getProperty() + " - references data URL with " + aDataURL.getContentLength() + " bytes of content");
} else
System.out.println(aDeclaration.getProperty() + " - references regular URL: " + aURI.getURI());
}
});
}
use of com.helger.css.decl.ICSSTopLevelRule in project ph-css by phax.
the class WikiVisitUrls method readFromStyleAttributeWithAPI.
public void readFromStyleAttributeWithAPI() {
final String sStyle = "@import 'foobar.css';\n" + "div{background:fixed url(a.gif) !important;}\n" + "span { background-image:url('/my/folder/b.gif');}";
final CascadingStyleSheet aCSS = CSSReader.readFromString(sStyle, ECSSVersion.CSS30);
CSSVisitor.visitCSSUrl(aCSS, new DefaultCSSUrlVisitor() {
// Called for each import
@Override
public void onImport(@Nonnull final CSSImportRule aImportRule) {
System.out.println("Import: " + aImportRule.getLocationString() + " - " + getSourceLocationString(aImportRule.getSourceLocation()));
}
// Call for URLs outside of URLs
@Override
public void onUrlDeclaration(@Nullable final ICSSTopLevelRule aTopLevelRule, @Nonnull final CSSDeclaration aDeclaration, @Nonnull final CSSExpressionMemberTermURI aURITerm) {
System.out.println(aDeclaration.getProperty() + " - references: " + aURITerm.getURIString() + " - " + getSourceLocationString(aURITerm.getSourceLocation()));
}
});
}
use of com.helger.css.decl.ICSSTopLevelRule in project ph-css by phax.
the class CSSVisitorDeclarationListFuncTest method testModifyingCSSUrlVisitor.
@Test
public void testModifyingCSSUrlVisitor() {
final CSSDeclarationList aCSS = CSSReaderDeclarationList.readFromString("background:url(a.gif);background:url(b.gif);", ECSSVersion.CSS30);
assertNotNull(aCSS);
// Append ".modified" to all URLs
final MockModifyingCSSUrlVisitor aVisitor2 = new MockModifyingCSSUrlVisitor();
CSSVisitor.visitAllDeclarationUrls(aCSS, aVisitor2);
// Check the result
assertEquals("background:url(a.gif.modified);background:url(b.gif.modified)", new CSSWriter(ECSSVersion.CSS30, true).getCSSAsString(aCSS));
// Re-iterate to check twice
CSSVisitor.visitAllDeclarationUrls(aCSS, new DefaultCSSUrlVisitor() {
@Override
public void onImport(@Nonnull final CSSImportRule aImportRule) {
assertTrue(aImportRule.getLocationString().endsWith(".modified"));
}
@Override
public void onUrlDeclaration(@Nullable final ICSSTopLevelRule aTopLevelRule, @Nonnull final CSSDeclaration aDeclaration, @Nonnull final CSSExpressionMemberTermURI aURITerm) {
assertTrue(aURITerm.getURIString().endsWith(".modified"));
}
});
}
Aggregations