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.htmlunit.junit.BrowserRunner;
19  import org.junit.Test;
20  import org.junit.runner.RunWith;
21  
22  /**
23   * Tests for {@link HtmlStyle}.
24   *
25   * @author Marc Guillemot
26   * @author Ahmed Ashour
27   */
28  @RunWith(BrowserRunner.class)
29  public class HtmlStyleTest extends SimpleWebTestCase {
30  
31      /**
32       * Verifies that asNormalizedText() returns "checked" or "unchecked" according to the state of the checkbox.
33       * @throws Exception if the test fails
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       * @throws Exception if the test fails
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       * See <a href="http://sourceforge.net/support/tracker.php?aid=2802096">Bug 2802096</a>.
80       * @throws Exception if the test fails
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 }