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 java.io.PrintWriter;
18  
19  import org.htmlunit.SgmlPage;
20  import org.w3c.dom.CDATASection;
21  
22  /**
23   * Representation of a CDATA node in the HTML DOM.
24   *
25   * @author Marc Guillemot
26   * @author David K. Taylor
27   */
28  public class DomCDataSection extends DomText implements CDATASection {
29  
30      /**
31       * Creates a new instance.
32       *
33       * @param page the Page that contains this element
34       * @param data the string data held by this node
35       */
36      public DomCDataSection(final SgmlPage page, final String data) {
37          super(page, data);
38      }
39  
40      /**
41       * @return the node type constant, in this case {@link org.w3c.dom.Node#CDATA_SECTION_NODE}
42       */
43      @Override
44      public short getNodeType() {
45          return CDATA_SECTION_NODE;
46      }
47  
48      /**
49       * {@inheritDoc}
50       */
51      @Override
52      public String getNodeName() {
53          return "#cdata-section";
54      }
55  
56      /**
57       * {@inheritDoc}
58       */
59      @Override
60      protected void printXml(final String indent, final PrintWriter printWriter) {
61          printWriter.print("<![CDATA[");
62          printWriter.print(getData());
63          printWriter.print("]]>");
64      }
65  
66      /**
67       * {@inheritDoc}
68       */
69      @Override
70      protected DomText createSplitTextNode(final int offset) {
71          return new DomCDataSection(getPage(), getData().substring(offset));
72      }
73  }