Search in sources :

Example 1 with CascadingStyleSheet

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);
        }
    }
}
Also used : CascadingStyleSheet(com.phloc.css.decl.CascadingStyleSheet) ByteArrayInputStreamProvider(com.phloc.commons.io.streamprovider.ByteArrayInputStreamProvider) CSSImportRule(com.phloc.css.decl.CSSImportRule) XStream(com.thoughtworks.xstream.XStream) Charset(java.nio.charset.Charset) CCharset(com.phloc.commons.charset.CCharset) TokenMgrError(com.phloc.css.parser.TokenMgrError)

Example 2 with CascadingStyleSheet

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);
    }
}
Also used : CascadingStyleSheet(com.phloc.css.decl.CascadingStyleSheet) EvidenceElement(org.asqatasun.entity.audit.EvidenceElement) IOException(java.io.IOException) File(java.io.File) CSSParserExceptionHandlerImpl(org.asqatasun.contentadapter.css.CSSParserExceptionHandlerImpl)

Aggregations

CascadingStyleSheet (com.phloc.css.decl.CascadingStyleSheet)2 CCharset (com.phloc.commons.charset.CCharset)1 ByteArrayInputStreamProvider (com.phloc.commons.io.streamprovider.ByteArrayInputStreamProvider)1 CSSImportRule (com.phloc.css.decl.CSSImportRule)1 TokenMgrError (com.phloc.css.parser.TokenMgrError)1 XStream (com.thoughtworks.xstream.XStream)1 File (java.io.File)1 IOException (java.io.IOException)1 Charset (java.nio.charset.Charset)1 CSSParserExceptionHandlerImpl (org.asqatasun.contentadapter.css.CSSParserExceptionHandlerImpl)1 EvidenceElement (org.asqatasun.entity.audit.EvidenceElement)1