• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hazendaz / httpunit / 656

06 Dec 2025 09:11PM UTC coverage: 80.452% (+0.02%) from 80.435%
656

push

github

hazendaz
[maven-release-plugin] prepare for next development iteration

3213 of 4105 branches covered (78.27%)

Branch coverage included in aggregate %.

8245 of 10137 relevant lines covered (81.34%)

0.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.8
/src/main/java/com/meterware/httpunit/javascript/JavaScript.java
1
/*
2
 * MIT License
3
 *
4
 * Copyright 2011-2025 Russell Gold
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
7
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
8
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
9
 * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10
 *
11
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions
12
 * of the Software.
13
 *
14
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
15
 * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
17
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
18
 * DEALINGS IN THE SOFTWARE.
19
 */
20
package com.meterware.httpunit.javascript;
21

22
import com.meterware.httpunit.ClientProperties;
23
import com.meterware.httpunit.HTMLPage;
24
import com.meterware.httpunit.HttpUnitOptions;
25
import com.meterware.httpunit.HttpUnitUtils;
26
import com.meterware.httpunit.WebForm;
27
import com.meterware.httpunit.WebImage;
28
import com.meterware.httpunit.WebLink;
29
import com.meterware.httpunit.WebResponse;
30
import com.meterware.httpunit.javascript.events.EventException;
31
import com.meterware.httpunit.javascript.events.EventTarget;
32
import com.meterware.httpunit.scripting.DocumentElement;
33
import com.meterware.httpunit.scripting.FormScriptable;
34
import com.meterware.httpunit.scripting.IdentifiedDelegate;
35
import com.meterware.httpunit.scripting.Input;
36
import com.meterware.httpunit.scripting.NamedDelegate;
37
import com.meterware.httpunit.scripting.ScriptableDelegate;
38
import com.meterware.httpunit.scripting.ScriptingEngine;
39
import com.meterware.httpunit.scripting.ScriptingHandler;
40
import com.meterware.httpunit.scripting.SelectionOption;
41
import com.meterware.httpunit.scripting.SelectionOptions;
42

43
import java.io.IOException;
44
import java.lang.reflect.InvocationTargetException;
45
import java.net.URL;
46
import java.util.HashMap;
47
import java.util.HashSet;
48
import java.util.Locale;
49
import java.util.Map;
50
import java.util.Set;
51

52
import org.mozilla.javascript.Context;
53
import org.mozilla.javascript.EvaluatorException;
54
import org.mozilla.javascript.JavaScriptException;
55
import org.mozilla.javascript.Scriptable;
56
import org.mozilla.javascript.ScriptableObject;
57
import org.mozilla.javascript.Undefined;
58
import org.xml.sax.SAXException;
59

60
/**
61
 * This class is the Rhino-compatible implementation of the JavaScript DOM objects.
62
 **/
