Skip to content

Commit

Permalink
Implement registered custom properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosame committed Feb 25, 2021
1 parent af559aa commit 7ac1602
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 3 deletions.
100 changes: 100 additions & 0 deletions junit/io/sf/carte/doc/dom4j/XHTMLDocumentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,29 @@

import io.sf.carte.doc.style.css.CSSComputedProperties;
import io.sf.carte.doc.style.css.CSSElement;
import io.sf.carte.doc.style.css.CSSPropertyDefinition;
import io.sf.carte.doc.style.css.CSSStyleDeclaration;
import io.sf.carte.doc.style.css.CSSStyleRule;
import io.sf.carte.doc.style.css.CSSTypedValue;
import io.sf.carte.doc.style.css.CSSUnit;
import io.sf.carte.doc.style.css.CSSValue;
import io.sf.carte.doc.style.css.CSSValueSyntax;
import io.sf.carte.doc.style.css.DocumentCSSStyleSheet;
import io.sf.carte.doc.style.css.StyleDeclarationErrorHandler;
import io.sf.carte.doc.style.css.nsac.CSSParseException;
import io.sf.carte.doc.style.css.nsac.InputSource;
import io.sf.carte.doc.style.css.nsac.LexicalUnit;
import io.sf.carte.doc.style.css.om.AbstractCSSRule;
import io.sf.carte.doc.style.css.om.AbstractCSSStyleDeclaration;
import io.sf.carte.doc.style.css.om.AbstractCSSStyleSheet;
import io.sf.carte.doc.style.css.om.BaseCSSDeclarationRule;
import io.sf.carte.doc.style.css.om.CSSOMParser;
import io.sf.carte.doc.style.css.om.CSSRuleArrayList;
import io.sf.carte.doc.style.css.om.ComputedCSSStyle;
import io.sf.carte.doc.style.css.om.DOMCSSStyleSheetFactoryTest;
import io.sf.carte.doc.style.css.om.MediaRule;
import io.sf.carte.doc.style.css.parser.SyntaxParser;
import io.sf.carte.doc.style.css.property.LexicalValue;

public class XHTMLDocumentTest {

Expand Down Expand Up @@ -286,6 +293,99 @@ public void getOverrideStyle() {
style.getMinifiedCssText());
}

