1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.javascript;
16
17 import static java.nio.charset.StandardCharsets.UTF_8;
18
19 import org.htmlunit.WebDriverTestCase;
20 import org.htmlunit.junit.annotation.Alerts;
21 import org.junit.jupiter.api.Test;
22 import org.openqa.selenium.WebDriver;
23
24
25
26
27
28
29
30
31 public class GlobalFunctionsTest extends WebDriverTestCase {
32
33
34
35
36
37
38
39
40 @Test
41 @Alerts({"7.89", "7.89"})
42 public void parseFloat() throws Exception {
43 final String html = DOCTYPE_HTML
44 + "<html><head><script>\n"
45 + LOG_TITLE_FUNCTION
46 + "function doTest() {\n"
47 + " log(parseFloat('\\n 7.89 '));\n"
48 + " log(parseFloat('7.89em'));\n"
49 + "}\n"
50 + "</script></head><body onload='doTest()'>\n"
51 + "</body></html>";
52
53 loadPageVerifyTitle2(html);
54 }
55
56
57
58
59
60
61 @Test
62 @Alerts({"0", "1", "-2345", "1", "12", "NaN", "0", "1", "8", "9", "100", "0", "1", "8", "9", "100",
63 "100", "NaN", "NaN"})
64 public void parseInt() throws Exception {
65 final String html = DOCTYPE_HTML
66 + "<html><head><script>\n"
67 + LOG_TITLE_FUNCTION
68 + "function doTest() {\n"
69 + " log(parseInt('0'));\n"
70 + " log(parseInt(' 1 '));\n"
71 + " log(parseInt('-2345'));\n"
72 + " log(parseInt('1.23'));\n"
73 + " log(parseInt('12,3'));\n"
74 + " log(parseInt('abc'));\n"
75
76 + " log(parseInt('0'));\n"
77 + " log(parseInt(' 01 '));\n"
78 + " log(parseInt('08'));\n"
79 + " log(parseInt('09'));\n"
80 + " log(parseInt('0100'));\n"
81
82 + " log(parseInt('0', 10));\n"
83 + " log(parseInt(' 01 ', 10));\n"
84 + " log(parseInt('08', 10));\n"
85 + " log(parseInt('09', 10));\n"
86 + " log(parseInt('0100', 10));\n"
87
88 + " log(parseInt('100', 0));\n"
89 + " log(parseInt('100', -1));\n"
90 + " log(parseInt('100', -7));\n"
91 + "}\n"
92 + "</script></head><body onload='doTest()'>\n"
93 + "</body></html>";
94
95 loadPageVerifyTitle2(html);
96 }
97
98
99
100
101
102 @Test
103 @Alerts({"decodeURI: function", "decodeURIComponent: function", "encodeURI: function",
104 "encodeURIComponent: function", "escape: function", "eval: function", "isFinite: function", "isNaN: function",
105 "parseFloat: function", "parseInt: function", "unescape: function"})
106 public void methods_common() throws Exception {
107 final String[] methods = {"decodeURI", "decodeURIComponent", "encodeURI", "encodeURIComponent", "escape",
108 "eval", "isFinite", "isNaN", "parseFloat", "parseInt", "unescape"};
109 final String html = NativeDateTest.createHTMLTestMethods("this", methods);
110 loadPageVerifyTitle2(html);
111 }
112
113
114
115
116
117 @Test
118 @Alerts({"isXMLName: undefined", "uneval: undefined"})
119 public void methods_different() throws Exception {
120 final String[] methods = {"isXMLName", "uneval"};
121 final String html = NativeDateTest.createHTMLTestMethods("this", methods);
122 loadPageVerifyTitle2(html);
123 }
124
125
126
127
128
129 @Test
130 @Alerts({"http%3A%2F%2Fw3schools.com%2Fmy%20test.asp%3Fname%3Dst%C3%A5le%26car%3Dsaab",
131 "%E6%B5%8B%E8%A9%A6"})
132 public void encodeURIComponent() throws Exception {
133 final String html = DOCTYPE_HTML
134 + "<html>\n"
135 + "<head>\n"
136 + "<script>\n"
137 + LOG_TITLE_FUNCTION
138 + " function test() {\n"
139 + " var uri='http://w3schools.com/my test.asp?name=st\u00E5le&car=saab';\n"
140 + " log(encodeURIComponent(uri));\n"
141
142 + " uri='\\u6D4B\\u8A66';\n"
143 + " log(encodeURIComponent(uri));\n"
144 + " }\n"
145 + "</script>\n"
146 + "</head>\n"
147 + "<body onload='test()'>\n"
148 + "</body></html>";
149
150 loadPageVerifyTitle2(html);
151 }
152
153
154
155
156
157 @Test
158 @Alerts("%E6%B5%8B%E8%A9%A6")
159 public void encodeURIComponentUtf8() throws Exception {
160 final String html = DOCTYPE_HTML
161 + "<html>\n"
162 + "<head>\n"
163 + "<script>\n"
164 + LOG_TITLE_FUNCTION
165 + " function test() {\n"
166 + " uri='\u6D4B\u8A66';\n"
167 + " log(encodeURIComponent(uri));\n"
168 + " }\n"
169 + "</script>\n"
170 + "</head>\n"
171 + "<body onload='test()'>\n"
172 + "</body></html>";
173
174 final WebDriver driver = loadPage2(html, URL_FIRST, "text/html;charset=UTF-8", UTF_8);
175 verifyTitle2(driver, getExpectedAlerts());
176 }
177
178
179
180
181
182 @Test
183 @Alerts({"\\u00ee\\u0010\\u0043\\u0072\\u00f4\\u00ef\\u00b6\\u0062\\u0034",
184 "\\u00ee\\u0010\\u0043\\u0072\\u00f4\\u00ef\\u00b6\\u0062\\u0034"})
185 public void decodeURIComponent() throws Exception {
186 final String html = DOCTYPE_HTML
187 + "<html>\n"
188 + "<head>\n"
189 + "<script>\n"
190 + " function log(msg) {\n"
191 + " var escaped = '';\n"
192 + " for (var i = 0; i < msg.length; ++i) {\n"
193 + " var hex = msg.charCodeAt(i).toString(16).toLowerCase();\n"
194 + " escaped += '\\\\u' + '0000'.substr(hex.length) + hex;\n"
195 + " }\n"
196 + " window.document.title += escaped + '\\u00a7';\n"
197 + " }\n"
198
199 + " function test() {\n"
200 + " var uri='%c3%ae%10%43%72%c3%b4%c3%af%c2%b6%62%34';\n"
201 + " log(decodeURIComponent(uri));\n"
202 + " log(decodeURIComponent(uri, false));\n"
203 + " }\n"
204 + "</script>\n"
205 + "</head>\n"
206 + "<body onload='test()'>\n"
207 + "</body></html>";
208
209 loadPageVerifyTitle2(html);
210 }
211 }