|
33 | 33 |
|
34 | 34 | import io.sf.carte.doc.style.css.CSSComputedProperties; |
35 | 35 | import io.sf.carte.doc.style.css.CSSElement; |
| 36 | +import io.sf.carte.doc.style.css.CSSPropertyDefinition; |
36 | 37 | import io.sf.carte.doc.style.css.CSSStyleDeclaration; |
37 | 38 | import io.sf.carte.doc.style.css.CSSStyleRule; |
38 | 39 | import io.sf.carte.doc.style.css.CSSTypedValue; |
39 | 40 | import io.sf.carte.doc.style.css.CSSUnit; |
40 | 41 | import io.sf.carte.doc.style.css.CSSValue; |
| 42 | +import io.sf.carte.doc.style.css.CSSValueSyntax; |
41 | 43 | import io.sf.carte.doc.style.css.DocumentCSSStyleSheet; |
42 | 44 | import io.sf.carte.doc.style.css.StyleDeclarationErrorHandler; |
| 45 | +import io.sf.carte.doc.style.css.nsac.CSSParseException; |
43 | 46 | import io.sf.carte.doc.style.css.nsac.InputSource; |
| 47 | +import io.sf.carte.doc.style.css.nsac.LexicalUnit; |
44 | 48 | import io.sf.carte.doc.style.css.om.AbstractCSSRule; |
45 | 49 | import io.sf.carte.doc.style.css.om.AbstractCSSStyleDeclaration; |
46 | 50 | import io.sf.carte.doc.style.css.om.AbstractCSSStyleSheet; |
47 | 51 | import io.sf.carte.doc.style.css.om.BaseCSSDeclarationRule; |
| 52 | +import io.sf.carte.doc.style.css.om.CSSOMParser; |
48 | 53 | import io.sf.carte.doc.style.css.om.CSSRuleArrayList; |
49 | 54 | import io.sf.carte.doc.style.css.om.ComputedCSSStyle; |
50 | 55 | import io.sf.carte.doc.style.css.om.DOMCSSStyleSheetFactoryTest; |
51 | 56 | import io.sf.carte.doc.style.css.om.MediaRule; |
| 57 | +import io.sf.carte.doc.style.css.parser.SyntaxParser; |
| 58 | +import io.sf.carte.doc.style.css.property.LexicalValue; |
52 | 59 |
|
53 | 60 | public class XHTMLDocumentTest { |
54 | 61 |
|
@@ -286,6 +293,99 @@ public void getOverrideStyle() { |
286 | 293 | style.getMinifiedCssText()); |
287 | 294 | } |
288 | 295 |
|
| 296 | + @Test |
| 297 | + public void testComputedStyleRegisteredProperties() throws CSSParseException, IOException { |
| 298 | + // Prepare property definition |
| 299 | + SyntaxParser syntaxParser = new SyntaxParser(); |
| 300 | + CSSValueSyntax syn = syntaxParser.parseSyntax("<length>"); |
| 301 | + // |
| 302 | + CSSOMParser parser = new CSSOMParser(); |
| 303 | + LexicalUnit lunit = parser.parsePropertyValue(new StringReader("15pt")); |
| 304 | + LexicalValue value = new LexicalValue(); |
| 305 | + value.setLexicalUnit(lunit); |
| 306 | + CSSPropertyDefinition pdef = xhtmlDoc.getStyleSheet().getStyleSheetFactory().createPropertyDefinition("--foo", |
| 307 | + syn, false, value); |
| 308 | + xhtmlDoc.registerProperty(pdef); |
| 309 | + // |
| 310 | + XHTMLElement elm = xhtmlDoc.getElementById("div1"); |
| 311 | + assertNotNull(elm); |
| 312 | + /* |
| 313 | + * custom property substitution. |
| 314 | + */ |
| 315 | + elm.getOverrideStyle(null).setCssText("margin-left:var(--foo)"); |
| 316 | + CSSComputedProperties style = elm.getComputedStyle(null); |
| 317 | + CSSTypedValue marginLeft = (CSSTypedValue) style.getPropertyCSSValue("margin-left"); |
| 318 | + assertEquals(15f, marginLeft.getFloatValue(CSSUnit.CSS_PT), 0.01f); |
| 319 | + assertFalse(xhtmlDoc.getErrorHandler().hasComputedStyleErrors(elm)); |
| 320 | + assertFalse(xhtmlDoc.getErrorHandler().hasComputedStyleErrors()); |
| 321 | + // |
| 322 | + elm.getOverrideStyle(null).setCssText("margin-left:var(--foo,7pt)"); |
| 323 | + style = elm.getComputedStyle(null); |
| 324 | + marginLeft = (CSSTypedValue) style.getPropertyCSSValue("margin-left"); |
| 325 | + assertEquals(7f, marginLeft.getFloatValue(CSSUnit.CSS_PT), 0.01f); |
| 326 | + assertFalse(xhtmlDoc.getErrorHandler().hasComputedStyleErrors(elm)); |
| 327 | + assertFalse(xhtmlDoc.getErrorHandler().hasComputedStyleErrors()); |
| 328 | + // |
| 329 | + elm.getOverrideStyle(null).setCssText("margin-left:var(--foo,1vb);--foo:8pt"); |
| 330 | + style = elm.getComputedStyle(null); |
| 331 | + marginLeft = (CSSTypedValue) style.getPropertyCSSValue("margin-left"); |
| 332 | + assertEquals(8f, marginLeft.getFloatValue(CSSUnit.CSS_PT), 0.01f); |
| 333 | + assertFalse(xhtmlDoc.getErrorHandler().hasComputedStyleErrors(elm)); |
| 334 | + assertFalse(xhtmlDoc.getErrorHandler().hasComputedStyleErrors()); |
| 335 | + // |
| 336 | + XHTMLElement listpara = xhtmlDoc.getElementById("listpara"); |
| 337 | + listpara.getOverrideStyle(null).setCssText("font-size:var(--foo,19pt)"); |
| 338 | + style = listpara.getComputedStyle(null); |
| 339 | + CSSTypedValue customProperty = (CSSTypedValue) style.getPropertyCSSValue("--foo"); |
| 340 | + assertNotNull(customProperty); |
| 341 | + assertEquals(15f, customProperty.getFloatValue(CSSUnit.CSS_PT), 1e-6); |
| 342 | + CSSTypedValue fontSize = (CSSTypedValue) style.getPropertyCSSValue("font-size"); |
| 343 | + assertEquals(19f, fontSize.getFloatValue(CSSUnit.CSS_PT), 0.01f); |
| 344 | + assertFalse(xhtmlDoc.getErrorHandler().hasComputedStyleErrors(listpara)); |
| 345 | + assertFalse(xhtmlDoc.getErrorHandler().hasComputedStyleErrors()); |
| 346 | + /* |
| 347 | + * Same as above, with custom property set in parent style |
| 348 | + */ |
| 349 | + xhtmlDoc.getErrorHandler().resetComputedStyleErrors(); |
| 350 | + XHTMLElement body = (XHTMLElement) elm.getParentNode(); |
| 351 | + body.getOverrideStyle(null).setCssText("--foo:9pt"); |
| 352 | + elm.getOverrideStyle(null).setCssText("margin-left:var(--foo)"); |
| 353 | + style = elm.getComputedStyle(null); |
| 354 | + marginLeft = (CSSTypedValue) style.getPropertyCSSValue("margin-left"); |
| 355 | + assertEquals(15f, marginLeft.getFloatValue(CSSUnit.CSS_PT), 0.01f); |
| 356 | + assertFalse(xhtmlDoc.getErrorHandler().hasComputedStyleErrors(elm)); |
| 357 | + assertFalse(xhtmlDoc.getErrorHandler().hasComputedStyleErrors()); |
| 358 | + /* |
| 359 | + * Same as above, with custom property set in parent style, fallback |
| 360 | + */ |
| 361 | + xhtmlDoc.getErrorHandler().resetComputedStyleErrors(); |
| 362 | + body = (XHTMLElement) elm.getParentNode(); |
| 363 | + elm.getOverrideStyle(null).setCssText("margin-left:var(--foo,21pt)"); |
| 364 | + style = elm.getComputedStyle(null); |
| 365 | + marginLeft = (CSSTypedValue) style.getPropertyCSSValue("margin-left"); |
| 366 | + assertEquals(21f, marginLeft.getFloatValue(CSSUnit.CSS_PT), 0.01f); |
| 367 | + assertFalse(xhtmlDoc.getErrorHandler().hasComputedStyleErrors(elm)); |
| 368 | + assertFalse(xhtmlDoc.getErrorHandler().hasComputedStyleErrors()); |
| 369 | + /* |
| 370 | + * custom property substitution, var() in fallback. |
| 371 | + */ |
| 372 | + elm.getOverrideStyle(null).setCssText("margin-left:var(--no-way,var(--foo));"); |
| 373 | + style = elm.getComputedStyle(null); |
| 374 | + marginLeft = (CSSTypedValue) style.getPropertyCSSValue("margin-left"); |
| 375 | + assertEquals(15f, marginLeft.getFloatValue(CSSUnit.CSS_PT), 0.01f); |
| 376 | + assertFalse(xhtmlDoc.getErrorHandler().hasComputedStyleErrors(elm)); |
| 377 | + assertFalse(xhtmlDoc.getErrorHandler().hasComputedStyleErrors()); |
| 378 | + /* |
| 379 | + * custom property substitution, var() in fallback, fallback-of-fallback. |
| 380 | + */ |
| 381 | + elm.getOverrideStyle(null).setCssText("margin-left:var(--no-way,var(--foo,17pt));"); |
| 382 | + style = elm.getComputedStyle(null); |
| 383 | + marginLeft = (CSSTypedValue) style.getPropertyCSSValue("margin-left"); |
| 384 | + assertEquals(17f, marginLeft.getFloatValue(CSSUnit.CSS_PT), 0.01f); |
| 385 | + assertFalse(xhtmlDoc.getErrorHandler().hasComputedStyleErrors(elm)); |
| 386 | + assertFalse(xhtmlDoc.getErrorHandler().hasComputedStyleErrors()); |
| 387 | + } |
| 388 | + |
289 | 389 | @Test |
290 | 390 | public void testComputedStyleAttr() { |
291 | 391 | XHTMLElement elm = xhtmlDoc.getElementById("firstH3"); |
|
0 commit comments