View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.html;
16  
17  import org.htmlunit.SimpleWebTestCase;
18  import org.junit.jupiter.api.Test;
19  
20  /**
21   * Tests for {@link HtmlStyle}.
22   *
23   * @author Marc Guillemot
24   * @author Ahmed Ashour
25   */
26  public class HtmlStyleTest extends SimpleWebTestCase {
27  
28      /**
29       * Verifies that asNormalizedText() returns "checked" or "unchecked" according to the state of the checkbox.
30       * @throws Exception if the test fails
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       * @throws Exception if the test fails
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       * See <a href="http://sourceforge.net/support/tracker.php?aid=2802096">Bug 2802096</a>.
77       * @throws Exception if the test fails
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  }