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 java.util.ArrayList;
18  
19  import org.htmlunit.corejs.javascript.ScriptableObject;
20  import org.htmlunit.javascript.HtmlUnitScriptable;
21  import org.htmlunit.javascript.configuration.JsxClass;
22  import org.htmlunit.javascript.configuration.JsxConstructor;
23  import org.htmlunit.javascript.configuration.JsxGetter;
24  import org.htmlunit.javascript.host.html.HTMLElement;
25  
26  /**
27   * A JavaScript object for {@code MutationRecord}.
28   *
29   * @author Ahmed Ashour
30   * @author Ronald Brill
31   *
32   * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/MutationRecord">MDN Documentation</a>
33   */
34  @JsxClass
35  public class MutationRecord extends HtmlUnitScriptable {
36  
37      private String type_;
38      private ScriptableObject target_;
39      private String oldValue_;
40      private String attributeName_;
41  
42      private NodeList addedNodes_;
43      private NodeList removedNodes_;
44      private Node previousSibling_;
45      private Node nextSibling_;
46  
47      /**
48       * JavaScript constructor.
49       */
50      @JsxConstructor
51      public void jsConstructor() {
52          // nothing to do
53      }
54  
55      /**
56       * Sets the {@code type} property.
57       * @param type the {@code type} property
58       */
59      void setType(final String type) {
60          type_ = type;
61      }
62  
63      /**
64       * Returns the {@code type} property.
65       * @return the {@code type} property
66       */
67      @JsxGetter
68      public String getType() {
69          return type_;
70      }
71  
72      /**
73       * Sets the {@code target} property.
74       * @param target the {@code target} property
75       */
76      void setTarget(final ScriptableObject target) {
77          target_ = target;
78      }
79  
80      /**
81       * Returns the {@code target} property.
82       * @return the {@code target} property
83       */
84      @JsxGetter
85      public ScriptableObject getTarget() {
86          return target_;
87      }
88  
89      /**
90       * Sets the {@code oldValue} property.
91       * @param oldValue the {@code oldValue} property
92       */
93      void setOldValue(final String oldValue) {
94          oldValue_ = oldValue;
95      }
96  
97      /**
98       * Returns the {@code oldValue} property.
99       * @return the {@code oldValue} property
100      */
101     @JsxGetter
102     public String getOldValue() {
103         return oldValue_;
104     }
105 
106     /**
107      * Sets the {@code attributeName} property.
108      * @param attributeName the {@code attributeName} property
109      */
110     void setAttributeName(final String attributeName) {
111         attributeName_ = attributeName;
112     }
113 
114     /**
115      * Returns the {@code attributeName} property.
116      * @return the {@code attributeName} property
117      */
118     @JsxGetter
119     public String getAttributeName() {
120         return attributeName_;
121     }
122 
123     /**
124      * Sets the {@code addedNodes} property.
125      * @param addedNodes the {@code addedNodes} property
126      */
127     void setAddedNodes(final NodeList addedNodes) {
128         addedNodes_ = addedNodes;
129     }
130 
131     /**
132      * Returns the {@code addedNodes} property.
133      * @return the {@code addedNodes} property
134      */
135     @JsxGetter
136     public NodeList getAddedNodes() {
137         if (addedNodes_ == null && target_ instanceof HTMLElement element) {
138             final NodeList addedNodes = new NodeList(element.getDomNodeOrDie(), new ArrayList<>());
139             addedNodes.setParentScope(getParentScope());
140             addedNodes.setPrototype(getPrototype(addedNodes.getClass()));
141 
142             addedNodes_ = addedNodes;
143         }
144         return addedNodes_;
145     }
146 
147     /**
148      * Sets the {@code removedNodes} property.
149      * @param removedNodes the {@code removedNodes} property
150      */
151     void setRemovedNodes(final NodeList removedNodes) {
152         removedNodes_ = removedNodes;
153     }
154 
155     /**
156      * Returns the {@code removedNodes} property.
157      * @return the {@code removedNodes} property
158      */
159     @JsxGetter
160     public NodeList getRemovedNodes() {
161         if (removedNodes_ == null && target_ instanceof HTMLElement element) {
162             final NodeList removedNodes = new NodeList(element.getDomNodeOrDie(), new ArrayList<>());
163             removedNodes.setParentScope(getParentScope());
164             removedNodes.setPrototype(getPrototype(removedNodes.getClass()));
165 
166             removedNodes_ = removedNodes;
167         }
168         return removedNodes_;
169     }
170 
171     /**
172      * Sets the {@code previousSibling} property.
173      * @param previousSibling the {@code previousSibling} property
174      */
175     void setPreviousSibling(final Node previousSibling) {
176         previousSibling_ = previousSibling;
177     }
178 
179     /**
180      * Returns the {@code previousSibling} property.
181      * @return the {@code previousSibling} property
182      */
183     @JsxGetter
184     public Node getPreviousSibling() {
185         return previousSibling_;
186     }
187 
188     /**
189      * Sets the {@code nextSibling} property.
190      * @param nextSibling the {@code nextSibling} property
191      */
192     void setNextSibling(final Node nextSibling) {
193         nextSibling_ = nextSibling;
194     }
195 
196     /**
197      * Returns the {@code nextSibling} property.
198      * @return the {@code nextSibling} property
199      */
200     @JsxGetter
201     public Node getNextSibling() {
202         return nextSibling_;
203     }
204 }