View Javadoc
1   /*
2    * Copyright (c) 2002-2026 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.html.DomProcessingInstruction;
18  import org.htmlunit.javascript.JavaScriptEngine;
19  import org.htmlunit.javascript.configuration.JsxClass;
20  import org.htmlunit.javascript.configuration.JsxConstructor;
21  import org.htmlunit.javascript.configuration.JsxFunction;
22  import org.htmlunit.javascript.configuration.JsxGetter;
23  import org.htmlunit.javascript.configuration.JsxSetter;
24  
25  /**
26   * A JavaScript object for {@code ProcessingInstruction}.
27   * <p>
28   * <em>Note:</em> There is an architectural difference between the Java DOM layer and the JavaScript host layer:
29   * {@link org.htmlunit.html.DomProcessingInstruction} extends {@link org.htmlunit.html.DomNode} directly
30   * rather than a Java-equivalent character data class, whereas this JavaScript class inherits from {@link CharacterData}
31   * to correctly mirror the standard browser prototype chain.
32   * </p>
33   *
34   * @author Ahmed Ashour
35   * @author Ronald Brill
36   *
37   * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/ProcessingInstruction">MDN Documentation</a>
38   */
39  @JsxClass(domClass = DomProcessingInstruction.class)
40  public class ProcessingInstruction extends CharacterData {
41  
42      /**
43       * JavaScript constructor.
44       */
45      @Override
46      @JsxConstructor
47      public void jsConstructor() {
48          super.jsConstructor();
49      }
50  
51      /**
52       * Returns the {@code target} attribute.
53       * @return the target
54       */
55      @JsxGetter
56      public String getTarget() {
57          return getDomProcessingInstruction().getTarget();
58      }
59  
60      /**
61       * Returns the {@code data} attribute.
62       * @return the data
63       */
64      @Override
65      @JsxGetter
66      public String getData() {
67          return getDomProcessingInstruction().getData();
68      }
69  
70      /**
71       * Sets the {@code data} attribute.
72       * @param data the data
73       */
74      @Override
75      @JsxSetter
76      public void setData(final String data) {
77          getDomProcessingInstruction().setData(data);
78      }
79  
80      /**
81       * Gets the number of character in the character data.
82       * @return the number of characters
83       */
84      @Override
85      @JsxGetter
86      public int getLength() {
87          return getDomProcessingInstruction().getLength();
88      }
89  
90      /**
91       * Appends a string to character data.
92       * @param arg the string to be appended to the character data
93       */
94      @Override
95      @JsxFunction
96      public void appendData(final String arg) {
97          getDomProcessingInstruction().appendData(arg);
98      }
99  
100     /**
101      * Deletes characters from character data.
102      * @param offset the position of the first character to be deleted
103      * @param count the number of characters to be deleted
104      */
105     @Override
106     @JsxFunction
107     public void deleteData(final int offset, final int count) {
108         if (offset < 0) {
109             throw JavaScriptEngine.asJavaScriptException(
110                     getWindow(),
111                     "Provided offset: " + offset + " is less than zero.",
112                     DOMException.INDEX_SIZE_ERR);
113         }
114 
115         final DomProcessingInstruction domProcessingInstruction = getDomProcessingInstruction();
116         if (offset > domProcessingInstruction.getLength()) {
117             throw JavaScriptEngine.asJavaScriptException(
118                     getWindow(),
119                     "Provided offset: " + offset + " is greater than length.",
120                     DOMException.INDEX_SIZE_ERR);
121         }
122 
123         domProcessingInstruction.deleteData(offset, count);
124     }
125 
126     /**
127      * Inserts a string into character data.
128      * @param offset the position within the first character at which
129      *        the string is to be inserted.
130      * @param arg the string to insert
131      */
132     @Override
133     @JsxFunction
134     public void insertData(final int offset, final String arg) {
135         getDomProcessingInstruction().insertData(offset, arg);
136     }
137 
138     /**
139      * Replaces characters of character data with a string.
140      * @param offset the position within the first character at which
141      *        the string is to be replaced.
142      * @param count the number of characters to be replaced
143      * @param arg the string that replaces the count characters beginning at
144      *        the character at offset.
145      */
146     @Override
147     @JsxFunction
148     public void replaceData(final int offset, final int count, final String arg) {
149         getDomProcessingInstruction().replaceData(offset, count, arg);
150     }
151 
152     /**
153      * Extracts a substring from character data.
154      * @param offset the position of the first character to be extracted
155      * @param count the number of characters to be extracted
156      * @return a string that consists of the count characters of the character
157      *         data starting from the character at position offset
158      */
159     @Override
160     @JsxFunction
161     public String substringData(final int offset, final int count) {
162         return getDomProcessingInstruction().substringData(offset, count);
163     }
164 
165     private DomProcessingInstruction getDomProcessingInstruction() {
166         return (DomProcessingInstruction) getDomNodeOrDie();
167     }
168 
169 }