@Test
public void testComputedStyleRegisteredProperties() throws CSSParseException, IOException {
// Prepare property definition
SyntaxParser syntaxParser = new SyntaxParser();
CSSValueSyntax syn = syntaxParser.parseSyntax("<length>");
//
CSSOMParser parser = new CSSOMParser();
LexicalUnit lunit = parser.parsePropertyValue(new StringReader("15pt"));
LexicalValue value = new LexicalValue();
value.setLexicalUnit(lunit);
CSSPropertyDefinition pdef = xhtmlDoc.getStyleSheet().getStyleSheetFactory().createPropertyDefinition("--foo",
syn, false, value);
xhtmlDoc.registerProperty(pdef);
//
XHTMLElement elm = xhtmlDoc.getElementById("div1");
assertNotNull(elm);
/*
* custom property substitution.
*/
elm.getOverrideStyle(null).setCssText("margin-left:var(--foo)");
CSSComputedProperties style = elm.getComputedStyle(null);
CSSTypedValue marginLeft = (CSSTypedValue) style.getPropertyCSSValue("margin-left");
assertEquals(15f, marginLeft.getFloatValue(CSSUnit.CSS_PT), 0.01f);
assertFalse(xhtmlDoc.getErrorHandler().hasComputedStyleErrors(elm));
assertFalse(xhtmlDoc.getErrorHandler().hasComputedStyleErrors());
//
elm.getOverrideStyle(null).setCssText("margin-left:var(--foo,7pt)");
style = elm.getComputedStyle(null);
marginLeft = (CSSTypedValue) style.getPropertyCSSValue("margin-left");
assertEquals(7f, marginLeft.getFloatValue(CSSUnit.CSS_PT), 0.01f);
assertFalse(xhtmlDoc.getErrorHandler().hasComputedStyleErrors(elm));
assertFalse(xhtmlDoc.getErrorHandler().hasComputedStyleErrors());
//
elm.getOverrideStyle(null).setCssText("margin-left:var(--foo,1vb);--foo:8pt");
style = elm.getComputedStyle(null);
marginLeft = (CSSTypedValue) style.getPropertyCSSValue("margin-left");
assertEquals(8f, marginLeft.getFloatValue(CSSUnit.CSS_PT), 0.01f);
assertFalse(xhtmlDoc.getErrorHandler().hasComputedStyleErrors(elm));
assertFalse(xhtmlDoc.getErrorHandler().hasComputedStyleErrors());
//
XHTMLElement listpara = xhtmlDoc.getElementById("listpara");
listpara.getOverrideStyle(null).setCssText("font-size:var(--foo,19pt)");
style = listpara.getComputedStyle(null);
CSSTypedValue customProperty = (CSSTypedValue) style.getPropertyCSSValue("--foo");
assertNotNull(customProperty);
assertEquals(15f, customProperty.getFloatValue(CSSUnit.CSS_PT), 1e-6);
CSSTypedValue fontSize = (CSSTypedValue) style.getPropertyCSSValue("font-size");
assertEquals(19f, fontSize.getFloatValue(CSSUnit.CSS_PT), 0.01f);
assertFalse(xhtmlDoc.getErrorHandler().hasComputedStyleErrors(listpara));
assertFalse(xhtmlDoc.getErrorHandler().hasComputedStyleErrors());
/*
* Same as above, with custom property set in parent style
*/
xhtmlDoc.getErrorHandler().resetComputedStyleErrors();
XHTMLElement body = (XHTMLElement) elm.getParentNode();
body.getOverrideStyle(null).setCssText("--foo:9pt");
elm.getOverrideStyle(null).setCssText("margin-left:var(--foo)");
style = elm.getComputedStyle(null);
marginLeft = (CSSTypedValue) style.getPropertyCSSValue("margin-left");
assertEquals(15f, marginLeft.getFloatValue(CSSUnit.CSS_PT), 0.01f);
assertFalse(xhtmlDoc.getErrorHandler().hasComputedStyleErrors(elm));
assertFalse(xhtmlDoc.getErrorHandler().hasComputedStyleErrors());
/*
* Same as above, with custom property set in parent style, fallback
*/
xhtmlDoc.getErrorHandler().resetComputedStyleErrors();
body = (XHTMLElement) elm.getParentNode();
elm.getOverrideStyle(null).setCssText("margin-left:var(--foo,21pt)");
style = elm.getComputedStyle(null);
marginLeft = (CSSTypedValue) style.getPropertyCSSValue("margin-left");
assertEquals(21f, marginLeft.getFloatValue(CSSUnit.CSS_PT), 0.01f);
assertFalse(xhtmlDoc.getErrorHandler().hasComputedStyleErrors(elm));
assertFalse(xhtmlDoc.getErrorHandler().hasComputedStyleErrors());
/*
* custom property substitution, var() in fallback.
*/
elm.getOverrideStyle(null).setCssText("margin-left:var(--no-way,var(--foo));");
style = elm.getComputedStyle(null);
marginLeft = (CSSTypedValue) style.getPropertyCSSValue("margin-left");
assertEquals(15f, marginLeft.getFloatValue(CSSUnit.CSS_PT), 0.01f);
assertFalse(xhtmlDoc.getErrorHandler().hasComputedStyleErrors(elm));
assertFalse(xhtmlDoc.getErrorHandler().hasComputedStyleErrors());
/*
* custom property substitution, var() in fallback, fallback-of-fallback.
*/
elm.getOverrideStyle(null).setCssText("margin-left:var(--no-way,var(--foo,17pt));");
style = elm.getComputedStyle(null);
marginLeft = (CSSTypedValue) style.getPropertyCSSValue("margin-left");
assertEquals(17f, marginLeft.getFloatValue(CSSUnit.CSS_PT), 0.01f);
assertFalse(xhtmlDoc.getErrorHandler().hasComputedStyleErrors(elm));
assertFalse(xhtmlDoc.getErrorHandler().hasComputedStyleErrors());
}

@Test
public void testComputedStyleAttr() {
XHTMLElement elm = xhtmlDoc.getElementById("firstH3");
Expand Down
25 changes: 22 additions & 3 deletions src/io/sf/carte/doc/dom4j/XHTMLDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Map;
Expand All @@ -37,6 +38,7 @@
import io.sf.carte.doc.style.css.CSSCanvas;
import io.sf.carte.doc.style.css.CSSDocument;
import io.sf.carte.doc.style.css.CSSMediaException;
import io.sf.carte.doc.style.css.CSSPropertyDefinition;
import io.sf.carte.doc.style.css.DocumentCSSStyleSheet;
import io.sf.carte.doc.style.css.ErrorHandler;
import io.sf.carte.doc.style.css.LinkStyle;
Expand All @@ -63,13 +65,15 @@ public class XHTMLDocument extends DOMDocument implements CSSDocument {

private static final long serialVersionUID = 7L;

private String documentURI = null;

private URL baseURL = null;

private DocumentCSSStyleSheet mergedStyleSheet = null;

private int styleCacheSerial = Integer.MIN_VALUE;

private String documentURI = null;

private URL baseURL = null;
private Set<CSSPropertyDefinition> registeredPropertySet = null;

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

Expand Down Expand Up @@ -163,6 +167,15 @@ public Object setUserData(String key, Object data, UserDataHandler handler) {
return null;
}

@Override
public void registerProperty(CSSPropertyDefinition definition) {
if (registeredPropertySet == null) {
registeredPropertySet = new HashSet<>();
}
registeredPropertySet.add(definition);
mergedStyleSheet = null;
}

/**
* A list containing all the style sheets explicitly linked into or embedded
* in a document. For HTML documents, this includes external style sheets,
Expand Down Expand Up @@ -248,6 +261,12 @@ private void mergeStyleSheets() {
while (it.hasNext()) {
mergedStyleSheet.addStyleSheet(it.next());
}
// Add DOM property definitions
if (registeredPropertySet != null) {
for (CSSPropertyDefinition def : registeredPropertySet) {
mergedStyleSheet.registerProperty(def);
}
}
}

/**
Expand Down

0 comments on commit 7ac1602

Please sign in to comment.