View Javadoc
1   /*
2    * Copyright (c) 2002-2025 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    * https://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 org.htmlunit.javascript.host.html;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.annotation.Alerts;
19  import org.junit.jupiter.api.Test;
20  
21  /**
22   * Unit tests for {@link HTMLPreElement}.
23   *
24   * @author Ronald Brill
25   */
26  public class HTMLPreElementTest extends WebDriverTestCase {
27  
28      /**
29       * @throws Exception if an error occurs
30       */
31      @Test
32      @Alerts({"0", "number", "100", "77", "number", "123"})
33      public void width() throws Exception {
34          final String html = DOCTYPE_HTML
35              + "<html>\n"
36              + "  <head>\n"
37              + "    <script>\n"
38              + LOG_TITLE_FUNCTION
39              + "      function test() {\n"
40              + "        var testPre = document.getElementById('testPre');\n"
41              + "        log(testPre.width);\n"
42              + "        log(typeof testPre.width);\n"
43              + "        testPre.width = 100;\n"
44              + "        log(testPre.width);\n"
45  
46              + "        var testPre = document.getElementById('testPreWidth');\n"
47              + "        log(testPreWidth.width);\n"
48              + "        log(typeof testPreWidth.width);\n"
49              + "        testPreWidth.width = 123;\n"
50              + "        log(testPreWidth.width);\n"
51              + "      }\n"
52              + "    </script>\n"
53              + "  </head>\n"
54              + "  <body onload='test()'>\n"
55              + "    <pre id='testPre'>pre content</pre>\n"
56              + "    <pre id='testPreWidth' width='77'>pre content</pre>\n"
57              + "  </body>\n"
58              + "</html>";
59  
60          loadPageVerifyTitle2(html);
61      }
62  
63      /**
64       * @throws Exception if an error occurs
65       */
66      @Test
67      @Alerts({"undefined", "undefined", "undefined", "undefined", "undefined", "undefined",
68               "undefined", "left", "none", "right", "all", "2", "abc", "8"})
69      public void clear() throws Exception {
70          final String html = DOCTYPE_HTML
71              + "<html><body>\n"
72              + "<pre id='p1'>pre1</pre>\n"
73              + "<pre id='p2' clear='left'>pre2</pre>\n"
74              + "<pre id='p3' clear='all'>pre3</pre>\n"
75              + "<pre id='p4' clear='right'>pre4</pre>\n"
76              + "<pre id='p5' clear='none'>pre5</pre>\n"
77              + "<pre id='p6' clear='2'>pre6</pre>\n"
78              + "<pre id='p7' clear='foo'>pre7</pre>\n"
79              + "<script>\n"
80              + LOG_TITLE_FUNCTION
81              + "function set(p, value) {\n"
82              + "  try {\n"
83              + "    p.clear = value;\n"
84              + "  } catch(e) {\n"
85              + "    log('!');\n"
86              + "  }\n"
87              + "}\n"
88              + "var p1 = document.getElementById('p1');\n"
89              + "var p2 = document.getElementById('p2');\n"
90              + "var p3 = document.getElementById('p3');\n"
91              + "var p4 = document.getElementById('p4');\n"
92              + "var p5 = document.getElementById('p5');\n"
93              + "var p6 = document.getElementById('p6');\n"
94              + "var p7 = document.getElementById('p7');\n"
95              + "log(p1.clear);\n"
96              + "log(p2.clear);\n"
97              + "log(p3.clear);\n"
98              + "log(p4.clear);\n"
99              + "log(p5.clear);\n"
100             + "log(p6.clear);\n"
101             + "log(p7.clear);\n"
102             + "set(p1, 'left');\n"
103             + "set(p2, 'none');\n"
104             + "set(p3, 'right');\n"
105             + "set(p4, 'all');\n"
106             + "set(p5, 2);\n"
107             + "set(p6, 'abc');\n"
108             + "set(p7, '8');\n"
109             + "log(p1.clear);\n"
110             + "log(p2.clear);\n"
111             + "log(p3.clear);\n"
112             + "log(p4.clear);\n"
113             + "log(p5.clear);\n"
114             + "log(p6.clear);\n"
115             + "log(p7.clear);\n"
116             + "</script>\n"
117             + "</body></html>";
118 
119         loadPageVerifyTitle2(html);
120     }
121 }