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 DomComment}.
22   *
23   * @author Karel Kolman
24   * @author Ahmed Ashour
25   * @author Philip Graf
26   * @author Ronald Brill
27   */
28  public class DomCommentTest extends SimpleWebTestCase {
29  
30      /**
31       * Test the comment not visible when viewed by user.
32       * @throws Exception if the test fails
33       */
34      @Test
35      public void asNormalizedText() throws Exception {
36          final String content = DOCTYPE_HTML + "<html><body><!-- a comment --></body></html>";
37          final HtmlPage page = loadPage(content);
38          assertEquals("", page.asNormalizedText());
39      }
40  
41       /**
42        * Test the comment correctness.
43        * @throws Exception if the test fails
44        */
45      @Test
46      public void asXml() throws Exception {
47          final String comment = "<!-- a comment -->";
48          final String content = DOCTYPE_HTML + "<html><body><span id='foo'>" + comment + "</span></body></html>";
49          final HtmlPage page = loadPage(content);
50          final HtmlElement elt = page.getHtmlElementById("foo");
51          final DomNode node = elt.getFirstChild();
52          assertEquals(comment, node.asXml());
53      }
54  
55       /**
56        * Test comment and character data sibling correctness.
57        * @throws Exception if the test fails
58        */
59      @Test
60      public void textSibling() throws Exception {
61          final String content = DOCTYPE_HTML + "<html><body id='body'><!-- c1 -->text<!-- c2 --></body></html>";
62          final HtmlPage page = loadPage(content);
63          final DomNode node = page.getHtmlElementById("body").getFirstChild();
64          assertEquals(DomText.NODE_NAME, node.getNextSibling().getNodeName());
65      }
66  
67      /**
68       * @throws Exception if an error occurs
69       */
70      @Test
71      public void setTextContent() throws Exception {
72          final String html = DOCTYPE_HTML + "<html><body><span id='s'><!--abc--></span></body></html>";
73          final HtmlPage page = loadPage(html);
74          final DomComment comment = (DomComment) page.getElementById("s").getFirstChild();
75          assertEquals("abc", comment.getTextContent());
76          comment.setTextContent("xyz");
77          assertEquals("xyz", comment.getTextContent());
78      }
79  
80      /**
81       * Tests if {@code getCanonicalXPath()} returns the correct XPath for a
82       * comment node.
83       *
84       * @throws Exception if an error occurs
85       */
86      @Test
87      public void getCanonicalXPath_withoutCommentSiblings() throws Exception {
88          final String html = DOCTYPE_HTML + "<html><body><span id='s'><!--abc--></span></body></html>";
89          final HtmlPage page = loadPage(html);
90          final DomComment comment = (DomComment) page.getElementById("s").getFirstChild();
91          assertEquals("/html/body/span/comment()", comment.getCanonicalXPath());
92          assertEquals(comment, page.getFirstByXPath(comment.getCanonicalXPath()));
93      }
94  
95      /**
96       * Tests if {@code getCanonicalXPath()} returns the correct XPath for a
97       * comment node with other comment node siblings.
98       *
99       * @throws Exception if an error occurs
100      */
101     @Test
102     public void getCanonicalXPath_withCommentSiblings() throws Exception {
103         final String html = DOCTYPE_HTML + "<html><body><span id='s'><!--abc--><br/><!--def--></span></body></html>";
104         final HtmlPage page = loadPage(html);
105 
106         final DomComment comment1 = (DomComment) page.getElementById("s").getFirstChild();
107         assertEquals("abc", comment1.getData());
108         assertEquals("/html/body/span/comment()[1]", comment1.getCanonicalXPath());
109         assertEquals(comment1, page.getFirstByXPath(comment1.getCanonicalXPath()));
110 
111         final DomComment comment2 = (DomComment) page.getElementById("s").getChildNodes().get(2);
112         assertEquals("def", comment2.getData());
113         assertEquals("/html/body/span/comment()[2]", comment2.getCanonicalXPath());
114         assertEquals(comment2, page.getFirstByXPath(comment2.getCanonicalXPath()));
115     }
116 
117 }