use of com.phloc.css.decl.CascadingStyleSheet in project Asqatasun by Asqatasun.
the class CSSJsoupPhlocContentAdapterImpl method adaptContent.
/**
* For shared css, the adaptation is only made the first time the css is
* encountered.
*
* @param stylesheetContent
* @param resource
* @param currentLocalResourcePath
* @param mediaAttributeValue
*
*/
private void adaptContent(StylesheetContent stylesheetContent, Resource resource, String currentLocalResourcePath, @Nullable List<CSSMediaQuery> mediaList) {
if (stylesheetContent.getAdaptedContent() == null && resource.getResource() != null && StringUtils.isNotBlank(resource.getResource())) {
Charset charset = null;
try {
charset = CSSReader.getCharsetDeclaredInCSS(new ByteArrayInputStreamProvider(resource.getResource().getBytes()));
LOGGER.debug("is css valid CSS2 " + CSSReader.isValidCSS(resource.getResource(), ECSSVersion.CSS21) + " " + stylesheetContent.getURI());
LOGGER.debug("is css valid CSS3 " + CSSReader.isValidCSS(resource.getResource(), ECSSVersion.CSS30) + " " + stylesheetContent.getURI());
} catch (TokenMgrError tme) {
LOGGER.debug(resource.getResource() + " is on error, so invalid");
LOGGER.debug(tme.getMessage());
}
if (charset == null) {
charset = Charset.forName("utf-8");
}
try {
CascadingStyleSheet aCSS = CSSReader.readFromString(resource.getResource(), charset, ECSSVersion.CSS30, new CSSParserExceptionHandlerImpl(stylesheetContent));
// has been set to "on error" and so is not null
if (stylesheetContent.getAdaptedContent() == null) {
if (CollectionUtils.isNotEmpty(mediaList) && MediaQueryTools.canWrapInMediaQuery(aCSS)) {
if (MediaQueryTools.canWrapInMediaQuery(aCSS)) {
aCSS = MediaQueryTools.getWrappedInMediaQuery(aCSS, mediaList);
} else {
LOGGER.warn(stylesheetContent.getURI() + " should be" + "wrapped into " + mediaList + " but it " + "is impossible");
}
}
stylesheetContent.setAdaptedContent(new XStream().toXML(aCSS));
if (aCSS.hasImportRules()) {
for (CSSImportRule cssImportRule : aCSS.getAllImportRules()) {
getImportedResources(cssImportRule, currentLocalResourcePath);
}
}
}
} catch (IllegalArgumentException | TokenMgrError iae) {
stylesheetContent.setAdaptedContent(CSS_ON_ERROR);
}
}
}
use of com.phloc.css.decl.CascadingStyleSheet in project Asqatasun by Asqatasun.
the class CssPropertyPresenceCheckerTest method initCheckerAndLaunch.
/**
*
* @param fileName
* @param resultOnDetection
*/
private void initCheckerAndLaunch(String fileName, String[] pseudoSelectors, TestSolution resultOnDetection, String selector) {
try {
String styleSheetName = fileName.substring(fileName.lastIndexOf("/") + 1);
expect(mockTestSolutionHandler.getTestSolution()).andReturn(resultOnDetection).times(2);
mockTestSolutionHandler.addTestSolution(resultOnDetection);
expectLastCall().anyTimes();
if (StringUtils.isNotBlank(selector)) {
EvidenceElement eElement1 = createMock(EvidenceElement.class);
EvidenceElement eElement2 = createMock(EvidenceElement.class);
expect(mockProcessRemarkService.getEvidenceElement(EvidenceStore.CSS_SELECTOR_EE, selector)).andReturn(eElement1);
expect(mockProcessRemarkService.getEvidenceElement(EvidenceStore.CSS_FILENAME_EE, styleSheetName)).andReturn(eElement2);
mockProcessRemarkService.addSourceCodeRemark(resultOnDetection, "content attribute not empty", contentAttributeDectected, eElement1, eElement2);
expectLastCall().once();
EasyMock.replay(mockProcessRemarkService);
}
EasyMock.replay(mockTestSolutionHandler);
CascadingStyleSheet cascadingStyleSheet = CSSReader.readFromString(FileUtils.readFileToString(new File(fileName)), Charset.defaultCharset(), ECSSVersion.CSS30, new CSSParserExceptionHandlerImpl(stylesheetContent));
Collection<String> pseudoSelectorList;
if (pseudoSelectors == null) {
pseudoSelectorList = new ArrayList<>();
} else {
pseudoSelectorList = Arrays.asList(pseudoSelectors);
}
CssPropertyPresenceChecker instance = new CssPropertyPresenceChecker(AttributeStore.CONTENT_ATTR, pseudoSelectorList, resultOnDetection, contentAttributeDectected);
instance.check(mockSSPHandler, styleSheetName, cascadingStyleSheet, mockTestSolutionHandler);
} catch (IOException ex) {
LOGGER.warn(ex);
}
}
Aggregations