|
| 1 | +package com.itextpdf.styledxmlparser.css.parse; |
| 2 | + |
| 3 | +import com.itextpdf.styledxmlparser.IXmlParser; |
| 4 | +import com.itextpdf.styledxmlparser.node.IAttributes; |
| 5 | +import com.itextpdf.styledxmlparser.node.IDocumentNode; |
| 6 | +import com.itextpdf.styledxmlparser.node.IElementNode; |
| 7 | +import com.itextpdf.styledxmlparser.node.INode; |
| 8 | +import com.itextpdf.styledxmlparser.node.impl.jsoup.JsoupHtmlParser; |
| 9 | +import com.itextpdf.test.ExtendedITextTest; |
| 10 | +import com.itextpdf.test.annotations.type.IntegrationTest; |
| 11 | + |
| 12 | +import java.io.FileInputStream; |
| 13 | +import java.io.IOException; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.List; |
| 16 | +import org.junit.Assert; |
| 17 | +import org.junit.Test; |
| 18 | +import org.junit.experimental.categories.Category; |
| 19 | + |
| 20 | +@Category(IntegrationTest.class) |
| 21 | +public class CssStyleAttributeParseTest extends ExtendedITextTest { |
| 22 | + private static final String SOURCE_FOLDER = "./src/test/resources/com/itextpdf/styledxmlparser/css/parse/CssStyleAttributeParseTest/"; |
| 23 | + |
| 24 | + @Test |
| 25 | + public void styleAttributeParseTest() throws IOException { |
| 26 | + String fileName = SOURCE_FOLDER + "cssStyleAttributeParse.html"; |
| 27 | + |
| 28 | + IXmlParser parser = new JsoupHtmlParser(); |
| 29 | + IDocumentNode document = parser.parse(new FileInputStream(fileName), "UTF-8"); |
| 30 | + |
| 31 | + List<String> styleDeclarations = new ArrayList<>(); |
| 32 | + |
| 33 | + List<String> expectStyleDeclarations = new ArrayList<>(); |
| 34 | + expectStyleDeclarations.add("display:none;"); |
| 35 | + expectStyleDeclarations.add("position:relative;"); |
| 36 | + expectStyleDeclarations.add("display:none"); |
| 37 | + expectStyleDeclarations.add("text-align:center;"); |
| 38 | + expectStyleDeclarations.add("white-space:nowrap;"); |
| 39 | + expectStyleDeclarations |
| 40 | + .add("float:right; clear:right; width:22.0em; margin:0 0 1.0em 1.0em; background:#f9f9f9;" |
| 41 | + + " border:1px solid #aaa; padding:0.2em; border-spacing:0.4em 0; text-align:center;" |
| 42 | + + " line-height:1.4em; font-size:88%;"); |
| 43 | + expectStyleDeclarations.add("padding:0.2em 0.4em 0.2em; font-size:145%; line-height:1.15em; font-weight:bold;" |
| 44 | + + " display:block; margin-bottom:0.25em;"); |
| 45 | + |
| 46 | + parseStyleAttrForSubtree(document, styleDeclarations); |
| 47 | + |
| 48 | + Assert.assertEquals(styleDeclarations.size(), expectStyleDeclarations.size()); |
| 49 | + |
| 50 | + for (int i = 0; i < expectStyleDeclarations.size(); i++) { |
| 51 | + Assert.assertEquals(expectStyleDeclarations.get(i), styleDeclarations.get(i)); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private void parseOwnStyleAttr(IElementNode element, List<String> styleDeclarations) { |
| 56 | + IAttributes attributes = element.getAttributes(); |
| 57 | + String styleAttr = attributes.getAttribute("style"); |
| 58 | + |
| 59 | + if (styleAttr != null && styleAttr.length() > 0) { |
| 60 | + styleDeclarations.add(styleAttr); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private void parseStyleAttrForSubtree(INode node, List<String> styleDeclarations) { |
| 65 | + if (node instanceof IElementNode) { |
| 66 | + parseOwnStyleAttr((IElementNode) node, styleDeclarations); |
| 67 | + } |
| 68 | + for (INode child : node.childNodes()) { |
| 69 | + parseStyleAttrForSubtree(child, styleDeclarations); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
0 commit comments