Search in sources :

Example 1 with CSSRuleList

use of org.loboevolution.html.node.css.CSSRuleList in project LoboEvolution by LoboEvolution.

the class HTMLDocumentTest method testStyleElement.

@Test
public void testStyleElement() {
    Element style = (Element) document.getElementsByTagName("style").item(0);
    CSSStyleSheetImpl sheet = (CSSStyleSheetImpl) ((HTMLStyleElement) style).getStyleSheet();
    assertNotNull(sheet);
    assertEquals(0, sheet.getMedia().getLength());
    assertTrue(sheet.getCssRules().getLength() > 0);
    assertSame(sheet.getOwnerNode(), style);
    style.setAttribute("media", "screen");
    CSSStyleSheetImpl sheet2 = (CSSStyleSheetImpl) ((HTMLStyleElement) style).getStyleSheet();
    assertNotNull(sheet2);
    assertSame(sheet2, sheet);
    assertEquals(1, sheet2.getMedia().getLength());
    assertEquals("screen", sheet2.getMedia().item(0));
    assertTrue(sheet2.getCssRules().getLength() > 0);
    style.setTextContent("body {font-size: 14pt; margin-left: 7%;} h1 {font-size: 2.4em;}");
    sheet = (CSSStyleSheetImpl) ((HTMLLinkElement) style).getSheet();
    assertSame(sheet2, sheet);
    assertEquals(2, sheet.getCssRules().getLength());
    assertSame(sheet.getOwnerNode(), style);
    assertEquals(2, sheet.insertRule("h3 {font-family: Arial}", 2));
    style.normalize();
    assertEquals("body {font-size: 14pt; margin-left: 7%; }h1 {font-size: 2.4em; }h3 {font-family: Arial; }", style.getTextContent());
    Attr type = style.getAttributeNode("type");
    type.setNodeValue("foo");
    assertNull(((HTMLLinkElement) style).getSheet());
    assertEquals("body {font-size: 14pt; margin-left: 7%; }h1 {font-size: 2.4em; }h3 {font-family: Arial; }", style.getTextContent());
    type.setNodeValue("");
    assertNotNull(((HTMLLinkElement) style).getSheet());
    assertEquals("body {font-size: 14pt; margin-left: 7%; }h1 {font-size: 2.4em; }h3 {font-family: Arial; }", style.getTextContent());
    type.setNodeValue("text/CSS");
    sheet = (CSSStyleSheetImpl) ((HTMLLinkElement) style).getSheet();
    assertNotNull(sheet);
    Attr media = style.getAttributeNode("media");
    media.setNodeValue("&%/(*");
    assertNull(((HTMLLinkElement) style).getSheet());
    media.setNodeValue("screen");
    sheet = (CSSStyleSheetImpl) ((HTMLLinkElement) style).getSheet();
    assertNotNull(sheet);
    long sz = sheet.getCssRules().getLength();
    assertEquals(3, sz);
    Text text = document.createTextNode("@namespace svg url('http://www.w3.org/2000/svg');\n");
    style.insertBefore(text, style.getFirstChild());
    long szp1 = sz + 1;
    assertEquals(szp1, sheet.getCssRules().getLength());
    sheet = (CSSStyleSheetImpl) ((HTMLLinkElement) style).getSheet();
    CSSRuleList rules = sheet.getCssRules();
    assertEquals(szp1, rules.getLength());
    Text text2 = document.createTextNode("@font-feature-values Some Font, Other Font {@swash{swishy:1;flowing:2;}@styleset{double-W:14;sharp-terminals:16 1;}}\n");
    style.replaceChild(text2, text);
    assertEquals(szp1, sheet.getCssRules().getLength());
    sheet = (CSSStyleSheetImpl) ((HTMLLinkElement) style).getSheet();
    rules = sheet.getCssRules();
    assertEquals(szp1, rules.getLength());
    try {
        style.removeChild(text);
        fail("Must throw exception");
    } catch (DOMException e) {
        assertEquals(DOMException.NOT_FOUND_ERR, e.getCode());
    }
    style.removeChild(text2);
    assertEquals(sz, sheet.getCssRules().getLength());
    sheet = (CSSStyleSheetImpl) ((HTMLLinkElement) style).getSheet();
    rules = sheet.getCssRules();
    assertEquals(sz, rules.getLength());
    style.setTextContent("$@foo{bar}");
    type.setNodeValue("text/xsl");
    style.setTextContent("<?xml version=\"1.0\"?><xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">" + "<xsl:output method=\"text\"/><xsl:template match=\"foo\">bar<xsl:value-of select=\".\"/>" + "</xsl:template></xsl:stylesheet>");
    assertNull(((HTMLLinkElement) style).getSheet());
    assertEquals("<?xml version=\"1.0\"?><xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">" + "<xsl:output method=\"text\"/><xsl:template match=\"foo\">bar<xsl:value-of select=\".\"/>" + "</xsl:template></xsl:stylesheet>", style.getTextContent());
    style.normalize();
    assertEquals("<?xml version=\"1.0\"?><xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">" + "<xsl:output method=\"text\"/><xsl:template match=\"foo\">bar<xsl:value-of select=\".\"/>" + "</xsl:template></xsl:stylesheet>", style.getTextContent());
}
Also used : DOMException(com.gargoylesoftware.css.dom.DOMException) CSSStyleSheetImpl(org.loboevolution.html.js.css.CSSStyleSheetImpl) CSSRuleList(org.loboevolution.html.node.css.CSSRuleList) Test(org.junit.Test) LoboUnitTest(org.loboevolution.driver.LoboUnitTest)

Aggregations

DOMException (com.gargoylesoftware.css.dom.DOMException)1 Test (org.junit.Test)1 LoboUnitTest (org.loboevolution.driver.LoboUnitTest)1 CSSStyleSheetImpl (org.loboevolution.html.js.css.CSSStyleSheetImpl)1 CSSRuleList (org.loboevolution.html.node.css.CSSRuleList)1