View Javadoc
1   /*
2    * Copyright (c) 2002-2018 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.javascript.configuration.SupportedBrowser.CHROME;
18  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.EDGE;
19  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF;
20  
21  import com.gargoylesoftware.css.parser.CSSException;
22  import com.gargoylesoftware.htmlunit.html.DomDocumentFragment;
23  import com.gargoylesoftware.htmlunit.html.DomNode;
24  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass;
25  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor;
26  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction;
27  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter;
28  import com.gargoylesoftware.htmlunit.javascript.host.Element;
29  import com.gargoylesoftware.htmlunit.javascript.host.html.HTMLCollection;
30  
31  import net.sourceforge.htmlunit.corejs.javascript.Context;
32  
33  /**
34   * A JavaScript object for {@code DocumentFragment}.
35   *
36   * @author Ahmed Ashour
37   * @author Frank Danek
38   *
39   * @see <a href="http://www.w3.org/TR/2000/WD-DOM-Level-1-20000929/level-one-core.html#ID-B63ED1A3">
40   * W3C Dom Level 1</a>
41   */
42  @JsxClass(domClass = DomDocumentFragment.class)
43  public class DocumentFragment extends Node {
44  
45      /**
46       * Creates an instance.
47       */
48      @JsxConstructor({CHROME, FF, EDGE})
49      public DocumentFragment() {
50      }
51  
52      /**
53       * Retrieves all element nodes from descendants of the starting element node that match any selector
54       * within the supplied selector strings.
55       * The NodeList object returned by the querySelectorAll() method must be static, not live.
56       * @param selectors the selectors
57       * @return the static node list
58       */
59      @JsxFunction
60      public NodeList querySelectorAll(final String selectors) {
61          try {
62              return NodeList.staticNodeList(this, getDomNodeOrDie().querySelectorAll(selectors));
63          }
64          catch (final CSSException e) {
65              throw Context.reportRuntimeError("An invalid or illegal selector was specified (selector: '"
66                      + selectors + "' error: " + e.getMessage() + ").");
67          }
68      }
69  
70      /**
71       * Returns the first element within the document that matches the specified group of selectors.
72       * @param selectors the selectors
73       * @return null if no matches are found; otherwise, it returns the first matching element
74       */
75      @JsxFunction
76      public Node querySelector(final String selectors) {
77          try {
78              final DomNode node = getDomNodeOrDie().querySelector(selectors);
79              if (node != null) {
80                  return (Node) node.getScriptableObject();
81              }
82              return null;
83          }
84          catch (final CSSException e) {
85              throw Context.reportRuntimeError("An invalid or illegal selector was specified (selector: '"
86                      + selectors + "' error: " + e.getMessage() + ").");
87          }
88      }
89  
90      /**
91       * {@inheritDoc}
92       */
93      @Override
94      public Object getDefaultValue(final Class<?> hint) {
95          if (String.class.equals(hint) || hint == null) {
96              return "[object " + getClassName() + "]";
97          }
98          return super.getDefaultValue(hint);
99      }
100 
101     /**
102      * {@inheritDoc}
103      */
104     @Override
105     @JsxGetter({CHROME, FF})
106     public int getChildElementCount() {
107         return super.getChildElementCount();
108     }
109 
110     /**
111      * {@inheritDoc}
112      */
113     @Override
114     @JsxGetter({CHROME, FF})
115     public Element getFirstElementChild() {
116         return super.getFirstElementChild();
117     }
118 
119     /**
120      * {@inheritDoc}
121      */
122     @Override
123     @JsxGetter({CHROME, FF})
124     public Element getLastElementChild() {
125         return super.getLastElementChild();
126     }
127 
128     /**
129      * {@inheritDoc}
130      */
131     @Override
132     @JsxGetter({CHROME, FF, EDGE})
133     public HTMLCollection getChildren() {
134         return super.getChildren();
135     }
136 
137 }