1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.html;
16
17 import org.htmlunit.SimpleWebTestCase;
18 import org.junit.jupiter.api.Test;
19
20
21
22
23
24
25
26 public class HtmlStyleTest extends SimpleWebTestCase {
27
28
29
30
31
32 @Test
33 public void asNormalizedText() throws Exception {
34 final String html = DOCTYPE_HTML
35 + "<html>\n"
36 + "<head>\n"
37 + " <title>foo</title>\n"
38 + " <style type='text/css' id='testStyle'>\n"
39 + " img { border: 0px }\n"
40 + " </style>\n"
41 + "</head>\n"
42 + "<body>\n"
43 + "</body>\n"
44 + "</html>";
45
46 final HtmlPage page = loadPage(html);
47
48 final DomNode node = page.getHtmlElementById("testStyle");
49 assertEquals("style", node.getNodeName());
50 assertEquals("", node.asNormalizedText());
51 }
52
53
54
55
56 @Test
57 public void asNormalizedText_getTextContent_insideDiv() throws Exception {
58 final String html = DOCTYPE_HTML
59 + "<html>\n"
60 + "<head></head>\n"
61 + "<body>"
62 + "<div id='tester'>"
63 + "<style>h6.add-class {color: green;}</style>"
64 + "Text content"
65 + "</div>\n"
66 + "</body></html>";
67
68 final HtmlPage page = loadPage(html);
69 final DomNode node = page.getHtmlElementById("tester");
70
71 assertEquals("Text content", node.asNormalizedText());
72 assertEquals("h6.add-class {color: green;}Text content", node.getTextContent());
73 }
74
75
76
77
78
79 @Test
80 public void asXml() throws Exception {
81 final String html = DOCTYPE_HTML
82 + "<html><head><title>foo</title>\n"
83 + "<style type='text/css'></style>\n"
84 + "<style type='text/css'><!-- \n"
85 + "body > p { color: red }\n"
86 + "--></style>\n"
87 + "</head><body>\n"
88 + "</body></html>";
89
90 final HtmlPage page = loadPage(html);
91
92 final String xml = page.asXml();
93 assertTrue("Style node not expanded in: " + xml, xml.contains("</style>"));
94
95 final String xmlWithoutSpace = xml.replaceAll("\\s", "");
96 assertTrue(xml, xmlWithoutSpace.contains("<styletype=\"text/css\"><!--body>p{color:red}--></style>"));
97 }
98 }