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_TABLE_COLUMN_WIDTH_NO_NEGATIVE_VALUES;
18  import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_TABLE_COLUMN_WIDTH_NULL_STRING;
19  import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_TABLE_SPAN_THROWS_EXCEPTION_IF_INVALID;
20  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.CHROME;
21  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.EDGE;
22  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF;
23  
24  import com.gargoylesoftware.htmlunit.html.HtmlTableColumn;
25  import com.gargoylesoftware.htmlunit.html.HtmlTableColumnGroup;
26  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass;
27  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor;
28  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter;
29  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter;
30  
31  import net.sourceforge.htmlunit.corejs.javascript.Context;
32  
33  /**
34   * The JavaScript object {@code HTMLTableColElement}.
35   *
36   * @author Ahmed Ashour
37   * @author Ronald Brill
38   */
39  @JsxClass(domClass = HtmlTableColumn.class)
40  @JsxClass(domClass = HtmlTableColumnGroup.class)
41  public class HTMLTableColElement extends HTMLTableComponent {
42  
43      /**
44       * Creates an instance.
45       */
46      @JsxConstructor({CHROME, FF, EDGE})
47      public HTMLTableColElement() {
48      }
49  
50      /**
51       * Returns the value of the {@code span} property.
52       * @return the value of the {@code span} property
53       */
54      @JsxGetter
55      public int getSpan() {
56          final String span = getDomNodeOrDie().getAttribute("span");
57          int i;
58          try {
59              i = Integer.parseInt(span);
60              if (i < 1) {
61                  i = 1;
62              }
63          }
64          catch (final NumberFormatException e) {
65              i = 1;
66          }
67          return i;
68      }
69  
70      /**
71       * Sets the value of the {@code span} property.
72       * @param span the value of the {@code span} property
73       */
74      @JsxSetter
75      public void setSpan(final Object span) {
76          final double d = Context.toNumber(span);
77          int i = (int) d;
78          if (i < 1) {
79              if (getBrowserVersion().hasFeature(JS_TABLE_SPAN_THROWS_EXCEPTION_IF_INVALID)) {
80                  final Exception e = new Exception("Cannot set the span property to invalid value: " + span);
81                  Context.throwAsScriptRuntimeEx(e);
82              }
83              else {
84                  i = 1;
85              }
86          }
87          getDomNodeOrDie().setAttribute("span", Integer.toString(i));
88      }
89  
90      /**
91       * Returns the value of the {@code width} property.
92       * @return the value of the {@code width} property
93       */
94      @JsxGetter(propertyName = "width")
95      public String getWidth_js() {
96          final boolean ie = getBrowserVersion().hasFeature(JS_TABLE_COLUMN_WIDTH_NO_NEGATIVE_VALUES);
97          final Boolean returnNegativeValues = ie ? Boolean.FALSE : null;
98          return getWidthOrHeight("width", returnNegativeValues);
99      }
100 
101     /**
102      * Sets the value of the {@code width} property.
103      * @param width the value of the {@code width} property
104      */
105     @JsxSetter
106     public void setWidth(final Object width) {
107         final String value;
108         if (width == null && !getBrowserVersion().hasFeature(JS_TABLE_COLUMN_WIDTH_NULL_STRING)) {
109             value = "";
110         }
111         else {
112             value = Context.toString(width);
113         }
114         setWidthOrHeight("width", value, false);
115     }
116 
117     /**
118      * {@inheritDoc}
119      */
120     @Override
121     protected boolean isEndTagForbidden() {
122         return getDomNodeOrDie() instanceof HtmlTableColumn;
123     }
124 
125     /**
126      * Overwritten to throw an exception.
127      * @param value the new value for replacing this node
128      */
129     @Override
130     public void setOuterHTML(final Object value) {
131         throw Context.reportRuntimeError("outerHTML is read-only for tag '"
132                             + getDomNodeOrDie().getNodeName() + "'");
133     }
134 }