|
45 | 45 | import io.sf.carte.doc.style.css.nsac.CSSParseException; |
46 | 46 | import io.sf.carte.doc.style.css.nsac.InputSource; |
47 | 47 | import io.sf.carte.doc.style.css.nsac.LexicalUnit; |
| 48 | +import io.sf.carte.doc.style.css.nsac.SelectorList; |
48 | 49 | import io.sf.carte.doc.style.css.om.AbstractCSSRule; |
49 | 50 | import io.sf.carte.doc.style.css.om.AbstractCSSStyleDeclaration; |
50 | 51 | import io.sf.carte.doc.style.css.om.AbstractCSSStyleSheet; |
51 | 52 | import io.sf.carte.doc.style.css.om.BaseCSSDeclarationRule; |
| 53 | +import io.sf.carte.doc.style.css.om.BaseCSSStyleSheet; |
52 | 54 | import io.sf.carte.doc.style.css.om.CSSOMParser; |
53 | 55 | import io.sf.carte.doc.style.css.om.CSSRuleArrayList; |
54 | 56 | import io.sf.carte.doc.style.css.om.ComputedCSSStyle; |
55 | 57 | import io.sf.carte.doc.style.css.om.DOMCSSStyleSheetFactoryTest; |
56 | 58 | import io.sf.carte.doc.style.css.om.MediaRule; |
| 59 | +import io.sf.carte.doc.style.css.om.PropertyCountVisitor; |
| 60 | +import io.sf.carte.doc.style.css.om.StyleCountVisitor; |
| 61 | +import io.sf.carte.doc.style.css.om.StyleRule; |
| 62 | +import io.sf.carte.doc.style.css.parser.CSSParser; |
57 | 63 | import io.sf.carte.doc.style.css.parser.SyntaxParser; |
58 | 64 | import io.sf.carte.doc.style.css.property.LexicalValue; |
| 65 | +import io.sf.carte.doc.style.css.property.TypedValue; |
59 | 66 |
|
60 | 67 | public class XHTMLDocumentTest { |
61 | 68 |
|
@@ -893,6 +900,60 @@ public void testCascade() throws IOException { |
893 | 900 | assertEquals("#8a2be2", style.getPropertyValue("color")); |
894 | 901 | } |
895 | 902 |
|
| 903 | + @Test |
| 904 | + public void testCascade2() throws IOException { |
| 905 | + BaseCSSStyleSheet sheet = (BaseCSSStyleSheet) xhtmlDoc.getStyleSheets().item(5); |
| 906 | + |
| 907 | + // Obtain the rule where a value is declared |
| 908 | + CSSParser parser = new CSSParser(); |
| 909 | + SelectorList selist = parser.parseSelectors("p.boldmargin"); |
| 910 | + StyleRule rule = (StyleRule) sheet.getFirstStyleRule(selist); |
| 911 | + assertNotNull(rule); |
| 912 | + |
| 913 | + AbstractCSSStyleDeclaration declStyle = rule.getStyle(); |
| 914 | + TypedValue declMarginLeft = (TypedValue) declStyle.getPropertyCSSValue("margin-left"); |
| 915 | + assertEquals("2%", declMarginLeft.getCssText()); |
| 916 | + |
| 917 | + /* |
| 918 | + * Get an element that obtains the above value as computed style |
| 919 | + */ |
| 920 | + XHTMLElement elm = xhtmlDoc.getElementById("para1"); |
| 921 | + assertNotNull(elm); |
| 922 | + CSSStyleDeclaration style = elm.getComputedStyle(null); |
| 923 | + assertEquals("2%", style.getPropertyValue("margin-left")); |
| 924 | + |
| 925 | + // Change the value itself |
| 926 | + declMarginLeft.setFloatValue(CSSUnit.CSS_PX, 6f); |
| 927 | + style = elm.getComputedStyle(null); |
| 928 | + assertEquals("6px", style.getPropertyValue("margin-left")); |
| 929 | + |
| 930 | + // Overwrite the property's value |
| 931 | + declStyle.setProperty("margin-left", "4px", null); |
| 932 | + style = elm.getComputedStyle(null); |
| 933 | + // The new value is not there yet |
| 934 | + assertEquals("6px", style.getPropertyValue("margin-left")); |
| 935 | + |
| 936 | + // Rebuild the cascade |
| 937 | + xhtmlDoc.rebuildCascade(); |
| 938 | + style = elm.getComputedStyle(null); |
| 939 | + assertEquals("4px", style.getPropertyValue("margin-left")); |
| 940 | + } |
| 941 | + |
| 942 | + @Test |
| 943 | + public void testVisitors() throws IOException { |
| 944 | + StyleCountVisitor visitor = new StyleCountVisitor(); |
| 945 | + xhtmlDoc.getStyleSheets().acceptStyleRuleVisitor(visitor); |
| 946 | + assertEquals(29, visitor.getCount()); |
| 947 | + // |
| 948 | + PropertyCountVisitor visitorP = new PropertyCountVisitor(); |
| 949 | + xhtmlDoc.getStyleSheets().acceptDeclarationRuleVisitor(visitorP); |
| 950 | + assertEquals(111, visitorP.getCount()); |
| 951 | + // |
| 952 | + visitorP.reset(); |
| 953 | + xhtmlDoc.getStyleSheets().acceptDescriptorRuleVisitor(visitorP); |
| 954 | + assertEquals(2, visitorP.getCount()); |
| 955 | + } |
| 956 | + |
896 | 957 | @Test |
897 | 958 | public void testStyleElement() { |
898 | 959 | StyleElement style = (StyleElement) xhtmlDoc.getElementsByTagName("style").item(0); |
|
0 commit comments