63
public class JavaScript {
×
64

65
    /** The throw exceptions on error. */
66
    private static boolean _throwExceptionsOnError = true;
1✔
67

68
    /**
69
     * Checks if is throw exceptions on error.
70
     *
71
     * @return true, if is throw exceptions on error
72
     */
73
    public static boolean isThrowExceptionsOnError() {
74
        return _throwExceptionsOnError;
1✔
75
    }
76

77
    /**
78
     * Sets the throw exceptions on error.
79
     *
80
     * @param throwExceptionsOnError
81
     *            the new throw exceptions on error
82
     */
83
    public static void setThrowExceptionsOnError(boolean throwExceptionsOnError) {
84
        _throwExceptionsOnError = throwExceptionsOnError;
1✔
85
    }
1✔
86

87
    /**
88
     * Initiates JavaScript execution for the specified web response.
89
     *
90
     * @param response
91
     *            the response
92
     *
93
     * @throws IllegalAccessException
94
     *             the illegal access exception
95
     * @throws InstantiationException
96
     *             the instantiation exception
97
     * @throws InvocationTargetException
98
     *             the invocation target exception
99
     * @throws EvaluatorException
100
     *             the evaluator exception
101
     * @throws EvaluatorException
102
     *             the evaluator exception
103
     * @throws SAXException
104
     *             the SAX exception
105
     * @throws JavaScriptException
106
     *             the java script exception
107
     */
108
    public static void run(WebResponse response) throws IllegalAccessException, InstantiationException,
109
            InvocationTargetException, EvaluatorException, EvaluatorException, SAXException, JavaScriptException {
110
        Context context = Context.enter();
1✔
111
        // suggest bug fix for large java scripts see
112
        // bug report [ 1216567 ] Exception for large javascripts
113
        // by Grzegorz Lukasik
114
        // and
115

116
        context.setOptimizationLevel(HttpUnitOptions.getJavaScriptOptimizationLevel());
1✔
117
        Scriptable scope = context.initStandardObjects(null);
1✔
118
        initHTMLObjects(scope);
1✔
119

120
        Window w = (Window) context.newObject(scope, "Window");
1✔
121
        w.initialize(null, response.getScriptableObject());
1✔
122
    }
1✔
123

124
    /**
125
     * Runs the onload event for the specified web response.
126
     *
127
     * @param response
128
     *            the response
129
     *
130
     * @throws EvaluatorException
131
     *             the evaluator exception
132
     * @throws EvaluatorException
133
     *             the evaluator exception
134
     * @throws InstantiationException
135
     *             the instantiation exception
136
     * @throws IllegalAccessException
137
     *             the illegal access exception
138
     * @throws InvocationTargetException
139
     *             the invocation target exception
140
     * @throws JavaScriptException
141
     *             the java script exception
142
     * @throws SAXException
143
     *             the SAX exception
144
     */
145
    public static void load(WebResponse response) throws EvaluatorException, InstantiationException,
146
            IllegalAccessException, InvocationTargetException, JavaScriptException, SAXException, EvaluatorException {
147
        if (!(response.getScriptableObject().getScriptEngine() instanceof JavaScriptEngine)) {
1!
148
            run(response);
×
149
        }
150
        response.getScriptableObject().load();
1✔
151
    }
1✔
152

153
    /**
154
     * Inits the HTML objects.
155
     *
156
     * @param scope
157
     *            the scope
158
     *
159
     * @throws IllegalAccessException
160
     *             the illegal access exception
161
     * @throws InstantiationException
162
     *             the instantiation exception
163
     * @throws InvocationTargetException
164
     *             the invocation target exception
165
     * @throws EvaluatorException
166
     *             the evaluator exception
167
     */
168
    private static void initHTMLObjects(Scriptable scope)
169
            throws IllegalAccessException, InstantiationException, InvocationTargetException, EvaluatorException {
170
        ScriptableObject.defineClass(scope, Window.class);
1✔
171
        ScriptableObject.defineClass(scope, Document.class);
1✔
172
        ScriptableObject.defineClass(scope, Style.class);
1✔
173
        ScriptableObject.defineClass(scope, Location.class);
1✔
174
        ScriptableObject.defineClass(scope, Navigator.class);
1✔
175
        ScriptableObject.defineClass(scope, Screen.class);
1✔
176
        ScriptableObject.defineClass(scope, Link.class);
1✔
177
        ScriptableObject.defineClass(scope, Form.class);
1✔
178
        ScriptableObject.defineClass(scope, Control.class);
1✔
179
        ScriptableObject.defineClass(scope, Link.class);
1✔
180
        ScriptableObject.defineClass(scope, Image.class);
1✔
181
        ScriptableObject.defineClass(scope, Options.class);
1✔
182
        ScriptableObject.defineClass(scope, Option.class);
1✔
183
        ScriptableObject.defineClass(scope, ElementArray.class);
1✔
184
        ScriptableObject.defineClass(scope, HTMLElement.class);
1✔
185
    }
1✔
186

187
    /**
188
     * abstract Engine for JavaScript.
189
     */
190
    abstract static class JavaScriptEngine extends ScriptingEngineImpl implements EventTarget {
1✔
191

192
        /** The Constant serialVersionUID. */
193
        private static final long serialVersionUID = 1L;
194

195
        /** The scriptable. */
196
        protected ScriptableDelegate _scriptable;
197

198
        /** The parent. */
199
        protected JavaScriptEngine _parent;
200

201
        /** The event listeners. */
202
        protected Map _eventListeners = new HashMap<>(); // Map<String,Set<EventListener>>
1✔
203

204
        /** The event capture listeners. */
205
        protected Map _eventCaptureListeners = new HashMap<>(); // Map<String,Set<EventListener>>
1✔
206

207
        /**
208
         * initialize JavaScript for the given ScriptEngine.
209
         *
210
         * @param parent
211
         *            the parent
212
         * @param scriptable
213
         *            the scriptable
214
         *
215
         * @throws SAXException
216
         *             the SAX exception
217
         * @throws JavaScriptException
218
         *             the java script exception
219
         * @throws EvaluatorException
220
         *             the evaluator exception
221
         *
222
         * @parent - the Script Engine to use
223
         *
224
         * @scriptable - the scriptable object to do the initialization for
225
         */
226
        void initialize(JavaScriptEngine parent, ScriptableDelegate scriptable)
227
                throws SAXException, JavaScriptException, EvaluatorException {
228
            _scriptable = scriptable;
1✔
229
            _scriptable.setScriptEngine(this);
1✔
230
            _parent = parent;
1✔
231
            if (parent != null) {
1✔
232
                setParentScope(parent);
1✔
233
            }
234
        }
1✔
235

236
        /**
237
         * Gets the name.
238
         *
239
         * @return the name
240
         */
241
        String getName() {
242
            return _scriptable instanceof NamedDelegate ? ((NamedDelegate) _scriptable).getName() : "";
1!
243
        }
244

245
        /**
246
         * Gets the id.
247
         *
248
         * @return the id
249
         */
250
        String getID() {
251
            return _scriptable instanceof IdentifiedDelegate ? ((IdentifiedDelegate) _scriptable).getID() : "";
1✔
252
        }
253

254
        /**
255
         * get the event Handler script for the event e.g. onchange, onmousedown, onclick, onmouseup execute the script
256
         * if it's assigned by calling doEvent for the script
257
         *
258
         * @param eventName
259
         *            the event name
260
         *
261
         * @return true, if successful
262
         */
263
        @Override
264
        public boolean handleEvent(String eventName) {
265
            return _scriptable.handleEvent(eventName);
×
266
        }
267

268
        @Override
269
        public boolean has(String propertyName, Scriptable scriptable) {
270
            return super.has(propertyName, scriptable) || _scriptable != null && _scriptable.get(propertyName) != null;
1✔
271
        }
272

273
        @Override
274
        public Object get(String propertyName, Scriptable scriptable) {
275
            Object result = super.get(propertyName, scriptable);
1✔
276
            if (result != NOT_FOUND) {
1✔
277
                return result;
1✔
278
            }
279
            if (_scriptable == null) {
1✔
280
                return NOT_FOUND;
1✔
281
            }
282

283
            return convertIfNeeded(_scriptable.get(propertyName));
1✔
284

285
        }
286

287
        @Override
288
        public Object get(int i, Scriptable scriptable) {
289
            Object result = super.get(i, scriptable);
1✔
290
            if (result != NOT_FOUND) {
1!
291
                return result;
×
292
            }
293
            if (_scriptable == null) {
1!
294
                return NOT_FOUND;
×
295
            }
296

297
            return convertIfNeeded(_scriptable.get(i));
1✔
298
        }
299

300
        /**
301
         * Convert if needed.
302
         *
303
         * @param property
304
         *            the property
305
         *
306
         * @return the object
307
         */
308
        private Object convertIfNeeded(final Object property) {
309
            if (property == null) {
1✔
310
                return NOT_FOUND;
1✔
311
            }
312

313
            if (property instanceof ScriptableDelegate[]) {
1✔
314
                return toScriptable((ScriptableDelegate[]) property);
1✔
315
            }
316
            if (!(property instanceof ScriptableDelegate)) {
1✔
317
                return property;
1✔
318
            }
319
            return toScriptable((ScriptableDelegate) property);
1✔
320
        }
321

322
        /**
323
         * To scriptable.
324
         *
325
         * @param list
326
         *            the list
327
         *
328
         * @return the object
329
         */
330
        private Object toScriptable(ScriptableDelegate[] list) {
331
            Object[] delegates = new Object[list.length];
1✔
332
            for (int i = 0; i < delegates.length; i++) {
1✔
333
                delegates[i] = toScriptable(list[i]);
1✔
334
            }
335
            return Context.getCurrentContext().newArray(this, delegates);
1✔
336
        }
337

338
        @Override
339
        public void put(String propertyName, Scriptable scriptable, Object value) {
340
            if (_scriptable == null || _scriptable.get(propertyName) == null) {
1✔
341
                super.put(propertyName, scriptable, value);
1✔
342
            } else {
343
                _scriptable.set(propertyName, value);
1✔
344
            }
345
        }
1✔
346

347
        @Override
348
        public String toString() {
349
            return (_scriptable == null ? "prototype " : "") + getClassName();
×
350
        }
351

352
        @Override
353
        public ScriptingEngine newScriptingEngine(ScriptableDelegate child) {
354
            try {
355
                return (ScriptingEngine) toScriptable(child);
1✔
356
            } catch (Exception e) {
×
357
                HttpUnitUtils.handleException(e);
×
358
                throw new RuntimeException(e.toString());
×
359
            }
360
        }
361

362
        @Override
363
        public void clearCaches() {
364
        }
×
365

366
        /**
367
         * To string if not undefined.
368
         *
369
         * @param object
370
         *            the object
371
         *
372
         * @return the string
373
         */
374
        protected static String toStringIfNotUndefined(Object object) {
375
            return object == null || Undefined.instance.equals(object) ? null : object.toString();
1!
376
        }
377

378
        /**
379
         * Converts a scriptable delegate obtained from a subobject into the appropriate Rhino-compatible Scriptable.
380
         *
381
         * @param delegate
382
         *            the delegate
383
         *
384
         * @return the object
385
         */
386
        final Object toScriptable(ScriptableDelegate delegate) {
387
            if (delegate == null) {
1!
388
                return NOT_FOUND;
×
389
            }
390
            if (delegate.getScriptEngine() instanceof Scriptable) {
1✔
391
                return delegate.getScriptEngine();
1✔
392
            }
393
            try {
394
                JavaScriptEngine element = (JavaScriptEngine) Context.getCurrentContext().newObject(this,
1✔
395
                        getScriptableClassName(delegate));
1✔
396
                element.initialize(this, delegate);
1✔
397
                return element;
1✔
398
            } catch (RuntimeException e) {
×
399
                throw e;
×
400
            } catch (Exception e) {
×
401
                throw new RhinoException(e);
×
402
            }
403
        }
404

405
        /**
406
         * get the classname of the given ScriptableDelegate.
407
         *
408
         * @param delegate
409
         *            - the object to get the class name for
410
         *
411
         * @return - the simple local class name for the delegate e.g. Window, Document, Form, Link, Image, Options,
412
         *         Option, Control, HTMLElement
413
         */
414
        private String getScriptableClassName(ScriptableDelegate delegate) {
415
            if (delegate instanceof WebResponse.Scriptable) {
1✔
416
                return "Window";
1✔
417
            }
418
            if (delegate instanceof HTMLPage.Scriptable) {
1✔
419
                return "Document";
1✔
420
            }
421
            if (delegate instanceof FormScriptable) {
1✔
422
                return "Form";
1✔
423
            }
424
            if (delegate instanceof WebLink.Scriptable) {
1✔
425
                return "Link";
1✔
426
            }
427
            if (delegate instanceof WebImage.Scriptable) {
1✔
428
                return "Image";
1✔
429
            }
430
            if (delegate instanceof SelectionOptions) {
1✔
431
                return "Options";
1✔
432
            }
433
            if (delegate instanceof SelectionOption) {
1✔
434
                return "Option";
1✔
435
            }
436
            if (delegate instanceof Input) {
1✔
437
                return "Control";
1✔
438
            }
439
            if (delegate instanceof DocumentElement) {
1!
440
                return "HTMLElement";
1✔
441
            }
442

443
            throw new IllegalArgumentException("Unknown ScriptableDelegate class: " + delegate.getClass());
×
444
        }
445

446
        /**
447
         * To element array.
448
         *
449
         * @param scriptables
450
         *            the scriptables
451
         *
452
         * @return the element array
453
         */
454
        protected ElementArray toElementArray(ScriptingHandler[] scriptables) {
455
            JavaScriptEngine[] elements = new JavaScriptEngine[scriptables.length];
1✔
456
            for (int i = 0; i < elements.length; i++) {
1✔
457
                elements[i] = (JavaScriptEngine) toScriptable((ScriptableDelegate) scriptables[i]);
1✔
458
            }
459
            ElementArray result = ElementArray.newElementArray(this);
1✔
460
            result.initialize(elements);
1✔
461
            return result;
1✔
462
        }
463

464
        /**
465
         * {@inheritDoc}
466
         */
467
        @Override
468
        public void jsFunction_addEventListener(String type, Scriptable listener, boolean useCapture) {
469
            if (useCapture) {
×
470
                Set set = (Set) _eventCaptureListeners.get(type); // Set<Scriptable>
×
471
                if (set == null) {
×
472
                    set = new HashSet<>();
×
473
                    _eventCaptureListeners.put(type, set);
×
474
                }
475
                set.add(listener);
×
476
            } else {
×
477
                Set set = (Set) _eventListeners.get(type); // Set<Scriptable>
×
478
                if (set == null) {
×
479
                    set = new HashSet<>();
×
480
                    _eventListeners.put(type, set);
×
481
                }
482
                set.add(listener);
×
483
            }
484
            // System.out.println(getClassName()+".addEventListener("+type+")");
485
        }
×
486

487
        /**
488
         * {@inheritDoc}
489
         */
490
        @Override
491
        public boolean jsFunction_dispatchEvent(Scriptable evt) throws EventException {
492
            // TODO implement event dispatching & listener invocation
493
            // System.out.println(getClassName()+".dispatchEvent("+evt.get("type",evt)+")");
494
            return true;
×
495
        }
496

497
        /**
498
         * {@inheritDoc}
499
         */
500
        @Override
501
        public void jsFunction_removeEventListener(String type, Scriptable listener, boolean useCapture) {
502
            if (useCapture) {
×
503
                Set set = (Set) _eventCaptureListeners.get(type); // Set<EventListener>
×
504
                if (set != null) {
×
505
                    set.remove(listener);
×
506
                }
507
            } else {
×
508
                Set set = (Set) _eventListeners.get(type); // Set<EventListener>
×
509
                if (set != null) {
×
510
                    set.remove(listener);
×
511
                }
512
            }
513
            // System.out.println(getClassName()+".removeEventListener("+type+")");
514
        }
×
515
    }
516

517
    /**
518
     * Window functions.
519
     */
520
    static public class Window extends JavaScriptEngine {
1✔
521

522
        /** The Constant serialVersionUID. */
523
        private static final long serialVersionUID = 1L;
524

525
        /** The document. */
526
        private Document _document;
527

528
        /** The navigator. */
529
        private Navigator _navigator;
530

531
        /** The location. */
532
        private Location _location;
533

534
        /** The screen. */
535
        private Screen _screen;
536

537
        /** The frames. */
538
        private ElementArray _frames;
539

540
        @Override
541
        public String getClassName() {
542
            return "Window";
1✔
543
        }
544

545
        /**
546
         * Js get window.
547
         *
548
         * @return the window
549
         */
550
        public Window jsGet_window() {
551
            return this;
1✔
552
        }
553

554
        /**
555
         * Js get self.
556
         *
557
         * @return the window
558
         */
559
        public Window jsGet_self() {
560
            return this;
×
561
        }
562

563
        /**
564
         * Js get document.
565
         *
566
         * @return the document
567
         */
568
        public Document jsGet_document() {
569
            if (_document == null) {
1✔
570
                _document = (Document) toScriptable(getDelegate().getDocument());
1✔
571
            }
572
            return _document;
1✔
573
        }
574

575
        /**
576
         * Js get frames.
577
         *
578
         * @return the scriptable
579
         *
580
         * @throws SAXException
581
         *             the SAX exception
582
         * @throws JavaScriptException
583
         *             the java script exception
584
         * @throws EvaluatorException
585
         *             the evaluator exception
586
         */
587
        public Scriptable jsGet_frames() throws SAXException, JavaScriptException, EvaluatorException {
588
            if (_frames == null) {
1✔
589
                WebResponse.Scriptable[] scriptables = getDelegate().getFrames();
1✔
590
                Window[] frames = new Window[scriptables.length];
1✔
591
                for (int i = 0; i < frames.length; i++) {
1✔
592
                    frames[i] = (Window) toScriptable(scriptables[i]);
1✔
593
                }
594
                _frames = (ElementArray) Context.getCurrentContext().newObject(this, "ElementArray");
1✔
595
                _frames.initialize(frames);
1✔
596
            }
597
            return _frames;
1✔
598
        }
599

600
        /**
601
         * Js get navigator.
602
         *
603
         * @return the navigator
604
         */
605
        public Navigator jsGet_navigator() {
606
            return _navigator;
1✔
607
        }
608

609
        /**
610
         * Js get screen.
611
         *
612
         * @return the screen
613
         */
614
        public Screen jsGet_screen() {
615
            return _screen;
1✔
616
        }
617

618
        /**
619
         * Js get location.
620
         *
621
         * @return the location
622
         */
623
        public Location jsGet_location() {
624
            return _location;
1✔
625
        }
626

627
        /**
628
         * Js set location.
629
         *
630
         * @param relativeURL
631
         *            the relative URL
632
         *
633
         * @throws IOException
634
         *             Signals that an I/O exception has occurred.
635
         * @throws SAXException
636
         *             the SAX exception
637
         */
638
        public void jsSet_location(String relativeURL) throws IOException, SAXException {
639
            setLocation(relativeURL);
1✔
640
        }
1✔
641

642
        /**
643
         * Sets the location.
644
         *
645
         * @param relativeURL
646
         *            the new location
647
         *
648
         * @throws IOException
649
         *             Signals that an I/O exception has occurred.
650
         * @throws SAXException
651
         *             the SAX exception
652
         */
653
        void setLocation(String relativeURL) throws IOException, SAXException {
654
            getDelegate().setLocation(relativeURL);
1✔
655
        }
1✔
656

657
        /**
658
         * initialize JavaScript for the given ScriptEngine
659
         *
660
         * @parent - the Script Engine to use
661
         *
662
         * @scriptable - the scriptable object to do the initialization for
663
         */
664
        @Override
665
        void initialize(JavaScriptEngine parent, ScriptableDelegate scriptable)
666
                throws JavaScriptException, EvaluatorException, SAXException {
667
            super.initialize(parent, scriptable);
1✔
668

669
            _location = (Location) Context.getCurrentContext().newObject(this, "Location");
1✔
670
            _location.initialize(this, ((WebResponse.Scriptable) scriptable).getURL());
1✔
671

672
            _navigator = (Navigator) Context.getCurrentContext().newObject(this, "Navigator");
1✔
673
            _navigator.setClientProperties(getDelegate().getClientProperties());
1✔
674

675
            _screen = (Screen) Context.getCurrentContext().newObject(this, "Screen");
1✔
676
            _screen.setClientProperties(getDelegate().getClientProperties());
1✔
677
        }
1✔
678

679
        /**
680
         * javascript alert handling.
681
         *
682
         * @param message
683
         *            - the alert message
684
         */
685
        public void jsFunction_alert(String message) {
686
            getDelegate().alertUser(message);
1✔
687
        }
1✔
688

689
        /**
690
         * javascript built in function "toLowerCase".
691
         *
692
         * @param s
693
         *            the s
694
         *
695
         * @return the string
696
         */
697
        public String jsFunction_toLowerCase(String s) {
698
            return s.toLowerCase(Locale.ENGLISH);
1✔
699
        }
700

701
        /**
702
         * Js function confirm.
703
         *
704
         * @param message
705
         *            the message
706
         *
707
         * @return true, if successful
708
         */
709
        public boolean jsFunction_confirm(String message) {
710
            return getDelegate().getConfirmationResponse(message);
1✔
711
        }
712

713
        /**
714
         * Js function prompt.
715
         *
716
         * @param message
717
         *            the message
718
         * @param defaultResponse
719
         *            the default response
720
         *
721
         * @return the string
722
         */
723
        public String jsFunction_prompt(String message, String defaultResponse) {
724
            return getDelegate().getUserResponse(message, defaultResponse);
1✔
725
        }
726

727
        /**
728
         * Js function move to.
729
         *
730
         * @param x
731
         *            the x
732
         * @param y
733
         *            the y
734
         */
735
        public void jsFunction_moveTo(int x, int y) {
736
        }
×
737

738
        /**
739
         * Js function scroll to.
740
         *
741
         * @param x
742
         *            the x
743
         * @param y
744
         *            the y
745
         */
746
        public void jsFunction_scrollTo(int x, int y) {
747
        }
×
748

749
        /**
750
         * Js function focus.
751
         */
752
        public void jsFunction_focus() {
753
        }
×
754

755
        /**
756
         * Js function set timeout.
757
         */
758
        public void jsFunction_setTimeout() {
759
        }
×
760

761
        /**
762
         * Js function close.
763
         */
764
        public void jsFunction_close() {
765
            getDelegate().closeWindow();
1✔
766
        }
1✔
767

768
        /**
769
         * Js function open.
770
         *
771
         * @param url
772
         *            the url
773
         * @param name
774
         *            the name
775
         * @param features
776
         *            the features
777
         * @param replace
778
         *            the replace
779
         *
780
         * @return the window
781
         *
782
         * @throws JavaScriptException
783
         *             the java script exception
784
         * @throws EvaluatorException
785
         *             the evaluator exception
786
         * @throws IOException
787
         *             Signals that an I/O exception has occurred.
788
         * @throws SAXException
789
         *             the SAX exception
790
         */
791
        public Window jsFunction_open(Object url, String name, String features, boolean replace)
792
                throws JavaScriptException, EvaluatorException, IOException, SAXException {
793
            WebResponse.Scriptable delegate = getDelegate().open(toStringIfNotUndefined(url), name, features, replace);
1✔
794
            return delegate == null ? null : (Window) toScriptable(delegate);
1✔
795
        }
796

797
        /**
798
         * The global "event" object is not supported, so return null (instead of causing '"event" is not defined').
799
         *
800
         * @return the location
801
         */
802
        public Location jsGet_event() {
803
            return null;
×
804
        }
805

806
        @Override
807
        public void clearCaches() {
808
            if (_document != null) {
1✔
809
                _document.clearCaches();
1✔
810
            }
811
        }
1✔
812

813
        @Override
814
        protected String getDocumentWriteBuffer() {
815
            return jsGet_document().getWriteBuffer().toString();
1✔
816
        }
817

818
        @Override
819
        protected void discardDocumentWriteBuffer() {
820
            jsGet_document().clearWriteBuffer();
1✔
821
        }
1✔
822

823
        /**
824
         * Gets the delegate.
825
         *
826
         * @return the delegate
827
         */
828
        private WebResponse.Scriptable getDelegate() {
829
            return (WebResponse.Scriptable) _scriptable;
1✔
830
        }
831
    }
832

833
    /**
834
     * Document script handling.
835
     */
836
    static public class Document extends JavaScriptEngine {
1✔
837

838
        /** The Constant serialVersionUID. */
839
        private static final long serialVersionUID = 1L;
840

841
        /** The forms. */
842
        private ElementArray _forms;
843

844
        /** The links. */
845
        private ElementArray _links;
846

847
        /** The images. */
848
        private ElementArray _images;
849

850
        /** The write buffer. */
851
        private StringBuilder _writeBuffer;
852

853
        /** The mime type. */
854
        private String _mimeType;
855

856
        @Override
857
        public String getClassName() {
858
            return "Document";
1✔
859
        }
860

861
        @Override
862
        public void clearCaches() {
863
            _forms = _links = _images = null;
1✔
864
        }
1✔
865

866
        /**
867
         * Js get title.
868
         *
869
         * @return the string
870
         *
871
         * @throws SAXException
872
         *             the SAX exception
873
         */
874
        public String jsGet_title() throws SAXException {
875
            return getDelegate().getTitle();
1✔
876
        }
877

878
        /**
879
         * Js get images.
880
         *
881
         * @return the scriptable
882
         *
883
         * @throws SAXException
884
         *             the SAX exception
885
         */
886
        public Scriptable jsGet_images() throws SAXException {
887
            if (_images == null) {
1✔
888
                _images = toElementArray(getDelegate().getImages());
1✔
889
            }
890
            return _images;
1✔
891
        }
892

893
        /**
894
         * Js get links.
895
         *
896
         * @return the scriptable
897
         *
898
         * @throws SAXException
899
         *             the SAX exception
900
         */
901
        public Scriptable jsGet_links() throws SAXException {
902
            if (_links == null) {
1✔
903
                _links = toElementArray(getDelegate().getLinks());
1✔
904
            }
905
            return _links;
1✔
906
        }
907

908
        /**
909
         * Js get forms.
910
         *
911
         * @return the scriptable
912
         *
913
         * @throws SAXException
914
         *             the SAX exception
915
         */
916
        public Scriptable jsGet_forms() throws SAXException {
917
            if (_forms == null) {
1✔
918
                _forms = toElementArray(getDelegate().getForms());
1✔
919
            }
920
            return _forms;
1✔
921
        }
922

923
        /**
924
         * Js function get element by id.
925
         *
926
         * @param id
927
         *            the id
928
         *
929
         * @return the object
930
         */
931
        public Object jsFunction_getElementById(String id) {
932
            ScriptableDelegate elementWithID = getDelegate().getElementWithID(id);
1✔
933
            return elementWithID == null ? null : toScriptable(elementWithID);
1✔
934
        }
935

936
        /**
937
         * Js function get elements by name.
938
         *
939
         * @param name
940
         *            the name
941
         *
942
         * @return the object
943
         */
944
        public Object jsFunction_getElementsByName(String name) {
945
            return toElementArray(getDelegate().getElementsByName(name));
1✔
946
        }
947

948
        /**
949
         * Js function get elements by tag name.
950
         *
951
         * @param name
952
         *            the name
953
         *
954
         * @return the object
955
         */
956
        public Object jsFunction_getElementsByTagName(String name) {
957
            return toElementArray(getDelegate().getElementsByTagName(name));
1✔
958
        }
959

960
        /**
961
         * Js get location.
962
         *
963
         * @return the object
964
         */
965
        public Object jsGet_location() {
966
            return _parent == null ? NOT_FOUND : getWindow().jsGet_location();
1!
967
        }
968

969
        /**
970
         * Js set location.
971
         *
972
         * @param urlString
973
         *            the url string
974
         *
975
         * @throws IOException
976
         *             Signals that an I/O exception has occurred.
977
         * @throws SAXException
978
         *             the SAX exception
979
         */
980
        public void jsSet_location(String urlString) throws IOException, SAXException {
981
            if (urlString.startsWith("color")) {
1!
982
                return;
×
983
            }
984
            getWindow().setLocation(urlString);
1✔
985
        }
1✔
986

987
        /**
988
         * Js get cookie.
989
         *
990
         * @return the string
991
         */
992
        public String jsGet_cookie() {
993
            return getDelegate().getCookie();
1✔
994
        }
995

996
        /**
997
         * Js set cookie.
998
         *
999
         * @param cookieSpec
1000
         *            the cookie spec
1001
         */
1002
        public void jsSet_cookie(String cookieSpec) {
1003
            final int equalsIndex = cookieSpec.indexOf('=');
1✔
1004
            if (equalsIndex < 0) {
1✔
1005
                return;
1✔
1006
            }
1007
            int endIndex = cookieSpec.indexOf(";", equalsIndex);
1✔
1008
            if (endIndex < 0) {
1!
1009
                endIndex = cookieSpec.length();
×
1010
            }
1011
            String name = cookieSpec.substring(0, equalsIndex);
1✔
1012
            String value = cookieSpec.substring(equalsIndex + 1, endIndex);
1✔
1013
            getDelegate().setCookie(name, value);
1✔
1014
        }
1✔
1015

1016
        /**
1017
         * Gets the window.
1018
         *
1019
         * @return the window
1020
         */
1021
        private Window getWindow() {
1022
            return (Window) _parent;
1✔
1023
        }
1024

1025
        /**
1026
         * Js function open.
1027
         *
1028
         * @param mimeType
1029
         *            the mime type
1030
         */
1031
        public void jsFunction_open(Object mimeType) {
1032
            _mimeType = toStringIfNotUndefined(mimeType);
1✔
1033
        }
1✔
1034

1035
        /**
1036
         * Js function close.
1037
         */
1038
        public void jsFunction_close() {
1039
            if (getDelegate().replaceText(getWriteBuffer().toString(), _mimeType == null ? "text/html" : _mimeType)) {
1✔
1040
                getWriteBuffer().setLength(0);
1✔
1041
            }
1042
        }
1✔
1043

1044
        /**
1045
         * Js function write.
1046
         *
1047
         * @param string
1048
         *            the string
1049
         */
1050
        public void jsFunction_write(String string) {
1051
            getWriteBuffer().append(string);
1✔
1052
        }
1✔
1053

1054
        /**
1055
         * Js function writeln.
1056
         *
1057
         * @param string
1058
         *            the string
1059
         */
1060
        public void jsFunction_writeln(String string) {
1061
            getWriteBuffer().append(string).append((char) 0x0D).append((char) 0x0A);
1✔
1062
        }
1✔
1063

1064
        /**
1065
         * Gets the write buffer.
1066
         *
1067
         * @return the write buffer
1068
         */
1069
        protected StringBuilder getWriteBuffer() {
1070
            if (_writeBuffer == null) {
1✔
1071
                _writeBuffer = new StringBuilder();
1✔
1072
            }
1073
            return _writeBuffer;
1✔
1074
        }
1075

1076
        /**
1077
         * Clear write buffer.
1078
         */
1079
        protected void clearWriteBuffer() {
1080
            _writeBuffer = null;
1✔
1081
        }
1✔
1082

1083
        /**
1084
         * Gets the delegate.
1085
         *
1086
         * @return the delegate
1087
         */
1088
        private HTMLPage.Scriptable getDelegate() {
1089
            return (HTMLPage.Scriptable) _scriptable;
1✔
1090
        }
1091

1092
        @Override
1093
        public void jsFunction_addEventListener(String type, Scriptable listener, boolean useCapture) {
1094
            // TODO Auto-generated method stub
1095

1096
        }
×
1097

1098
        @Override
1099
        public boolean jsFunction_dispatchEvent(Scriptable evt) throws EventException {
1100
            // TODO Auto-generated method stub
1101
            return false;
×
1102
        }
1103

1104
        @Override
1105
        public void jsFunction_removeEventListener(String type, Scriptable listener, boolean useCapture) {
1106
            // TODO Auto-generated method stub
1107

1108
        }
×
1109

1110
    }
1111

1112
    /**
1113
     * The Class Location.
1114
     */
1115
    static public class Location extends JavaScriptEngine {
1✔
1116

1117
        /** The Constant serialVersionUID. */
1118
        private static final long serialVersionUID = 1L;
1119

1120
        /** The url. */
1121
        private URL _url;
1122

1123
        /** The window. */
1124
        private Window _window;
1125

1126
        @Override
1127
        public String getClassName() {
1128
            return "Location";
1✔
1129
        }
1130

1131
        /**
1132
         * Initialize.
1133
         *
1134
         * @param window
1135
         *            the window
1136
         * @param url
1137
         *            the url
1138
         */
1139
        void initialize(Window window, URL url) {
1140
            _window = window;
1✔
1141
            _url = url;
1✔
1142
        }
1✔
1143

1144
        /**
1145
         * Js function replace.
1146
         *
1147
         * @param urlString
1148
         *            the url string
1149
         *
1150
         * @throws IOException
1151
         *             Signals that an I/O exception has occurred.
1152
         * @throws SAXException
1153
         *             the SAX exception
1154
         */
1155
        public void jsFunction_replace(String urlString) throws IOException, SAXException {
1156
            _window.setLocation(urlString);
1✔
1157
        }
1✔
1158

1159
        /**
1160
         * Js get href.
1161
         *
1162
         * @return the string
1163
         */
1164
        public String jsGet_href() {
1165
            return toString();
1✔
1166
        }
1167

1168
        /**
1169
         * Js set href.
1170
         *
1171
         * @param urlString
1172
         *            the url string
1173
         *
1174
         * @throws SAXException
1175
         *             the SAX exception
1176
         * @throws IOException
1177
         *             Signals that an I/O exception has occurred.
1178
         */
1179
        public void jsSet_href(String urlString) throws SAXException, IOException {
1180
            _window.setLocation(urlString);
1✔
1181
        }
1✔
1182

1183
        /**
1184
         * Js get protocol.
1185
         *
1186
         * @return the string
1187
         */
1188
        public String jsGet_protocol() {
1189
            return _url.getProtocol() + ':';
1✔
1190
        }
1191

1192
        /**
1193
         * Js get host.
1194
         *
1195
         * @return the string
1196
         */
1197
        public String jsGet_host() {
1198
            return _url.getHost() + ':' + _url.getPort();
1✔
1199
        }
1200

1201
        /**
1202
         * Js get hostname.
1203
         *
1204
         * @return the string
1205
         */
1206
        public String jsGet_hostname() {
1207
            return _url.getHost();
1✔
1208
        }
1209

1210
        /**
1211
         * Js get port.
1212
         *
1213
         * @return the string
1214
         */
1215
        public String jsGet_port() {
1216
            return String.valueOf(_url.getPort());
1✔
1217
        }
1218

1219
        /**
1220
         * Js get pathname.
1221
         *
1222
         * @return the string
1223
         */
1224
        public String jsGet_pathname() {
1225
            return _url.getPath();
1✔
1226
        }
1227

1228
        /**
1229
         * Js set pathname.
1230
         *
1231
         * @param newPath
1232
         *            the new path
1233
         *
1234
         * @throws SAXException
1235
         *             the SAX exception
1236
         * @throws IOException
1237
         *             Signals that an I/O exception has occurred.
1238
         */
1239
        public void jsSet_pathname(String newPath) throws SAXException, IOException {
1240
            if (!newPath.startsWith("/")) {
1!
1241
                newPath = '/' + newPath;
×
1242
            }
1243
            URL newURL = new URL(_url, newPath);
1✔
1244
            _window.setLocation(newURL.toExternalForm());
1✔
1245
        }
1✔
1246

1247
        /**
1248
         * Js get search.
1249
         *
1250
         * @return the string
1251
         */
1252
        public String jsGet_search() {
1253
            return '?' + _url.getQuery();
1✔
1254
        }
1255

1256
        /**
1257
         * Js set search.
1258
         *
1259
         * @param newSearch
1260
         *            the new search
1261
         *
1262
         * @throws SAXException
1263
         *             the SAX exception
1264
         * @throws IOException
1265
         *             Signals that an I/O exception has occurred.
1266
         */
1267
        public void jsSet_search(String newSearch) throws SAXException, IOException {
1268
            if (!newSearch.startsWith("?")) {
1!
1269
                newSearch = '?' + newSearch;
×
1270
            }
1271
            _window.setLocation(jsGet_protocol() + "//" + jsGet_host() + jsGet_pathname() + newSearch);
1✔
1272
        }
1✔
1273

1274
        /**
1275
         * Returns the default value of this scriptable object. In this case, it returns simply the URL as a string.
1276
         * Note that this method is necessary, since Rhino will only call the toString method directly if there are no
1277
         * Rhino methods defined (jsGet_*, jsFunction_*, etc.)
1278
         */
1279
        @Override
1280
        public Object getDefaultValue(Class typeHint) {
1281
            return _url.toExternalForm();
1✔
1282
        }
1283

1284
        @Override
1285
        public String toString() {
1286
            return _url.toExternalForm();
1✔
1287
        }
1288

1289
    }
1290

1291
    /**
1292
     * The Class Style.
1293
     */
1294
    static public class Style extends JavaScriptEngine {
1✔
1295

1296
        /** The Constant serialVersionUID. */
1297
        private static final long serialVersionUID = 1L;
1298

1299
        /** The display. */
1300
        private String _display = "inline";
1✔
1301

1302
        /** The visibility. */
1303
        private String _visibility = "visible";
1✔
1304

1305
        @Override
1306
        public String getClassName() {
1307
            return "Style";
1✔
1308
        }
1309

1310
        /**
1311
         * Js get display.
1312
         *
1313
         * @return the string
1314
         */
1315
        public String jsGet_display() {
1316
            return _display;
1✔
1317
        }
1318

1319
        /**
1320
         * Js set display.
1321
         *
1322
         * @param display
1323
         *            the display
1324
         */
1325
        public void jsSet_display(String display) {
1326
            _display = display;
1✔
1327
        }
1✔
1328

1329
        /**
1330
         * Js get visibility.
1331
         *
1332
         * @return the string
1333
         */
1334
        public String jsGet_visibility() {
1335
            return _visibility;
1✔
1336
        }
1337

1338
        /**
1339
         * Js set visibility.
1340
         *
1341
         * @param visibility
1342
         *            the visibility
1343
         */
1344
        public void jsSet_visibility(String visibility) {
1345
            _visibility = visibility;
1✔
1346
        }
1✔
1347
    }
1348

1349
    /**
1350
     * The Class Navigator.
1351
     */
1352
    static public class Navigator extends JavaScriptEngine {
1✔
1353

1354
        /** The Constant serialVersionUID. */
1355
        private static final long serialVersionUID = 1L;
1356

1357
        /** The client properties. */
1358
        private ClientProperties _clientProperties;
1359

1360
        @Override
1361
        public String getClassName() {
1362
            return "Navigator";
1✔
1363
        }
1364

1365
        /**
1366
         * Sets the client properties.
1367
         *
1368
         * @param clientProperties
1369
         *            the new client properties
1370
         */
1371
        void setClientProperties(ClientProperties clientProperties) {
1372
            _clientProperties = clientProperties;
1✔
1373
        }
1✔
1374

1375
        /**
1376
         * Js get app name.
1377
         *
1378
         * @return the string
1379
         */
1380
        public String jsGet_appName() {
1381
            return _clientProperties.getApplicationName();
1✔
1382
        }
1383

1384
        /**
1385
         * Js get app code name.
1386
         *
1387
         * @return the string
1388
         */
1389
        public String jsGet_appCodeName() {
1390
            return _clientProperties.getApplicationCodeName();
1✔
1391
        }
1392

1393
        /**
1394
         * Js get app version.
1395
         *
1396
         * @return the string
1397
         */
1398
        public String jsGet_appVersion() {
1399
            return _clientProperties.getApplicationVersion();
1✔
1400
        }
1401

1402
        /**
1403
         * Js get user agent.
1404
         *
1405
         * @return the string
1406
         */
1407
        public String jsGet_userAgent() {
1408
            return _clientProperties.getUserAgent();
1✔
1409
        }
1410

1411
        /**
1412
         * Js get platform.
1413
         *
1414
         * @return the string
1415
         */
1416
        public String jsGet_platform() {
1417
            return _clientProperties.getPlatform();
1✔
1418
        }
1419

1420
        /**
1421
         * Js get plugins.
1422
         *
1423
         * @return the object[]
1424
         */
1425
        public Object[] jsGet_plugins() {
1426
            return new Object[0];
1✔
1427
        }
1428

1429
        /**
1430
         * Js function java enabled.
1431
         *
1432
         * @return true, if successful
1433
         */
1434
        public boolean jsFunction_javaEnabled() {
1435
            return false; // no support is provided for applets at present
1✔
1436
        }
1437

1438
    }
1439

1440
    /**
1441
     * The Class Screen.
1442
     */
1443
    static public class Screen extends JavaScriptEngine {
1✔
1444

1445
        /** The Constant serialVersionUID. */
1446
        private static final long serialVersionUID = 1L;
1447

1448
        /** The client properties. */
1449
        private ClientProperties _clientProperties;
1450

1451
        /**
1452
         * Sets the client properties.
1453
         *
1454
         * @param clientProperties
1455
         *            the new client properties
1456
         */
1457
        void setClientProperties(ClientProperties clientProperties) {
1458
            _clientProperties = clientProperties;
1✔
1459
        }
1✔
1460

1461
        @Override
1462
        public String getClassName() {
1463
            return "Screen";
1✔
1464
        }
1465

1466
        /**
1467
         * Js get avail width.
1468
         *
1469
         * @return the int
1470
         */
1471
        public int jsGet_availWidth() {
1472
            return _clientProperties.getAvailableScreenWidth();
1✔
1473
        }
1474

1475
        /**
1476
         * Js get avail height.
1477
         *
1478
         * @return the int
1479
         */
1480
        public int jsGet_availHeight() {
1481
            return _clientProperties.getAvailHeight();
1✔
1482
        }
1483

1484
    }
1485

1486
    /**
1487
     * The Class ElementArray.
1488
     */
1489
    static public class ElementArray extends ScriptableObject {
1490

1491
        /** The Constant serialVersionUID. */
1492
        private static final long serialVersionUID = 1L;
1493

1494
        /** The contents. */
1495
        private JavaScriptEngine[] _contents = new HTMLElement[0];
1✔
1496

1497
        /**
1498
         * New element array.
1499
         *
1500
         * @param parent
1501
         *            the parent
1502
         *
1503
         * @return the element array
1504
         */
1505
        static ElementArray newElementArray(Scriptable parent) {
1506
            try {
1507
                return (ElementArray) Context.getCurrentContext().newObject(parent, "ElementArray");
1✔
1508
            } catch (EvaluatorException | JavaScriptException e) {
×
1509
                throw new RhinoException(e);
×
1510
            }
1511
        }
1512

1513
        /**
1514
         * Instantiates a new element array.
1515
         */
1516
        public ElementArray() {
1✔
1517
        }
1✔
1518

1519
        /**
1520
         * Initialize.
1521
         *
1522
         * @param contents
1523
         *            the contents
1524
         */
1525
        void initialize(JavaScriptEngine[] contents) {
1526
            _contents = contents;
1✔
1527
        }
1✔
1528

1529
        /**
1530
         * Js get length.
1531
         *
1532
         * @return the int
1533
         */
1534
        public int jsGet_length() {
1535
            return _contents.length;
1✔
1536
        }
1537

1538
        @Override
1539
        public String getClassName() {
1540
            return "ElementArray";
1✔
1541
        }
1542

1543
        @Override
1544
        public Object get(int i, Scriptable scriptable) {
1545
            if (i >= 0 && i < _contents.length) {
1!
1546
                return _contents[i];
1✔
1547
            }
1548
            return super.get(i, scriptable);
×
1549
        }
1550

1551
        @Override
1552
        public Object get(String name, Scriptable scriptable) {
1553
            for (JavaScriptEngine content : _contents) {
1✔
1554
                if (name.equalsIgnoreCase(content.getID())) {
1✔
1555
                    return content;
1✔
1556
                }
1557
            }
1558
            for (JavaScriptEngine content : _contents) {
1✔
1559
                if (name.equalsIgnoreCase(content.getName())) {
1✔
1560
                    return content;
1✔
1561
                }
1562
            }
1563
            return super.get(name, scriptable);
1✔
1564
        }
1565

1566
        /**
1567
         * Gets the contents.
1568
         *
1569
         * @return the contents
1570
         */
1571
        protected JavaScriptEngine[] getContents() {
1572
            return _contents;
×
1573
        }
1574
    }
1575

1576
    /**
1577
     * HTML Element support for JavaScript.
1578
     */
1579
    static public class HTMLElement extends JavaScriptEngine {
1✔
1580

1581
        /** The Constant serialVersionUID. */
1582
        private static final long serialVersionUID = 1L;
1583

1584
        /** The style. */
1585
        private Style _style;
1586

1587
        /** The document. */
1588
        private Document _document;
1589

1590
        @Override
1591
        public String getClassName() {
1592
            return "HTMLElement";
1✔
1593
        }
1594

1595
        /**
1596
         * Js get document.
1597
         *
1598
         * @return the document
1599
         */
1600
        public Document jsGet_document() {
1601
            return _document;
1✔
1602
        }
1603

1604
        /**
1605
         * Js get style.
1606
         *
1607
         * @return the style
1608
         */
1609
        public Style jsGet_style() {
1610
            return _style;
1✔
1611
        }
1612

1613
        /**
1614
         * arbitrary attribute access.
1615
         *
1616
         * @param attributeName
1617
         *            the attribute name
1618
         *
1619
         * @return the object
1620
         */
1621
        public Object jsFunction_getAttribute(String attributeName) {
1622
            return _scriptable.get(attributeName);
×
1623
        }
1624

1625
        @Override
1626
        void initialize(JavaScriptEngine parent, ScriptableDelegate scriptable)
1627
                throws JavaScriptException, EvaluatorException, SAXException {
1628
            super.initialize(parent, scriptable);
1✔
1629
            _document = (Document) parent;
1✔
1630
            _style = (Style) Context.getCurrentContext().newObject(this, "Style");
1✔
1631
        }
1✔
1632

1633
    }
1634

1635
    /**
1636
     * The Class Image.
1637
     */
1638
    static public class Image extends HTMLElement {
1✔
1639

1640
        /** The Constant serialVersionUID. */
1641
        private static final long serialVersionUID = 1L;
1642

1643
        @Override
1644
        public String getClassName() {
1645
            return "Image";
1✔
1646
        }
1647
    }
1648

1649
    /**
1650
     * The Class Link.
1651
     */
1652
    static public class Link extends HTMLElement {
1✔
1653

1654
        /** The Constant serialVersionUID. */
1655
        private static final long serialVersionUID = 1L;
1656

1657
        @Override
1658
        public Document jsGet_document() {
1659
            return super.jsGet_document();
1✔
1660
        }
1661

1662
        @Override
1663
        public String getClassName() {
1664
            return "Link";
1✔
1665
        }
1666
    }
1667

1668
    /**
1669
     * Form functions.
1670
     */
1671
    static public class Form extends HTMLElement {
1✔
1672

1673
        /** The Constant serialVersionUID. */
1674
        private static final long serialVersionUID = 1L;
1675

1676
        /** The controls. */
1677
        private ElementArray _controls;
1678

1679
        @Override
1680
        public String getClassName() {
1681
            return "Form";
1✔
1682
        }
1683

1684
        /**
1685
         * Js get name.
1686
         *
1687
         * @return the string
1688
         */
1689
        public String jsGet_name() {
1690
            return getDelegate().getName();
1✔
1691
        }
1692

1693
        /**
1694
         * Js set name.
1695
         *
1696
         * @param name
1697
         *            the name
1698
         */
1699
        public void jsSet_name(String name) {
1700
            getDelegate().set("name", name);
1✔
1701
        }
1✔
1702

1703
        /**
1704
         * Js get action.
1705
         *
1706
         * @return the string
1707
         */
1708
        public String jsGet_action() {
1709
            return getDelegate().getAction();
×
1710
        }
1711

1712
        /**
1713
         * Js set action.
1714
         *
1715
         * @param action
1716
         *            the action
1717
         */
1718
        public void jsSet_action(String action) {
1719
            getDelegate().setAction(action);
×
1720
        }
×
1721

1722
        /**
1723
         * Js get elements.
1724
         *
1725
         * @return the scriptable
1726
         *
1727
         * @throws EvaluatorException
1728
         *             the evaluator exception
1729
         * @throws JavaScriptException
1730
         *             the java script exception
1731
         */
1732
        public Scriptable jsGet_elements() throws EvaluatorException, JavaScriptException {
1733
            if (_controls == null) {
1✔
1734
                initializeControls();
1✔
1735
            }
1736
            return _controls;
1✔
1737
        }
1738

1739
        /**
1740
         * Js function get elements by tag name.
1741
         *
1742
         * @param name
1743
         *            the name
1744
         *
1745
         * @return the object
1746
         *
1747
         * @throws SAXException
1748
         *             the SAX exception
1749
         */
1750
        public Object jsFunction_getElementsByTagName(String name) throws SAXException {
1751
            return toElementArray(getDelegate().getElementsByTagName(name));
1✔
1752
        }
1753

1754
        /**
1755
         * Js function submit.
1756
         *
1757
         * @throws IOException
1758
         *             Signals that an I/O exception has occurred.
1759
         * @throws SAXException
1760
         *             the SAX exception
1761
         */
1762
        public void jsFunction_submit() throws IOException, SAXException {
1763
            getDelegate().submit();
1✔
1764
        }
1✔
1765

1766
        /**
1767
         * Js function reset.
1768
         *
1769
         * @throws IOException
1770
         *             Signals that an I/O exception has occurred.
1771
         * @throws SAXException
1772
         *             the SAX exception
1773
         */
1774
        public void jsFunction_reset() throws IOException, SAXException {
1775
            getDelegate().reset();
1✔
1776
        }
1✔
1777

1778
        /**
1779
         * Initialize controls.
1780
         *
1781
         * @throws EvaluatorException
1782
         *             the evaluator exception
1783
         * @throws JavaScriptException
1784
         *             the java script exception
1785
         */
1786
        private void initializeControls() throws EvaluatorException, JavaScriptException {
1787
            ScriptableDelegate[] scriptables = getDelegate().getElementDelegates();
1✔
1788
            Control[] controls = new Control[scriptables.length];
1✔
1789
            for (int i = 0; i < controls.length; i++) {
1✔
1790
                controls[i] = (Control) toScriptable(scriptables[i]);
1✔
1791
            }
1792
            _controls = (ElementArray) Context.getCurrentContext().newObject(this, "ElementArray");
1✔
1793
            _controls.initialize(controls);
1✔
1794
        }
1✔
1795

1796
        /**
1797
         * Gets the delegate.
1798
         *
1799
         * @return the delegate
1800
         */
1801
        private WebForm.Scriptable getDelegate() {
1802
            return (WebForm.Scriptable) _scriptable;
1✔
1803
        }
1804

1805
    }
1806

1807
    /**
1808
     * Javascript support for any control.
1809
     */
1810
    static public class Control extends JavaScriptEngine {
1✔
1811

1812
        /** The Constant serialVersionUID. */
1813
        private static final long serialVersionUID = 1L;
1814

1815
        /** The form. */
1816
        private Form _form;
1817

1818
        @Override
1819
        public String getClassName() {
1820
            return "Control";
1✔
1821
        }
1822

1823
        /**
1824
         * Js get form.
1825
         *
1826
         * @return the form
1827
         */
1828
        public Form jsGet_form() {
1829
            return _form;
1✔
1830
        }
1831

1832
        /**
1833
         * Js function focus.
1834
         */
1835
        public void jsFunction_focus() {
1836
        }
×
1837

1838
        /**
1839
         * Js function select.
1840
         */
1841
        public void jsFunction_select() {
1842
        }
×
1843

1844
        /**
1845
         * click via javascript.
1846
         *
1847
         * @throws IOException
1848
         *             Signals that an I/O exception has occurred.
1849
         * @throws SAXException
1850
         *             the SAX exception
1851
         */
1852
        public void jsFunction_click() throws IOException, SAXException {
1853
            getDelegate().click();
1✔
1854
        }
1✔
1855

1856
        /**
1857
         * Gets the delegate.
1858
         *
1859
         * @return the delegate
1860
         */
1861
        private Input getDelegate() {
1862
            return (Input) _scriptable;
1✔
1863
        }
1864

1865
        /**
1866
         * Support getting value of arbitrary attribute.
1867
         *
1868
         * @param attributeName
1869
         *            the attribute name
1870
         *
1871
         * @return the object
1872
         *
1873
         * @throws JavaScriptException
1874
         *             the java script exception
1875
         */
1876
        public Object jsFunction_getAttribute(String attributeName) throws JavaScriptException {
1877
            return getDelegate().get(attributeName);
1✔
1878
        }
1879

1880
        /**
1881
         * Support getting value of arbitrary attribute.
1882
         *
1883
         * @param attributeName
1884
         *            the attribute name
1885
         * @param value
1886
         *            the value
1887
         *
1888
         * @throws JavaScriptException
1889
         *             the java script exception
1890
         */
1891
        public void jsFunction_setAttribute(String attributeName, Object value) throws JavaScriptException {
1892
            getDelegate().setAttribute(attributeName, value);
1✔
1893
        }
1✔
1894

1895
        /**
1896
         * Support getting value of arbitrary attribute.
1897
         *
1898
         * @param attributeName
1899
         *            the attribute name
1900
         *
1901
         * @throws JavaScriptException
1902
         *             the java script exception
1903
         */
1904
        public void jsFunction_removeAttribute(String attributeName) throws JavaScriptException {
1905
            getDelegate().removeAttribute(attributeName);
×
1906
        }
×
1907

1908
        /**
1909
         * Allow calling onchange() from within a JavaScript function.
1910
         *
1911
         * @throws JavaScriptException
1912
         *             the java script exception
1913
         */
1914
        public void jsFunction_onchange() throws JavaScriptException {
1915
            Input myInput = this.getDelegate();
1✔
1916
            myInput.sendOnChangeEvent();
1✔
1917
        }
1✔
1918

1919
        @Override
1920
        void initialize(JavaScriptEngine parent, ScriptableDelegate scriptable)
1921
                throws JavaScriptException, EvaluatorException, SAXException {
1922
            super.initialize(parent, scriptable);
1✔
1923
            if (parent instanceof Form) {
1✔
1924
                _form = (Form) parent;
1✔
1925
            }
1926
        }
1✔
1927

1928
    }
1929

1930
    /**
1931
     * The Class Options.
1932
     */
1933
    static public class Options extends JavaScriptEngine {
1✔
1934

1935
        /** The Constant serialVersionUID. */
1936
        private static final long serialVersionUID = 1L;
1937

1938
        @Override
1939
        public String getClassName() {
1940
            return "Options";
1✔
1941
        }
1942

1943
        /**
1944
         * Js get length.
1945
         *
1946
         * @return the int
1947
         */
1948
        public int jsGet_length() {
1949
            return getDelegate().getLength();
1✔
1950
        }
1951

1952
        /**
1953
         * Js set length.
1954
         *
1955
         * @param length
1956
         *            the length
1957
         */
1958
        public void jsSet_length(int length) {
1959
            getDelegate().setLength(length);
1✔
1960
        }
1✔
1961

1962
        @Override
1963
        public void put(int i, Scriptable scriptable, Object object) {
1964
            if (object == null) {
1✔
1965
                getDelegate().put(i, null);
1✔
1966
            } else {
1967
                if (!(object instanceof Option)) {
1!
1968
                    throw new IllegalArgumentException("May only add an Option to this array");
×
1969
                }
1970
                Option option = (Option) object;
1✔
1971
                getDelegate().put(i, option.getDelegate());
1✔
1972
            }
1973
        }
1✔
1974

1975
        /**
1976
         * Gets the delegate.
1977
         *
1978
         * @return the delegate
1979
         */
1980
        private SelectionOptions getDelegate() {
1981
            return (SelectionOptions) _scriptable;
1✔
1982
        }
1983

1984
    }
1985

1986
    /**
1987
     * The Class Option.
1988
     */
1989
    static public class Option extends JavaScriptEngine {
1✔
1990

1991
        /** The Constant serialVersionUID. */
1992
        private static final long serialVersionUID = 1L;
1993

1994
        @Override
1995
        public String getClassName() {
1996
            return "Option";
1✔
1997
        }
1998

1999
        /**
2000
         * Js constructor.
2001
         *
2002
         * @param text
2003
         *            the text
2004
         * @param value
2005
         *            the value
2006
         * @param defaultSelected
2007
         *            the default selected
2008
         * @param selected
2009
         *            the selected
2010
         */
2011
        public void jsConstructor(String text, String value, boolean defaultSelected, boolean selected) {
2012
            _scriptable = WebResponse.newDelegate("Option");
1✔
2013
            getDelegate().initialize(text, value, defaultSelected, selected);
1✔
2014
        }
1✔
2015

2016
        /**
2017
         * Js get index.
2018
         *
2019
         * @return the int
2020
         */
2021
        public int jsGet_index() {
2022
            return getDelegate().getIndex();
1✔
2023
        }
2024

2025
        /**
2026
         * Js get text.
2027
         *
2028
         * @return the string
2029
         */
2030
        public String jsGet_text() {
2031
            return getDelegate().getText();
1✔
2032
        }
2033

2034
        /**
2035
         * Js set text.
2036
         *
2037
         * @param text
2038
         *            the text
2039
         */
2040
        public void jsSet_text(String text) {
2041
            getDelegate().setText(text);
1✔
2042
        }
1✔
2043

2044
        /**
2045
         * Js get value.
2046
         *
2047
         * @return the string
2048
         */
2049
        public String jsGet_value() {
2050
            return getDelegate().getValue();
1✔
2051
        }
2052

2053
        /**
2054
         * Js set value.
2055
         *
2056
         * @param value
2057
         *            the value
2058
         */
2059
        public void jsSet_value(String value) {
2060
            getDelegate().setValue(value);
1✔
2061
        }
1✔
2062

2063
        /**
2064
         * Js get selected.
2065
         *
2066
         * @return true, if successful
2067
         */
2068
        public boolean jsGet_selected() {
2069
            return getDelegate().isSelected();
1✔
2070
        }
2071

2072
        /**
2073
         * Js set selected.
2074
         *
2075
         * @param selected
2076
         *            the selected
2077
         */
2078
        public void jsSet_selected(boolean selected) {
2079
            getDelegate().setSelected(selected);
1✔
2080
        }
1✔
2081

2082
        /**
2083
         * Js get default selected.
2084
         *
2085
         * @return true, if successful
2086
         */
2087
        public boolean jsGet_defaultSelected() {
2088
            return getDelegate().isDefaultSelected();
×
2089
        }
2090

2091
        /**
2092
         * Gets the delegate.
2093
         *
2094
         * @return the delegate
2095
         */
2096
        SelectionOption getDelegate() {
2097
            return (SelectionOption) _scriptable;
1✔
2098
        }
2099
    }
2100

2101
}
2102

2103
/**
2104
 * special exception for the Rhino Javscript engine
2105
 */
2106
class RhinoException extends RuntimeException {
2107

2108
    private static final long serialVersionUID = 1L;
2109
    private Exception _cause;
2110

2111
    public RhinoException(Exception cause) {
×
2112
        _cause = cause;
×
2113
    }
×
2114

2115
    @Override
2116
    public String getMessage() {
2117
        return "Rhino exception: " + _cause;
×
2118
    }
2119
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc