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