View Javadoc
1   /*
2    * Copyright (c) 2002-2017 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    * http://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 com.gargoylesoftware.htmlunit.javascript.host.dom;
16  
17  import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_DOCTYPE_ENTITIES_NULL;
18  import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_DOCTYPE_NOTATIONS_NULL;
19  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.CHROME;
20  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.EDGE;
21  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF;
22  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF45;
23  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.IE;
24  
25  import org.apache.commons.lang3.StringUtils;
26  import org.w3c.dom.NamedNodeMap;
27  
28  import com.gargoylesoftware.htmlunit.html.DomDocumentType;
29  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass;
30  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor;
31  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction;
32  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter;
33  
34  import net.sourceforge.htmlunit.corejs.javascript.Context;
35  import net.sourceforge.htmlunit.corejs.javascript.Function;
36  import net.sourceforge.htmlunit.corejs.javascript.Scriptable;
37  import net.sourceforge.htmlunit.corejs.javascript.Undefined;
38  
39  /**
40   * A JavaScript object for {@code DocumentType}.
41   *
42   * @author Ahmed Ashour
43   * @author Frank Danek
44   * @see <a href="http://msdn.microsoft.com/en-us/library/ms762752.aspx">MSDN documentation</a>
45   * @see <a href="http://www.xulplanet.com/references/objref/DocumentType.html">XUL Planet</a>
46   */
47  @JsxClass(domClass = DomDocumentType.class)
48  public class DocumentType extends Node {
49  
50      /**
51       * Creates an instance.
52       */
53      @JsxConstructor({CHROME, FF, EDGE})
54      public DocumentType() {
55      }
56  
57      /**
58       * Returns the name.
59       * @return the name
60       */
61      @JsxGetter
62      public String getName() {
63          return ((DomDocumentType) getDomNodeOrDie()).getName();
64      }
65  
66      /**
67       * {@inheritDoc}
68       */
69      @Override
70      public String getNodeName() {
71          return getName();
72      }
73  
74      /**
75       * Returns the publicId.
76       * @return the publicId
77       */
78      @JsxGetter
79      public String getPublicId() {
80          return ((DomDocumentType) getDomNodeOrDie()).getPublicId();
81      }
82  
83      /**
84       * Returns the systemId.
85       * @return the systemId
86       */
87      @JsxGetter
88      public String getSystemId() {
89          return ((DomDocumentType) getDomNodeOrDie()).getSystemId();
90      }
91  
92      /**
93       * Returns the internal subset.
94       * @return the internal subset
95       */
96      @JsxGetter(IE)
97      public String getInternalSubset() {
98          final String subset = ((DomDocumentType) getDomNodeOrDie()).getInternalSubset();
99          if (StringUtils.isNotEmpty(subset)) {
100             return subset;
101         }
102 
103         return null;
104     }
105 
106     /**
107      * Returns entities.
108      * @return entities
109      */
110     @JsxGetter
111     public Object getEntities() {
112         final NamedNodeMap entities = ((DomDocumentType) getDomNodeOrDie()).getEntities();
113         if (null != entities) {
114             return entities;
115         }
116 
117         if (getBrowserVersion().hasFeature(JS_DOCTYPE_ENTITIES_NULL)) {
118             return null;
119         }
120         return Undefined.instance;
121     }
122 
123     /**
124      * Returns notations.
125      * @return notations
126      */
127     @JsxGetter
128     public Object getNotations() {
129         final NamedNodeMap notations = ((DomDocumentType) getDomNodeOrDie()).getNotations();
130         if (null != notations) {
131             return notations;
132         }
133 
134         if (getBrowserVersion().hasFeature(JS_DOCTYPE_NOTATIONS_NULL)) {
135             return null;
136         }
137         return Undefined.instance;
138     }
139 
140     /**
141      * {@inheritDoc}
142      */
143     @Override
144     @JsxGetter(FF45)
145     public Object getPrefix() {
146         return super.getPrefix();
147     }
148 
149     /**
150      * {@inheritDoc}
151      */
152     @Override
153     @JsxGetter(FF45)
154     public Object getLocalName() {
155         return super.getLocalName();
156     }
157 
158     /**
159      * {@inheritDoc}
160      */
161     @Override
162     @JsxGetter(FF45)
163     public Object getNamespaceURI() {
164         return super.getNamespaceURI();
165     }
166 
167     /**
168      * {@inheritDoc}
169      */
170     @Override
171     @JsxFunction({CHROME, FF, EDGE})
172     public void remove() {
173         super.remove();
174     }
175 
176     /**
177      * Inserts a set of Node or DOMString objects in the children list of this ChildNode's parent,
178      * just before this ChildNode.
179      * @param context the context
180      * @param thisObj this object
181      * @param args the arguments
182      * @param function the function
183      */
184     @JsxFunction({CHROME, FF})
185     public static void before(final Context context, final Scriptable thisObj, final Object[] args,
186             final Function function) {
187         Node.before(context, thisObj, args, function);
188     }
189 
190     /**
191      * Inserts a set of Node or DOMString objects in the children list of this ChildNode's parent,
192      * just after this ChildNode.
193      * @param context the context
194      * @param thisObj this object
195      * @param args the arguments
196      * @param function the function
197      */
198     @JsxFunction({CHROME, FF})
199     public static void after(final Context context, final Scriptable thisObj, final Object[] args,
200             final Function function) {
201         Node.after(context, thisObj, args, function);
202     }
203 
204     /**
205      * Replaces the node wit a set of Node or DOMString objects.
206      * @param context the context
207      * @param thisObj this object
208      * @param args the arguments
209      * @param function the function
210      */
211     @JsxFunction({CHROME, FF})
212     public static void replaceWith(final Context context, final Scriptable thisObj, final Object[] args,
213             final Function function) {
214         Node.replaceWith(context, thisObj, args, function);
215     }
216 }