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.javascript.host.dom;
16  
17  import org.htmlunit.corejs.javascript.Context;
18  import org.htmlunit.corejs.javascript.Function;
19  import org.htmlunit.corejs.javascript.Scriptable;
20  import org.htmlunit.html.DomCharacterData;
21  import org.htmlunit.html.DomElement;
22  import org.htmlunit.javascript.JavaScriptEngine;
23  import org.htmlunit.javascript.configuration.JsxClass;
24  import org.htmlunit.javascript.configuration.JsxConstructor;
25  import org.htmlunit.javascript.configuration.JsxFunction;
26  import org.htmlunit.javascript.configuration.JsxGetter;
27  import org.htmlunit.javascript.configuration.JsxSetter;
28  import org.htmlunit.javascript.host.Element;
29  
30  /**
31   * A JavaScript object for {@code CharacterData}.
32   *
33   * @author David K. Taylor
34   * @author Chris Erskine
35   * @author Ahmed Ashour
36   * @author Ronald Brill
37   */
38  @JsxClass
39  public class CharacterData extends Node {
40  
41      /**
42       * JavaScript constructor.
43       */
44      @Override
45      @JsxConstructor
46      public void jsConstructor() {
47          super.jsConstructor();
48      }
49  
50      /**
51       * Gets the JavaScript property {@code data} for this character data.
52       * @return the String of data
53       */
54      @JsxGetter
55      public String getData() {
56          return getDomCharacterDataOrDie().getData();
57      }
58  
59      /**
60       * Sets the JavaScript property {@code data} for this character data.
61       * @param newValue the new String of data
62       */
63      @JsxSetter
64      public void setData(final String newValue) {
65          getDomCharacterDataOrDie().setData(newValue);
66      }
67  
68      /**
69       * Gets the number of character in the character data.
70       * @return the number of characters
71       */
72      @JsxGetter
73      public int getLength() {
74          return getDomCharacterDataOrDie().getLength();
75      }
76  
77      /**
78       * Append a string to character data.
79       * @param arg the string to be appended to the character data
80       */
81      @JsxFunction
82      public void appendData(final String arg) {
83          getDomCharacterDataOrDie().appendData(arg);
84      }
85  
86      /**
87       * Delete characters from character data.
88       * @param offset the position of the first character to be deleted
89       * @param count the number of characters to be deleted
90       */
91      @JsxFunction
92      public void deleteData(final int offset, final int count) {
93          if (offset < 0) {
94              throw JavaScriptEngine.asJavaScriptException(
95                      getWindow(),
96                      "Provided offset: " + offset + " is less than zero.",
97                      DOMException.INDEX_SIZE_ERR);
98          }
99  
100         final DomCharacterData domCharacterData = getDomCharacterDataOrDie();
101         if (offset > domCharacterData.getLength()) {
102             throw JavaScriptEngine.asJavaScriptException(
103                     getWindow(),
104                     "Provided offset: " + offset + " is greater than length.",
105                     DOMException.INDEX_SIZE_ERR);
106         }
107 
108         domCharacterData.deleteData(offset, count);
109     }
110 
111     /**
112      * Insert a string into character data.
113      * @param offset the position within the first character at which
114      *        the string is to be inserted.
115      * @param arg the string to insert
116      */
117     @JsxFunction
118     public void insertData(final int offset, final String arg) {
119         getDomCharacterDataOrDie().insertData(offset, arg);
120     }
121 
122     /**
123      * Replace characters of character data with a string.
124      * @param offset the position within the first character at which
125      *        the string is to be replaced.
126      * @param count the number of characters to be replaced
127      * @param arg the string that replaces the count characters beginning at
128      *        the character at offset.
129      */
130     @JsxFunction
131     public void replaceData(final int offset, final int count, final String arg) {
132         getDomCharacterDataOrDie().replaceData(offset, count, arg);
133     }
134 
135     /**
136      * Extract a substring from character data.
137      * @param offset the position of the first character to be extracted
138      * @param count the number of characters to be extracted
139      * @return a string that consists of the count characters of the character
140      *         data starting from the character at position offset
141      */
142     @JsxFunction
143     public String substringData(final int offset, final int count) {
144         return getDomCharacterDataOrDie().substringData(offset, count);
145     }
146 
147     private DomCharacterData getDomCharacterDataOrDie() {
148         return (DomCharacterData) super.getDomNodeOrDie();
149     }
150 
151     /**
152      * Returns the next element sibling.
153      * @return the next element sibling
154      */
155     @JsxGetter
156     public Element getNextElementSibling() {
157         final DomElement child = getDomNodeOrDie().getNextElementSibling();
158         if (child != null) {
159             return child.getScriptableObject();
160         }
161         return null;
162     }
163 
164     /**
165      * Returns the previous element sibling.
166      * @return the previous element sibling
167      */
168     @JsxGetter
169     public Element getPreviousElementSibling() {
170         final DomElement child = getDomNodeOrDie().getPreviousElementSibling();
171         if (child != null) {
172             return child.getScriptableObject();
173         }
174         return null;
175     }
176 
177     /**
178      * {@inheritDoc}
179      */
180     @Override
181     @JsxFunction
182     public void remove() {
183         super.remove();
184     }
185 
186     /**
187      * Inserts a set of Node or DOMString objects in the children list of this ChildNode's parent,
188      * just before this ChildNode.
189      * @param context the context
190      * @param scope the scope
191      * @param thisObj this object
192      * @param args the arguments
193      * @param function the function
194      */
195     @JsxFunction
196     public static void before(final Context context, final Scriptable scope,
197             final Scriptable thisObj, final Object[] args,  final Function function) {
198         Node.before(context, thisObj, args, function);
199     }
200 
201     /**
202      * Inserts a set of Node or DOMString objects in the children list of this ChildNode's parent,
203      * just after this ChildNode.
204      * @param context the context
205      * @param scope the scope
206      * @param thisObj this object
207      * @param args the arguments
208      * @param function the function
209      */
210     @JsxFunction
211     public static void after(final Context context, final Scriptable scope,
212             final Scriptable thisObj, final Object[] args, final Function function) {
213         Node.after(context, thisObj, args, function);
214     }
215 
216     /**
217      * Replaces the node wit a set of Node or DOMString objects.
218      * @param context the context
219      * @param scope the scope
220      * @param thisObj this object
221      * @param args the arguments
222      * @param function the function
223      */
224     @JsxFunction
225     public static void replaceWith(final Context context, final Scriptable scope,
226             final Scriptable thisObj, final Object[] args, final Function function) {
227         Node.replaceWith(context, thisObj, args, function);
228     }
229 }