Skip to content

Commit 7ac1602

Browse files
committed
Implement registered custom properties.
1 parent af559aa commit 7ac1602

File tree

2 files changed

+122
-3
lines changed

2 files changed

+122
-3
lines changed

junit/io/sf/carte/doc/dom4j/XHTMLDocumentTest.java

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,29 @@
3333

3434
import io.sf.carte.doc.style.css.CSSComputedProperties;
3535
import io.sf.carte.doc.style.css.CSSElement;
36+
import io.sf.carte.doc.style.css.CSSPropertyDefinition;
3637
import io.sf.carte.doc.style.css.CSSStyleDeclaration;
3738
import io.sf.carte.doc.style.css.CSSStyleRule;
3839
import io.sf.carte.doc.style.css.CSSTypedValue;
3940
import io.sf.carte.doc.style.css.CSSUnit;
4041
import io.sf.carte.doc.style.css.CSSValue;
42+
import io.sf.carte.doc.style.css.CSSValueSyntax;
4143
import io.sf.carte.doc.style.css.DocumentCSSStyleSheet;
4244
import io.sf.carte.doc.style.css.StyleDeclarationErrorHandler;
45+
import io.sf.carte.doc.style.css.nsac.CSSParseException;
4346
import io.sf.carte.doc.style.css.nsac.InputSource;
47+
import io.sf.carte.doc.style.css.nsac.LexicalUnit;
4448
import io.sf.carte.doc.style.css.om.AbstractCSSRule;
4549
import io.sf.carte.doc.style.css.om.AbstractCSSStyleDeclaration;
4650
import io.sf.carte.doc.style.css.om.AbstractCSSStyleSheet;
4751
import io.sf.carte.doc.style.css.om.BaseCSSDeclarationRule;
52+
import io.sf.carte.doc.style.css.om.CSSOMParser;
4853
import io.sf.carte.doc.style.css.om.CSSRuleArrayList;
4954
import io.sf.carte.doc.style.css.om.ComputedCSSStyle;
5055
import io.sf.carte.doc.style.css.om.DOMCSSStyleSheetFactoryTest;
5156
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;
5259

5360
public class XHTMLDocumentTest {
5461

@@ -286,6 +293,99 @@ public void getOverrideStyle() {
286293
style.getMinifiedCssText());
287294
}
288295

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+
289389
@Test
290390
public void testComputedStyleAttr() {
291391
XHTMLElement elm = xhtmlDoc.getElementById("firstH3");

src/io/sf/carte/doc/dom4j/XHTMLDocument.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.net.URL;
1818
import java.net.URLConnection;
1919
import java.util.HashMap;
20+
import java.util.HashSet;
2021
import java.util.Iterator;
2122
import java.util.LinkedHashSet;
2223
import java.util.Map;
@@ -37,6 +38,7 @@
3738
import io.sf.carte.doc.style.css.CSSCanvas;
3839
import io.sf.carte.doc.style.css.CSSDocument;
3940
import io.sf.carte.doc.style.css.CSSMediaException;
41+
import io.sf.carte.doc.style.css.CSSPropertyDefinition;
4042
import io.sf.carte.doc.style.css.DocumentCSSStyleSheet;
4143
import io.sf.carte.doc.style.css.ErrorHandler;
4244
import io.sf.carte.doc.style.css.LinkStyle;
@@ -63,13 +65,15 @@ public class XHTMLDocument extends DOMDocument implements CSSDocument {
6365

6466
private static final long serialVersionUID = 7L;
6567

68+
private String documentURI = null;
69+
70+
private URL baseURL = null;
71+
6672
private DocumentCSSStyleSheet mergedStyleSheet = null;
6773

6874
private int styleCacheSerial = Integer.MIN_VALUE;
6975

70-
private String documentURI = null;
71-
72-
private URL baseURL = null;
76+
private Set<CSSPropertyDefinition> registeredPropertySet = null;
7377

7478
Set<LinkElement> linkedStyle = new LinkedHashSet<LinkElement>(4);
7579

@@ -163,6 +167,15 @@ public Object setUserData(String key, Object data, UserDataHandler handler) {
163167
return null;
164168
}
165169

170+
@Override
171+
public void registerProperty(CSSPropertyDefinition definition) {
172+
if (registeredPropertySet == null) {
173+
registeredPropertySet = new HashSet<>();
174+
}
175+
registeredPropertySet.add(definition);
176+
mergedStyleSheet = null;
177+
}
178+
166179
/**
167180
* A list containing all the style sheets explicitly linked into or embedded
168181
* in a document. For HTML documents, this includes external style sheets,
@@ -248,6 +261,12 @@ private void mergeStyleSheets() {
248261
while (it.hasNext()) {
249262
mergedStyleSheet.addStyleSheet(it.next());
250263
}
264+
// Add DOM property definitions
265+
if (registeredPropertySet != null) {
266+
for (CSSPropertyDefinition def : registeredPropertySet) {
267+
mergedStyleSheet.registerProperty(def);
268+
}
269+
}
251270
}
252271

253272
/**

0 commit comments

Comments
 (0)