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