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.html;
16  
17  import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_PRE_WIDTH_STRING;
18  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.CHROME;
19  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.EDGE;
20  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF;
21  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF52;
22  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.IE;
23  
24  import org.apache.commons.lang3.ArrayUtils;
25  
26  import com.gargoylesoftware.htmlunit.html.HtmlExample;
27  import com.gargoylesoftware.htmlunit.html.HtmlListing;
28  import com.gargoylesoftware.htmlunit.html.HtmlPreformattedText;
29  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass;
30  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor;
31  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter;
32  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter;
33  
34  import net.sourceforge.htmlunit.corejs.javascript.Context;
35  
36  /**
37   * The JavaScript object {@code HTMLPreElement}.
38   *
39   * @author Ahmed Ashour
40   * @author Ronald Brill
41   */
42  @JsxClass(domClass = HtmlExample.class, value = CHROME)
43  @JsxClass(domClass = HtmlPreformattedText.class)
44  @JsxClass(domClass = HtmlListing.class, value = {CHROME, FF52})
45  @JsxClass(domClass = HtmlExample.class, value = FF52)
46  public class HTMLPreElement extends HTMLElement {
47  
48      /** Valid values for the {@link #getClear() clear} property. */
49      private static final String[] VALID_CLEAR_VALUES = new String[] {"left", "right", "all", "none"};
50  
51      /**
52       * Creates an instance.
53       */
54      @JsxConstructor({CHROME, FF, EDGE})
55      public HTMLPreElement() {
56      }
57  
58      /**
59       * Returns the value of the {@code cite} property.
60       * @return the value of the {@code cite} property
61       */
62      @JsxGetter(IE)
63      public String getCite() {
64          final String cite = getDomNodeOrDie().getAttribute("cite");
65          return cite;
66      }
67  
68      /**
69       * Returns the value of the {@code cite} property.
70       * @param cite the value
71       */
72      @JsxSetter(IE)
73      public void setCite(final String cite) {
74          getDomNodeOrDie().setAttribute("cite", cite);
75      }
76  
77      /**
78       * Returns the {@code width} property.
79       * @return the {@code width} property
80       */
81      @JsxGetter(propertyName = "width")
82      public Object getWidth_js() {
83          if (getBrowserVersion().hasFeature(JS_PRE_WIDTH_STRING)) {
84              return getWidthOrHeight("width", Boolean.TRUE);
85          }
86          final String value = getDomNodeOrDie().getAttribute("width");
87          final Integer intValue = HTMLCanvasElement.getValue(value);
88          if (intValue != null) {
89              return intValue;
90          }
91          return 0;
92      }
93  
94      /**
95       * Sets the {@code width} property.
96       * @param width the {@code width} property
97       */
98      @JsxSetter
99      public void setWidth(final String width) {
100         if (getBrowserVersion().hasFeature(JS_PRE_WIDTH_STRING)) {
101             setWidthOrHeight("width", width, true);
102         }
103         else {
104             getDomNodeOrDie().setAttribute("width", width);
105         }
106     }
107 
108     /**
109      * Returns the value of the {@code clear} property.
110      * @return the value of the {@code clear} property
111      */
112     @JsxGetter(IE)
113     public String getClear() {
114         final String clear = getDomNodeOrDie().getAttribute("clear");
115         if (!ArrayUtils.contains(VALID_CLEAR_VALUES, clear)) {
116             return "";
117         }
118         return clear;
119     }
120 
121     /**
122      * Sets the value of the {@code clear} property.
123      * @param clear the value of the {@code clear} property
124      */
125     @JsxSetter(IE)
126     public void setClear(final String clear) {
127         if (!ArrayUtils.contains(VALID_CLEAR_VALUES, clear)) {
128             throw Context.reportRuntimeError("Invalid clear property value: '" + clear + "'.");
129         }
130         getDomNodeOrDie().setAttribute("clear", clear);
131     }
132 }