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

hazendaz / httpunit / 755

14 Feb 2026 07:14PM UTC coverage: 80.526%. Remained the same
755

push

github

hazendaz
[ci] Fix badge

3213 of 4105 branches covered (78.27%)

Branch coverage included in aggregate %.

8245 of 10124 relevant lines covered (81.44%)

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
 * SPDX-License-Identifier: MIT
3
 * See LICENSE file for details.
4
 *
5
 * Copyright 2000-2026 Russell Gold
6
 * Copyright 2021-2000 hazendaz
7
 */
8
package com.meterware.httpunit.javascript;
9

10
import com.meterware.httpunit.ClientProperties;
11
import com.meterware.httpunit.HTMLPage;
12
import com.meterware.httpunit.HttpUnitOptions;
13
import com.meterware.httpunit.HttpUnitUtils;
14
import com.meterware.httpunit.WebForm;
15
import com.meterware.httpunit.WebImage;
16
import com.meterware.httpunit.WebLink;
17
import com.meterware.httpunit.WebResponse;
18
import com.meterware.httpunit.javascript.events.EventException;
19
import com.meterware.httpunit.javascript.events.EventTarget;
20
import com.meterware.httpunit.scripting.DocumentElement;
21
import com.meterware.httpunit.scripting.FormScriptable;
22
import com.meterware.httpunit.scripting.IdentifiedDelegate;
23
import com.meterware.httpunit.scripting.Input;
24
import com.meterware.httpunit.scripting.NamedDelegate;
25
import com.meterware.httpunit.scripting.ScriptableDelegate;
26
import com.meterware.httpunit.scripting.ScriptingEngine;
27
import com.meterware.httpunit.scripting.ScriptingHandler;
28
import com.meterware.httpunit.scripting.SelectionOption;
29
import com.meterware.httpunit.scripting.SelectionOptions;
30

31
import java.io.IOException;
32
import java.lang.reflect.InvocationTargetException;
33
import java.net.URL;
34
import java.util.HashMap;
35
import java.util.HashSet;
36
import java.util.Locale;
37
import java.util.Map;
38
import java.util.Set;
39

40
import org.mozilla.javascript.Context;
41
import org.mozilla.javascript.EvaluatorException;
42
import org.mozilla.javascript.JavaScriptException;
43
import org.mozilla.javascript.Scriptable;
44
import org.mozilla.javascript.ScriptableObject;
45
import org.mozilla.javascript.Undefined;
46
import org.xml.sax.SAXException;
47

48
/**
49
 * This class is the Rhino-compatible implementation of the JavaScript DOM objects.
50
 **/
51
public class JavaScript {
×
52

53
    /** The throw exceptions on error. */
54
    private static boolean _throwExceptionsOnError = true;
1✔
55

56
    /**
57
     * Checks if is throw exceptions on error.
58
     *
59
     * @return true, if is throw exceptions on error
60
     */
61
    public static boolean isThrowExceptionsOnError() {
62
        return _throwExceptionsOnError;
1✔
63
    }
64

65
    /**
66
     * Sets the throw exceptions on error.
67
     *
68
     * @param throwExceptionsOnError
69
     *            the new throw exceptions on error
70
     */
71
    public static void setThrowExceptionsOnError(boolean throwExceptionsOnError) {
72
        _throwExceptionsOnError = throwExceptionsOnError;
1✔
73
    }
1✔
74

75
    /**
76
     * Initiates JavaScript execution for the specified web response.
77
     *
78
     * @param response
79
     *            the response
80
     *
81
     * @throws IllegalAccessException
82
     *             the illegal access exception
83
     * @throws InstantiationException
84
     *             the instantiation exception
85
     * @throws InvocationTargetException
86
     *             the invocation target exception
87
     * @throws EvaluatorException
88
     *             the evaluator exception
89
     * @throws EvaluatorException
90
     *             the evaluator exception
91
     * @throws SAXException
92
     *             the SAX exception
93
     * @throws JavaScriptException
94
     *             the java script exception
95
     */
96
    public static void run(WebResponse response) throws IllegalAccessException, InstantiationException,
97
            InvocationTargetException, EvaluatorException, EvaluatorException, SAXException, JavaScriptException {
98
        Context context = Context.enter();
1✔
99
        // suggest bug fix for large java scripts see
100
        // bug report [ 1216567 ] Exception for large javascripts
101
        // by Grzegorz Lukasik
102
        // and
103

104
        context.setOptimizationLevel(HttpUnitOptions.getJavaScriptOptimizationLevel());
1✔
105
        Scriptable scope = context.initStandardObjects(null);
1✔
106
        initHTMLObjects(scope);
1✔
107

108
        Window w = (Window) context.newObject(scope, "Window");
1✔
109
        w.initialize(null, response.getScriptableObject());
1✔
110
    }
1✔
111

112
    /**
113
     * Runs the onload event for the specified web response.
114
     *
115
     * @param response
116
     *            the response
117
     *
118
     * @throws EvaluatorException
119
     *             the evaluator exception
120
     * @throws EvaluatorException
121
     *             the evaluator exception
122
     * @throws InstantiationException
123
     *             the instantiation exception
124
     * @throws IllegalAccessException
125
     *             the illegal access exception
126
     * @throws InvocationTargetException
127
     *             the invocation target exception
128
     * @throws JavaScriptException
129
     *             the java script exception
130
     * @throws SAXException
131
     *             the SAX exception
132
     */
133
    public static void load(WebResponse response) throws EvaluatorException, InstantiationException,
134
            IllegalAccessException, InvocationTargetException, JavaScriptException, SAXException, EvaluatorException {
135
        if (!(response.getScriptableObject().getScriptEngine() instanceof JavaScriptEngine)) {
1!
136
            run(response);
×
137
        }
138
        response.getScriptableObject().load();
1✔
139
    }
1✔
140

141
    /**
142
     * Inits the HTML objects.
143
     *
144
     * @param scope
145
     *            the scope
146
     *
147
     * @throws IllegalAccessException
148
     *             the illegal access exception
149
     * @throws InstantiationException
150
     *             the instantiation exception
151
     * @throws InvocationTargetException
152
     *             the invocation target exception
153
     * @throws EvaluatorException
154
     *             the evaluator exception
155
     */
156
    private static void initHTMLObjects(Scriptable scope)
157
            throws IllegalAccessException, InstantiationException, InvocationTargetException, EvaluatorException {
158
        ScriptableObject.defineClass(scope, Window.class);
1✔
159
        ScriptableObject.defineClass(scope, Document.class);
1✔
160
        ScriptableObject.defineClass(scope, Style.class);
1✔
161
        ScriptableObject.defineClass(scope, Location.class);
1✔
162
        ScriptableObject.defineClass(scope, Navigator.class);
1✔
163
        ScriptableObject.defineClass(scope, Screen.class);
1✔
164
        ScriptableObject.defineClass(scope, Link.class);
1✔
165
        ScriptableObject.defineClass(scope, Form.class);
1✔
166
        ScriptableObject.defineClass(scope, Control.class);
1✔
167
        ScriptableObject.defineClass(scope, Link.class);
1✔
168
        ScriptableObject.defineClass(scope, Image.class);
1✔
169
        ScriptableObject.defineClass(scope, Options.class);
1✔
170
        ScriptableObject.defineClass(scope, Option.class);
1✔
171
        ScriptableObject.defineClass(scope, ElementArray.class);
1✔
172
        ScriptableObject.defineClass(scope, HTMLElement.class);
1✔
173
    }
1✔
174

175
    /**
176
     * abstract Engine for JavaScript.
177
     */
178
    abstract static class JavaScriptEngine extends ScriptingEngineImpl implements EventTarget {
1✔
179

180
        /** The Constant serialVersionUID. */
181
        private static final long serialVersionUID = 1L;
182

183
        /** The scriptable. */
184
        protected ScriptableDelegate _scriptable;
185

186
        /** The parent. */
187
        protected JavaScriptEngine _parent;
188

189
        /** The event listeners. */
190
        protected Map _eventListeners = new HashMap<>(); // Map<String,Set<EventListener>>
1✔
191

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

195
        /**
196
         * initialize JavaScript for the given ScriptEngine.
197
         *
198
         * @param parent
199
         *            the parent
200
         * @param scriptable
201
         *            the scriptable
202
         *
203
         * @throws SAXException
204
         *             the SAX exception
205
         * @throws JavaScriptException
206
         *             the java script exception
207
         * @throws EvaluatorException
208
         *             the evaluator exception
209
         *
210
         * @parent - the Script Engine to use
211
         *
212
         * @scriptable - the scriptable object to do the initialization for
213
         */
214
        void initialize(JavaScriptEngine parent, ScriptableDelegate scriptable)
215
                throws SAXException, JavaScriptException, EvaluatorException {
216
            _scriptable = scriptable;
1✔
217
            _scriptable.setScriptEngine(this);
1✔
218
            _parent = parent;
1✔
219
            if (parent != null) {
1✔
220
                setParentScope(parent);
1✔
221
            }
222
        }
1✔
223

224
        /**
225
         * Gets the name.
226
         *
227
         * @return the name
228
         */
229
        String getName() {
230
            return _scriptable instanceof NamedDelegate ? ((NamedDelegate) _scriptable).getName() : "";
1!
231
        }
232

233
        /**
234
         * Gets the id.
235
         *
236
         * @return the id
237
         */
238
        String getID() {
239
            return _scriptable instanceof IdentifiedDelegate ? ((IdentifiedDelegate) _scriptable).getID() : "";
1✔
240
        }
241

242
        /**
243
         * get the event Handler script for the event e.g. onchange, onmousedown, onclick, onmouseup execute the script
244
         * if it's assigned by calling doEvent for the script
245
         *
246
         * @param eventName
247
         *            the event name
248
         *
249
         * @return true, if successful
250
         */
251
        @Override
252
        public boolean handleEvent(String eventName) {
253
            return _scriptable.handleEvent(eventName);
×
254
        }
255

256
        @Override
257
        public boolean has(String propertyName, Scriptable scriptable) {
258
            return super.has(propertyName, scriptable) || _scriptable != null && _scriptable.get(propertyName) != null;
1✔
259
        }
260

261
        @Override
262
        public Object get(String propertyName, Scriptable scriptable) {
263
            Object result = super.get(propertyName, scriptable);
1✔
264
            if (result != NOT_FOUND) {
1✔
265
                return result;
1✔
266
            }
267
            if (_scriptable == null) {
1✔
268
                return NOT_FOUND;
1✔
269
            }
270

271
            return convertIfNeeded(_scriptable.get(propertyName));
1✔
272

273
        }
274

275
        @Override
276
        public Object get(int i, Scriptable scriptable) {
277
            Object result = super.get(i, scriptable);
1✔
278
            if (result != NOT_FOUND) {
1!
279
                return result;
×
280
            }
281
            if (_scriptable == null) {
1!
282
                return NOT_FOUND;
×
283
            }
284

285
            return convertIfNeeded(_scriptable.get(i));
1✔
286
        }
287

288
        /**
289
         * Convert if needed.
290
         *
291
         * @param property
292
         *            the property
293
         *
294
         * @return the object
295
         */
296
        private Object convertIfNeeded(final Object property) {
297
            if (property == null) {
1✔
298
                return NOT_FOUND;
1✔
299
            }
300

301
            if (property instanceof ScriptableDelegate[]) {
1✔
302
                return toScriptable((ScriptableDelegate[]) property);
1✔
303
            }
304
            if (!(property instanceof ScriptableDelegate)) {
1✔
305
                return property;
1✔
306
            }
307
            return toScriptable((ScriptableDelegate) property);
1✔
308
        }
309

310
        /**
311
         * To scriptable.
312
         *
313
         * @param list
314
         *            the list
315
         *
316
         * @return the object
317
         */
318
        private Object toScriptable(ScriptableDelegate[] list) {
319
            Object[] delegates = new Object[list.length];
1✔
320
            for (int i = 0; i < delegates.length; i++) {
1✔
321
                delegates[i] = toScriptable(list[i]);
1✔
322
            }
323
            return Context.getCurrentContext().newArray(this, delegates);
1✔
324
        }
325

326
        @Override
327
        public void put(String propertyName, Scriptable scriptable, Object value) {
328
            if (_scriptable == null || _scriptable.get(propertyName) == null) {
1✔
329
                super.put(propertyName, scriptable, value);
1✔
330
            } else {
331
                _scriptable.set(propertyName, value);
1✔
332
            }
333
        }
1✔
334

335
        @Override
336
        public String toString() {
337
            return (_scriptable == null ? "prototype " : "") + getClassName();
×
338
        }
339

340
        @Override
341
        public ScriptingEngine newScriptingEngine(ScriptableDelegate child) {
342
            try {
343
                return (ScriptingEngine) toScriptable(child);
1✔
344
            } catch (Exception e) {
×
345
                HttpUnitUtils.handleException(e);
×
346
                throw new RuntimeException(e.toString());
×
347
            }
348
        }
349

350
        @Override
351
        public void clearCaches() {
352
        }
×
353

354
        /**
355
         * To string if not undefined.
356
         *
357
         * @param object
358
         *            the object
359
         *
360
         * @return the string
361
         */
362
        protected static String toStringIfNotUndefined(Object object) {
363
            return object == null || Undefined.instance.equals(object) ? null : object.toString();
1!
364
        }
365

366
        /**
367
         * Converts a scriptable delegate obtained from a subobject into the appropriate Rhino-compatible Scriptable.
368
         *
369
         * @param delegate
370
         *            the delegate
371
         *
372
         * @return the object
373
         */
374
        final Object toScriptable(ScriptableDelegate delegate) {
375
            if (delegate == null) {
1!
376
                return NOT_FOUND;
×
377
            }
378
            if (delegate.getScriptEngine() instanceof Scriptable) {
1✔
379
                return delegate.getScriptEngine();
1✔
380
            }
381
            try {
382
                JavaScriptEngine element = (JavaScriptEngine) Context.getCurrentContext().newObject(this,
1✔
383
                        getScriptableClassName(delegate));
1✔
384
                element.initialize(this, delegate);
1✔
385
                return element;
1✔
386
            } catch (RuntimeException e) {
×
387
                throw e;
×
388
            } catch (Exception e) {
×
389
                throw new RhinoException(e);
×
390
            }
391
        }
392

393
        /**
394
         * get the classname of the given ScriptableDelegate.
395
         *
396
         * @param delegate
397
         *            - the object to get the class name for
398
         *
399
         * @return - the simple local class name for the delegate e.g. Window, Document, Form, Link, Image, Options,
400
         *         Option, Control, HTMLElement
401
         */
402
        private String getScriptableClassName(ScriptableDelegate delegate) {
403
            if (delegate instanceof WebResponse.Scriptable) {
1✔
404
                return "Window";
1✔
405
            }
406
            if (delegate instanceof HTMLPage.Scriptable) {
1✔
407
                return "Document";
1✔
408
            }
409
            if (delegate instanceof FormScriptable) {
1✔
410
                return "Form";
1✔
411
            }
412
            if (delegate instanceof WebLink.Scriptable) {
1✔
413
                return "Link";
1✔
414
            }
415
            if (delegate instanceof WebImage.Scriptable) {
1✔
416
                return "Image";
1✔
417
            }
418
            if (delegate instanceof SelectionOptions) {
1✔
419
                return "Options";
1✔
420
            }
421
            if (delegate instanceof SelectionOption) {
1✔
422
                return "Option";
1✔
423
            }
424
            if (delegate instanceof Input) {
1✔
425
                return "Control";
1✔
426
            }
427
            if (delegate instanceof DocumentElement) {
1!
428
                return "HTMLElement";
1✔
429
            }
430

431
            throw new IllegalArgumentException("Unknown ScriptableDelegate class: " + delegate.getClass());
×
432
        }
433

434
        /**
435
         * To element array.
436
         *
437
         * @param scriptables
438
         *            the scriptables
439
         *
440
         * @return the element array
441
         */
442
        protected ElementArray toElementArray(ScriptingHandler[] scriptables) {
443
            JavaScriptEngine[] elements = new JavaScriptEngine[scriptables.length];
1✔
444
            for (int i = 0; i < elements.length; i++) {
1✔
445
                elements[i] = (JavaScriptEngine) toScriptable((ScriptableDelegate) scriptables[i]);
1✔
446
            }
447
            ElementArray result = ElementArray.newElementArray(this);
1✔
448
            result.initialize(elements);
1✔
449
            return result;
1✔
450
        }
451

452
        /**
453
         * {@inheritDoc}
454
         */
455
        @Override
456
        public void jsFunction_addEventListener(String type, Scriptable listener, boolean useCapture) {
457
            if (useCapture) {
×
458
                Set set = (Set) _eventCaptureListeners.get(type); // Set<Scriptable>
×
459
                if (set == null) {
×
460
                    set = new HashSet<>();
×
461
                    _eventCaptureListeners.put(type, set);
×
462
                }
463
                set.add(listener);
×
464
            } else {
×
465
                Set set = (Set) _eventListeners.get(type); // Set<Scriptable>
×
466
                if (set == null) {
×
467
                    set = new HashSet<>();
×
468
                    _eventListeners.put(type, set);
×
469
                }
470
                set.add(listener);
×
471
            }
472
            // System.out.println(getClassName()+".addEventListener("+type+")");
473
        }
×
474

475
        /**
476
         * {@inheritDoc}
477
         */
478
        @Override
479
        public boolean jsFunction_dispatchEvent(Scriptable evt) throws EventException {
480
            // TODO implement event dispatching & listener invocation
481
            // System.out.println(getClassName()+".dispatchEvent("+evt.get("type",evt)+")");
482
            return true;
×
483
        }
484

485
        /**
486
         * {@inheritDoc}
487
         */
488
        @Override
489
        public void jsFunction_removeEventListener(String type, Scriptable listener, boolean useCapture) {
490
            if (useCapture) {
×
491
                Set set = (Set) _eventCaptureListeners.get(type); // Set<EventListener>
×
492
                if (set != null) {
×
493
                    set.remove(listener);
×
494
                }
495
            } else {
×
496
                Set set = (Set) _eventListeners.get(type); // Set<EventListener>
×
497
                if (set != null) {
×
498
                    set.remove(listener);
×
499
                }
500
            }
501
            // System.out.println(getClassName()+".removeEventListener("+type+")");
502
        }
×
503
    }
504

505
    /**
506
     * Window functions.
507
     */
508
    static public class Window extends JavaScriptEngine {
1✔
509

510
        /** The Constant serialVersionUID. */
511
        private static final long serialVersionUID = 1L;
512

513
        /** The document. */
514
        private Document _document;
515

516
        /** The navigator. */
517
        private Navigator _navigator;
518

519
        /** The location. */
520
        private Location _location;
521

522
        /** The screen. */
523
        private Screen _screen;
524

525
        /** The frames. */
526
        private ElementArray _frames;
527

528
        @Override
529
        public String getClassName() {
530
            return "Window";
1✔
531
        }
532

533
        /**
534
         * Js get window.
535
         *
536
         * @return the window
537
         */
538
        public Window jsGet_window() {
539
            return this;
1✔
540
        }
541

542
        /**
543
         * Js get self.
544
         *
545
         * @return the window
546
         */
547
        public Window jsGet_self() {
548
            return this;
×
549
        }
550

551
        /**
552
         * Js get document.
553
         *
554
         * @return the document
555
         */
556
        public Document jsGet_document() {
557
            if (_document == null) {
1✔
558
                _document = (Document) toScriptable(getDelegate().getDocument());
1✔
559
            }
560
            return _document;
1✔
561
        }
562

563
        /**
564
         * Js get frames.
565
         *
566
         * @return the scriptable
567
         *
568
         * @throws SAXException
569
         *             the SAX exception
570
         * @throws JavaScriptException
571
         *             the java script exception
572
         * @throws EvaluatorException
573
         *             the evaluator exception
574
         */
575
        public Scriptable jsGet_frames() throws SAXException, JavaScriptException, EvaluatorException {
576
            if (_frames == null) {
1✔
577
                WebResponse.Scriptable[] scriptables = getDelegate().getFrames();
1✔
578
                Window[] frames = new Window[scriptables.length];
1✔
579
                for (int i = 0; i < frames.length; i++) {
1✔
580
                    frames[i] = (Window) toScriptable(scriptables[i]);
1✔
581
                }
582
                _frames = (ElementArray) Context.getCurrentContext().newObject(this, "ElementArray");
1✔
583
                _frames.initialize(frames);
1✔
584
            }
585
            return _frames;
1✔
586
        }
587

588
        /**
589
         * Js get navigator.
590
         *
591
         * @return the navigator
592
         */
593
        public Navigator jsGet_navigator() {
594
            return _navigator;
1✔
595
        }
596

597
        /**
598
         * Js get screen.
599
         *
600
         * @return the screen
601
         */
602
        public Screen jsGet_screen() {
603
            return _screen;
1✔
604
        }
605

606
        /**
607
         * Js get location.
608
         *
609
         * @return the location
610
         */
611
        public Location jsGet_location() {
612
            return _location;
1✔
613
        }
614

615
        /**
616
         * Js set location.
617
         *
618
         * @param relativeURL
619
         *            the relative URL
620
         *
621
         * @throws IOException
622
         *             Signals that an I/O exception has occurred.
623
         * @throws SAXException
624
         *             the SAX exception
625
         */
626
        public void jsSet_location(String relativeURL) throws IOException, SAXException {
627
            setLocation(relativeURL);
1✔
628
        }
1✔
629

630
        /**
631
         * Sets the location.
632
         *
633
         * @param relativeURL
634
         *            the new location
635
         *
636
         * @throws IOException
637
         *             Signals that an I/O exception has occurred.
638
         * @throws SAXException
639
         *             the SAX exception
640
         */
641
        void setLocation(String relativeURL) throws IOException, SAXException {
642
            getDelegate().setLocation(relativeURL);
1✔
643
        }
1✔
644

645
        /**
646
         * initialize JavaScript for the given ScriptEngine
647
         *
648
         * @parent - the Script Engine to use
649
         *
650
         * @scriptable - the scriptable object to do the initialization for
651
         */
652
        @Override
653
        void initialize(JavaScriptEngine parent, ScriptableDelegate scriptable)
654
                throws JavaScriptException, EvaluatorException, SAXException {
655
            super.initialize(parent, scriptable);
1✔
656

657
            _location = (Location) Context.getCurrentContext().newObject(this, "Location");
1✔
658
            _location.initialize(this, ((WebResponse.Scriptable) scriptable).getURL());
1✔
659

660
            _navigator = (Navigator) Context.getCurrentContext().newObject(this, "Navigator");
1✔
661
            _navigator.setClientProperties(getDelegate().getClientProperties());
1✔
662

663
            _screen = (Screen) Context.getCurrentContext().newObject(this, "Screen");
1✔
664
            _screen.setClientProperties(getDelegate().getClientProperties());
1✔
665
        }
1✔
666

667
        /**
668
         * javascript alert handling.
669
         *
670
         * @param message
671
         *            - the alert message
672
         */
673
        public void jsFunction_alert(String message) {
674
            getDelegate().alertUser(message);
1✔
675
        }
1✔
676

677
        /**
678
         * javascript built in function "toLowerCase".
679
         *
680
         * @param s
681
         *            the s
682
         *
683
         * @return the string
684
         */
685
        public String jsFunction_toLowerCase(String s) {
686
            return s.toLowerCase(Locale.ENGLISH);
1✔
687
        }
688

689
        /**
690
         * Js function confirm.
691
         *
692
         * @param message
693
         *            the message
694
         *
695
         * @return true, if successful
696
         */
697
        public boolean jsFunction_confirm(String message) {
698
            return getDelegate().getConfirmationResponse(message);
1✔
699
        }
700

701
        /**
702
         * Js function prompt.
703
         *
704
         * @param message
705
         *            the message
706
         * @param defaultResponse
707
         *            the default response
708
         *
709
         * @return the string
710
         */
711
        public String jsFunction_prompt(String message, String defaultResponse) {
712
            return getDelegate().getUserResponse(message, defaultResponse);
1✔
713
        }
714

715
        /**
716
         * Js function move to.
717
         *
718
         * @param x
719
         *            the x
720
         * @param y
721
         *            the y
722
         */
723
        public void jsFunction_moveTo(int x, int y) {
724
        }
×
725

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

737
        /**
738
         * Js function focus.
739
         */
740
        public void jsFunction_focus() {
741
        }
×
742

743
        /**
744
         * Js function set timeout.
745
         */
746
        public void jsFunction_setTimeout() {
747
        }
×
748

749
        /**
750
         * Js function close.
751
         */
752
        public void jsFunction_close() {
753
            getDelegate().closeWindow();
1✔
754
        }
1✔
755

756
        /**
757
         * Js function open.
758
         *
759
         * @param url
760
         *            the url
761
         * @param name
762
         *            the name
763
         * @param features
764
         *            the features
765
         * @param replace
766
         *            the replace
767
         *
768
         * @return the window
769
         *
770
         * @throws JavaScriptException
771
         *             the java script exception
772
         * @throws EvaluatorException
773
         *             the evaluator exception
774
         * @throws IOException
775
         *             Signals that an I/O exception has occurred.
776
         * @throws SAXException
777
         *             the SAX exception
778
         */
779
        public Window jsFunction_open(Object url, String name, String features, boolean replace)
780
                throws JavaScriptException, EvaluatorException, IOException, SAXException {
781
            WebResponse.Scriptable delegate = getDelegate().open(toStringIfNotUndefined(url), name, features, replace);
1✔
782
            return delegate == null ? null : (Window) toScriptable(delegate);
1✔
783
        }
784

785
        /**
786
         * The global "event" object is not supported, so return null (instead of causing '"event" is not defined').
787
         *
788
         * @return the location
789
         */
790
        public Location jsGet_event() {
791
            return null;
×
792
        }
793

794
        @Override
795
        public void clearCaches() {
796
            if (_document != null) {
1✔
797
                _document.clearCaches();
1✔
798
            }
799
        }
1✔
800

801
        @Override
802
        protected String getDocumentWriteBuffer() {
803
            return jsGet_document().getWriteBuffer().toString();
1✔
804
        }
805

806
        @Override
807
        protected void discardDocumentWriteBuffer() {
808
            jsGet_document().clearWriteBuffer();
1✔
809
        }
1✔
810

811
        /**
812
         * Gets the delegate.
813
         *
814
         * @return the delegate
815
         */
816
        private WebResponse.Scriptable getDelegate() {
817
            return (WebResponse.Scriptable) _scriptable;
1✔
818
        }
819
    }
820

821
    /**
822
     * Document script handling.
823
     */
824
    static public class Document extends JavaScriptEngine {
1✔
825

826
        /** The Constant serialVersionUID. */
827
        private static final long serialVersionUID = 1L;
828

829
        /** The forms. */
830
        private ElementArray _forms;
831

832
        /** The links. */
833
        private ElementArray _links;
834

835
        /** The images. */
836
        private ElementArray _images;
837

838
        /** The write buffer. */
839
        private StringBuilder _writeBuffer;
840

841
        /** The mime type. */
842
        private String _mimeType;
843

844
        @Override
845
        public String getClassName() {
846
            return "Document";
1✔
847
        }
848

849
        @Override
850
        public void clearCaches() {
851
            _forms = _links = _images = null;
1✔
852
        }
1✔
853

854
        /**
855
         * Js get title.
856
         *
857
         * @return the string
858
         *
859
         * @throws SAXException
860
         *             the SAX exception
861
         */
862
        public String jsGet_title() throws SAXException {
863
            return getDelegate().getTitle();
1✔
864
        }
865

866
        /**
867
         * Js get images.
868
         *
869
         * @return the scriptable
870
         *
871
         * @throws SAXException
872
         *             the SAX exception
873
         */
874
        public Scriptable jsGet_images() throws SAXException {
875
            if (_images == null) {
1✔
876
                _images = toElementArray(getDelegate().getImages());
1✔
877
            }
878
            return _images;
1✔
879
        }
880

881
        /**
882
         * Js get links.
883
         *
884
         * @return the scriptable
885
         *
886
         * @throws SAXException
887
         *             the SAX exception
888
         */
889
        public Scriptable jsGet_links() throws SAXException {
890
            if (_links == null) {
1✔
891
                _links = toElementArray(getDelegate().getLinks());
1✔
892
            }
893
            return _links;
1✔
894
        }
895

896
        /**
897
         * Js get forms.
898
         *
899
         * @return the scriptable
900
         *
901
         * @throws SAXException
902
         *             the SAX exception
903
         */
904
        public Scriptable jsGet_forms() throws SAXException {
905
            if (_forms == null) {
1✔
906
                _forms = toElementArray(getDelegate().getForms());
1✔
907
            }
908
            return _forms;
1✔
909
        }
910

911
        /**
912
         * Js function get element by id.
913
         *
914
         * @param id
915
         *            the id
916
         *
917
         * @return the object
918
         */
919
        public Object jsFunction_getElementById(String id) {
920
            ScriptableDelegate elementWithID = getDelegate().getElementWithID(id);
1✔
921
            return elementWithID == null ? null : toScriptable(elementWithID);
1✔
922
        }
923

924
        /**
925
         * Js function get elements by name.
926
         *
927
         * @param name
928
         *            the name
929
         *
930
         * @return the object
931
         */
932
        public Object jsFunction_getElementsByName(String name) {
933
            return toElementArray(getDelegate().getElementsByName(name));
1✔
934
        }
935

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

948
        /**
949
         * Js get location.
950
         *
951
         * @return the object
952
         */
953
        public Object jsGet_location() {
954
            return _parent == null ? NOT_FOUND : getWindow().jsGet_location();
1!
955
        }
956

957
        /**
958
         * Js set location.
959
         *
960
         * @param urlString
961
         *            the url string
962
         *
963
         * @throws IOException
964
         *             Signals that an I/O exception has occurred.
965
         * @throws SAXException
966
         *             the SAX exception
967
         */
968
        public void jsSet_location(String urlString) throws IOException, SAXException {
969
            if (urlString.startsWith("color")) {
1!
970
                return;
×
971
            }
972
            getWindow().setLocation(urlString);
1✔
973
        }
1✔
974

975
        /**
976
         * Js get cookie.
977
         *
978
         * @return the string
979
         */
980
        public String jsGet_cookie() {
981
            return getDelegate().getCookie();
1✔
982
        }
983

984
        /**
985
         * Js set cookie.
986
         *
987
         * @param cookieSpec
988
         *            the cookie spec
989
         */
990
        public void jsSet_cookie(String cookieSpec) {
991
            final int equalsIndex = cookieSpec.indexOf('=');
1✔
992
            if (equalsIndex < 0) {
1✔
993
                return;
1✔
994
            }
995
            int endIndex = cookieSpec.indexOf(";", equalsIndex);
1✔
996
            if (endIndex < 0) {
1!
997
                endIndex = cookieSpec.length();
×
998
            }
999
            String name = cookieSpec.substring(0, equalsIndex);
1✔
1000
            String value = cookieSpec.substring(equalsIndex + 1, endIndex);
1✔
1001
            getDelegate().setCookie(name, value);
1✔
1002
        }
1✔
1003

1004
        /**
1005
         * Gets the window.
1006
         *
1007
         * @return the window
1008
         */
1009
        private Window getWindow() {
1010
            return (Window) _parent;
1✔
1011
        }
1012

1013
        /**
1014
         * Js function open.
1015
         *
1016
         * @param mimeType
1017
         *            the mime type
1018
         */
1019
        public void jsFunction_open(Object mimeType) {
1020
            _mimeType = toStringIfNotUndefined(mimeType);
1✔
1021
        }
1✔
1022

1023
        /**
1024
         * Js function close.
1025
         */
1026
        public void jsFunction_close() {
1027
            if (getDelegate().replaceText(getWriteBuffer().toString(), _mimeType == null ? "text/html" : _mimeType)) {
1✔
1028
                getWriteBuffer().setLength(0);
1✔
1029
            }
1030
        }
1✔
1031

1032
        /**
1033
         * Js function write.
1034
         *
1035
         * @param string
1036
         *            the string
1037
         */
1038
        public void jsFunction_write(String string) {
1039
            getWriteBuffer().append(string);
1✔
1040
        }
1✔
1041

1042
        /**
1043
         * Js function writeln.
1044
         *
1045
         * @param string
1046
         *            the string
1047
         */
1048
        public void jsFunction_writeln(String string) {
1049
            getWriteBuffer().append(string).append((char) 0x0D).append((char) 0x0A);
1✔
1050
        }
1✔
1051

1052
        /**
1053
         * Gets the write buffer.
1054
         *
1055
         * @return the write buffer
1056
         */
1057
        protected StringBuilder getWriteBuffer() {
1058
            if (_writeBuffer == null) {
1✔
1059
                _writeBuffer = new StringBuilder();
1✔
1060
            }
1061
            return _writeBuffer;
1✔
1062
        }
1063

1064
        /**
1065
         * Clear write buffer.
1066
         */
1067
        protected void clearWriteBuffer() {
1068
            _writeBuffer = null;
1✔
1069
        }
1✔
1070

1071
        /**
1072
         * Gets the delegate.
1073
         *
1074
         * @return the delegate
1075
         */
1076
        private HTMLPage.Scriptable getDelegate() {
1077
            return (HTMLPage.Scriptable) _scriptable;
1✔
1078
        }
1079

1080
        @Override
1081
        public void jsFunction_addEventListener(String type, Scriptable listener, boolean useCapture) {
1082
            // TODO Auto-generated method stub
1083

1084
        }
×
1085

1086
        @Override
1087
        public boolean jsFunction_dispatchEvent(Scriptable evt) throws EventException {
1088
            // TODO Auto-generated method stub
1089
            return false;
×
1090
        }
1091

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

1096
        }
×
1097

1098
    }
1099

1100
    /**
1101
     * The Class Location.
1102
     */
1103
    static public class Location extends JavaScriptEngine {
1✔
1104

1105
        /** The Constant serialVersionUID. */
1106
        private static final long serialVersionUID = 1L;
1107

1108
        /** The url. */
1109
        private URL _url;
1110

1111
        /** The window. */
1112
        private Window _window;
1113

1114
        @Override
1115
        public String getClassName() {
1116
            return "Location";
1✔
1117
        }
1118

1119
        /**
1120
         * Initialize.
1121
         *
1122
         * @param window
1123
         *            the window
1124
         * @param url
1125
         *            the url
1126
         */
1127
        void initialize(Window window, URL url) {
1128
            _window = window;
1✔
1129
            _url = url;
1✔
1130
        }
1✔
1131

1132
        /**
1133
         * Js function replace.
1134
         *
1135
         * @param urlString
1136
         *            the url string
1137
         *
1138
         * @throws IOException
1139
         *             Signals that an I/O exception has occurred.
1140
         * @throws SAXException
1141
         *             the SAX exception
1142
         */
1143
        public void jsFunction_replace(String urlString) throws IOException, SAXException {
1144
            _window.setLocation(urlString);
1✔
1145
        }
1✔
1146

1147
        /**
1148
         * Js get href.
1149
         *
1150
         * @return the string
1151
         */
1152
        public String jsGet_href() {
1153
            return toString();
1✔
1154
        }
1155

1156
        /**
1157
         * Js set href.
1158
         *
1159
         * @param urlString
1160
         *            the url string
1161
         *
1162
         * @throws SAXException
1163
         *             the SAX exception
1164
         * @throws IOException
1165
         *             Signals that an I/O exception has occurred.
1166
         */
1167
        public void jsSet_href(String urlString) throws SAXException, IOException {
1168
            _window.setLocation(urlString);
1✔
1169
        }
1✔
1170

1171
        /**
1172
         * Js get protocol.
1173
         *
1174
         * @return the string
1175
         */
1176
        public String jsGet_protocol() {
1177
            return _url.getProtocol() + ':';
1✔
1178
        }
1179

1180
        /**
1181
         * Js get host.
1182
         *
1183
         * @return the string
1184
         */
1185
        public String jsGet_host() {
1186
            return _url.getHost() + ':' + _url.getPort();
1✔
1187
        }
1188

1189
        /**
1190
         * Js get hostname.
1191
         *
1192
         * @return the string
1193
         */
1194
        public String jsGet_hostname() {
1195
            return _url.getHost();
1✔
1196
        }
1197

1198
        /**
1199
         * Js get port.
1200
         *
1201
         * @return the string
1202
         */
1203
        public String jsGet_port() {
1204
            return String.valueOf(_url.getPort());
1✔
1205
        }
1206

1207
        /**
1208
         * Js get pathname.
1209
         *
1210
         * @return the string
1211
         */
1212
        public String jsGet_pathname() {
1213
            return _url.getPath();
1✔
1214
        }
1215

1216
        /**
1217
         * Js set pathname.
1218
         *
1219
         * @param newPath
1220
         *            the new path
1221
         *
1222
         * @throws SAXException
1223
         *             the SAX exception
1224
         * @throws IOException
1225
         *             Signals that an I/O exception has occurred.
1226
         */
1227
        public void jsSet_pathname(String newPath) throws SAXException, IOException {
1228
            if (!newPath.startsWith("/")) {
1!
1229
                newPath = '/' + newPath;
×
1230
            }
1231
            URL newURL = new URL(_url, newPath);
1✔
1232
            _window.setLocation(newURL.toExternalForm());
1✔
1233
        }
1✔
1234

1235
        /**
1236
         * Js get search.
1237
         *
1238
         * @return the string
1239
         */
1240
        public String jsGet_search() {
1241
            return '?' + _url.getQuery();
1✔
1242
        }
1243

1244
        /**
1245
         * Js set search.
1246
         *
1247
         * @param newSearch
1248
         *            the new search
1249
         *
1250
         * @throws SAXException
1251
         *             the SAX exception
1252
         * @throws IOException
1253
         *             Signals that an I/O exception has occurred.
1254
         */
1255
        public void jsSet_search(String newSearch) throws SAXException, IOException {
1256
            if (!newSearch.startsWith("?")) {
1!
1257
                newSearch = '?' + newSearch;
×
1258
            }
1259
            _window.setLocation(jsGet_protocol() + "//" + jsGet_host() + jsGet_pathname() + newSearch);
1✔
1260
        }
1✔
1261

1262
        /**
1263
         * Returns the default value of this scriptable object. In this case, it returns simply the URL as a string.
1264
         * Note that this method is necessary, since Rhino will only call the toString method directly if there are no
1265
         * Rhino methods defined (jsGet_*, jsFunction_*, etc.)
1266
         */
1267
        @Override
1268
        public Object getDefaultValue(Class typeHint) {
1269
            return _url.toExternalForm();
1✔
1270
        }
1271

1272
        @Override
1273
        public String toString() {
1274
            return _url.toExternalForm();
1✔
1275
        }
1276

1277
    }
1278

1279
    /**
1280
     * The Class Style.
1281
     */
1282
    static public class Style extends JavaScriptEngine {
1✔
1283

1284
        /** The Constant serialVersionUID. */
1285
        private static final long serialVersionUID = 1L;
1286

1287
        /** The display. */
1288
        private String _display = "inline";
1✔
1289

1290
        /** The visibility. */
1291
        private String _visibility = "visible";
1✔
1292

1293
        @Override
1294
        public String getClassName() {
1295
            return "Style";
1✔
1296
        }
1297

1298
        /**
1299
         * Js get display.
1300
         *
1301
         * @return the string
1302
         */
1303
        public String jsGet_display() {
1304
            return _display;
1✔
1305
        }
1306

1307
        /**
1308
         * Js set display.
1309
         *
1310
         * @param display
1311
         *            the display
1312
         */
1313
        public void jsSet_display(String display) {
1314
            _display = display;
1✔
1315
        }
1✔
1316

1317
        /**
1318
         * Js get visibility.
1319
         *
1320
         * @return the string
1321
         */
1322
        public String jsGet_visibility() {
1323
            return _visibility;
1✔
1324
        }
1325

1326
        /**
1327
         * Js set visibility.
1328
         *
1329
         * @param visibility
1330
         *            the visibility
1331
         */
1332
        public void jsSet_visibility(String visibility) {
1333
            _visibility = visibility;
1✔
1334
        }
1✔
1335
    }
1336

1337
    /**
1338
     * The Class Navigator.
1339
     */
1340
    static public class Navigator extends JavaScriptEngine {
1✔
1341

1342
        /** The Constant serialVersionUID. */
1343
        private static final long serialVersionUID = 1L;
1344

1345
        /** The client properties. */
1346
        private ClientProperties _clientProperties;
1347

1348
        @Override
1349
        public String getClassName() {
1350
            return "Navigator";
1✔
1351
        }
1352

1353
        /**
1354
         * Sets the client properties.
1355
         *
1356
         * @param clientProperties
1357
         *            the new client properties
1358
         */
1359
        void setClientProperties(ClientProperties clientProperties) {
1360
            _clientProperties = clientProperties;
1✔
1361
        }
1✔
1362

1363
        /**
1364
         * Js get app name.
1365
         *
1366
         * @return the string
1367
         */
1368
        public String jsGet_appName() {
1369
            return _clientProperties.getApplicationName();
1✔
1370
        }
1371

1372
        /**
1373
         * Js get app code name.
1374
         *
1375
         * @return the string
1376
         */
1377
        public String jsGet_appCodeName() {
1378
            return _clientProperties.getApplicationCodeName();
1✔
1379
        }
1380

1381
        /**
1382
         * Js get app version.
1383
         *
1384
         * @return the string
1385
         */
1386
        public String jsGet_appVersion() {
1387
            return _clientProperties.getApplicationVersion();
1✔
1388
        }
1389

1390
        /**
1391
         * Js get user agent.
1392
         *
1393
         * @return the string
1394
         */
1395
        public String jsGet_userAgent() {
1396
            return _clientProperties.getUserAgent();
1✔
1397
        }
1398

1399
        /**
1400
         * Js get platform.
1401
         *
1402
         * @return the string
1403
         */
1404
        public String jsGet_platform() {
1405
            return _clientProperties.getPlatform();
1✔
1406
        }
1407

1408
        /**
1409
         * Js get plugins.
1410
         *
1411
         * @return the object[]
1412
         */
1413
        public Object[] jsGet_plugins() {
1414
            return new Object[0];
1✔
1415
        }
1416

1417
        /**
1418
         * Js function java enabled.
1419
         *
1420
         * @return true, if successful
1421
         */
1422
        public boolean jsFunction_javaEnabled() {
1423
            return false; // no support is provided for applets at present
1✔
1424
        }
1425

1426
    }
1427

1428
    /**
1429
     * The Class Screen.
1430
     */
1431
    static public class Screen extends JavaScriptEngine {
1✔
1432

1433
        /** The Constant serialVersionUID. */
1434
        private static final long serialVersionUID = 1L;
1435

1436
        /** The client properties. */
1437
        private ClientProperties _clientProperties;
1438

1439
        /**
1440
         * Sets the client properties.
1441
         *
1442
         * @param clientProperties
1443
         *            the new client properties
1444
         */
1445
        void setClientProperties(ClientProperties clientProperties) {
1446
            _clientProperties = clientProperties;
1✔
1447
        }
1✔
1448

1449
        @Override
1450
        public String getClassName() {
1451
            return "Screen";
1✔
1452
        }
1453

1454
        /**
1455
         * Js get avail width.
1456
         *
1457
         * @return the int
1458
         */
1459
        public int jsGet_availWidth() {
1460
            return _clientProperties.getAvailableScreenWidth();
1✔
1461
        }
1462

1463
        /**
1464
         * Js get avail height.
1465
         *
1466
         * @return the int
1467
         */
1468
        public int jsGet_availHeight() {
1469
            return _clientProperties.getAvailHeight();
1✔
1470
        }
1471

1472
    }
1473

1474
    /**
1475
     * The Class ElementArray.
1476
     */
1477
    static public class ElementArray extends ScriptableObject {
1478

1479
        /** The Constant serialVersionUID. */
1480
        private static final long serialVersionUID = 1L;
1481

1482
        /** The contents. */
1483
        private JavaScriptEngine[] _contents = new HTMLElement[0];
1✔
1484

1485
        /**
1486
         * New element array.
1487
         *
1488
         * @param parent
1489
         *            the parent
1490
         *
1491
         * @return the element array
1492
         */
1493
        static ElementArray newElementArray(Scriptable parent) {
1494
            try {
1495
                return (ElementArray) Context.getCurrentContext().newObject(parent, "ElementArray");
1✔
1496
            } catch (EvaluatorException | JavaScriptException e) {
×
1497
                throw new RhinoException(e);
×
1498
            }
1499
        }
1500

1501
        /**
1502
         * Instantiates a new element array.
1503
         */
1504
        public ElementArray() {
1✔
1505
        }
1✔
1506

1507
        /**
1508
         * Initialize.
1509
         *
1510
         * @param contents
1511
         *            the contents
1512
         */
1513
        void initialize(JavaScriptEngine[] contents) {
1514
            _contents = contents;
1✔
1515
        }
1✔
1516

1517
        /**
1518
         * Js get length.
1519
         *
1520
         * @return the int
1521
         */
1522
        public int jsGet_length() {
1523
            return _contents.length;
1✔
1524
        }
1525

1526
        @Override
1527
        public String getClassName() {
1528
            return "ElementArray";
1✔
1529
        }
1530

1531
        @Override
1532
        public Object get(int i, Scriptable scriptable) {
1533
            if (i >= 0 && i < _contents.length) {
1!
1534
                return _contents[i];
1✔
1535
            }
1536
            return super.get(i, scriptable);
×
1537
        }
1538

1539
        @Override
1540
        public Object get(String name, Scriptable scriptable) {
1541
            for (JavaScriptEngine content : _contents) {
1✔
1542
                if (name.equalsIgnoreCase(content.getID())) {
1✔
1543
                    return content;
1✔
1544
                }
1545
            }
1546
            for (JavaScriptEngine content : _contents) {
1✔
1547
                if (name.equalsIgnoreCase(content.getName())) {
1✔
1548
                    return content;
1✔
1549
                }
1550
            }
1551
            return super.get(name, scriptable);
1✔
1552
        }
1553

1554
        /**
1555
         * Gets the contents.
1556
         *
1557
         * @return the contents
1558
         */
1559
        protected JavaScriptEngine[] getContents() {
1560
            return _contents;
×
1561
        }
1562
    }
1563

1564
    /**
1565
     * HTML Element support for JavaScript.
1566
     */
1567
    static public class HTMLElement extends JavaScriptEngine {
1✔
1568

1569
        /** The Constant serialVersionUID. */
1570
        private static final long serialVersionUID = 1L;
1571

1572
        /** The style. */
1573
        private Style _style;
1574

1575
        /** The document. */
1576
        private Document _document;
1577

1578
        @Override
1579
        public String getClassName() {
1580
            return "HTMLElement";
1✔
1581
        }
1582

1583
        /**
1584
         * Js get document.
1585
         *
1586
         * @return the document
1587
         */
1588
        public Document jsGet_document() {
1589
            return _document;
1✔
1590
        }
1591

1592
        /**
1593
         * Js get style.
1594
         *
1595
         * @return the style
1596
         */
1597
        public Style jsGet_style() {
1598
            return _style;
1✔
1599
        }
1600

1601
        /**
1602
         * arbitrary attribute access.
1603
         *
1604
         * @param attributeName
1605
         *            the attribute name
1606
         *
1607
         * @return the object
1608
         */
1609
        public Object jsFunction_getAttribute(String attributeName) {
1610
            return _scriptable.get(attributeName);
×
1611
        }
1612

1613
        @Override
1614
        void initialize(JavaScriptEngine parent, ScriptableDelegate scriptable)
1615
                throws JavaScriptException, EvaluatorException, SAXException {
1616
            super.initialize(parent, scriptable);
1✔
1617
            _document = (Document) parent;
1✔
1618
            _style = (Style) Context.getCurrentContext().newObject(this, "Style");
1✔
1619
        }
1✔
1620

1621
    }
1622

1623
    /**
1624
     * The Class Image.
1625
     */
1626
    static public class Image extends HTMLElement {
1✔
1627

1628
        /** The Constant serialVersionUID. */
1629
        private static final long serialVersionUID = 1L;
1630

1631
        @Override
1632
        public String getClassName() {
1633
            return "Image";
1✔
1634
        }
1635
    }
1636

1637
    /**
1638
     * The Class Link.
1639
     */
1640
    static public class Link extends HTMLElement {
1✔
1641

1642
        /** The Constant serialVersionUID. */
1643
        private static final long serialVersionUID = 1L;
1644

1645
        @Override
1646
        public Document jsGet_document() {
1647
            return super.jsGet_document();
1✔
1648
        }
1649

1650
        @Override
1651
        public String getClassName() {
1652
            return "Link";
1✔
1653
        }
1654
    }
1655

1656
    /**
1657
     * Form functions.
1658
     */
1659
    static public class Form extends HTMLElement {
1✔
1660

1661
        /** The Constant serialVersionUID. */
1662
        private static final long serialVersionUID = 1L;
1663

1664
        /** The controls. */
1665
        private ElementArray _controls;
1666

1667
        @Override
1668
        public String getClassName() {
1669
            return "Form";
1✔
1670
        }
1671

1672
        /**
1673
         * Js get name.
1674
         *
1675
         * @return the string
1676
         */
1677
        public String jsGet_name() {
1678
            return getDelegate().getName();
1✔
1679
        }
1680

1681
        /**
1682
         * Js set name.
1683
         *
1684
         * @param name
1685
         *            the name
1686
         */
1687
        public void jsSet_name(String name) {
1688
            getDelegate().set("name", name);
1✔
1689
        }
1✔
1690

1691
        /**
1692
         * Js get action.
1693
         *
1694
         * @return the string
1695
         */
1696
        public String jsGet_action() {
1697
            return getDelegate().getAction();
×
1698
        }
1699

1700
        /**
1701
         * Js set action.
1702
         *
1703
         * @param action
1704
         *            the action
1705
         */
1706
        public void jsSet_action(String action) {
1707
            getDelegate().setAction(action);
×
1708
        }
×
1709

1710
        /**
1711
         * Js get elements.
1712
         *
1713
         * @return the scriptable
1714
         *
1715
         * @throws EvaluatorException
1716
         *             the evaluator exception
1717
         * @throws JavaScriptException
1718
         *             the java script exception
1719
         */
1720
        public Scriptable jsGet_elements() throws EvaluatorException, JavaScriptException {
1721
            if (_controls == null) {
1✔
1722
                initializeControls();
1✔
1723
            }
1724
            return _controls;
1✔
1725
        }
1726

1727
        /**
1728
         * Js function get elements by tag name.
1729
         *
1730
         * @param name
1731
         *            the name
1732
         *
1733
         * @return the object
1734
         *
1735
         * @throws SAXException
1736
         *             the SAX exception
1737
         */
1738
        public Object jsFunction_getElementsByTagName(String name) throws SAXException {
1739
            return toElementArray(getDelegate().getElementsByTagName(name));
1✔
1740
        }
1741

1742
        /**
1743
         * Js function submit.
1744
         *
1745
         * @throws IOException
1746
         *             Signals that an I/O exception has occurred.
1747
         * @throws SAXException
1748
         *             the SAX exception
1749
         */
1750
        public void jsFunction_submit() throws IOException, SAXException {
1751
            getDelegate().submit();
1✔
1752
        }
1✔
1753

1754
        /**
1755
         * Js function reset.
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_reset() throws IOException, SAXException {
1763
            getDelegate().reset();
1✔
1764
        }
1✔
1765

1766
        /**
1767
         * Initialize controls.
1768
         *
1769
         * @throws EvaluatorException
1770
         *             the evaluator exception
1771
         * @throws JavaScriptException
1772
         *             the java script exception
1773
         */
1774
        private void initializeControls() throws EvaluatorException, JavaScriptException {
1775
            ScriptableDelegate[] scriptables = getDelegate().getElementDelegates();
1✔
1776
            Control[] controls = new Control[scriptables.length];
1✔
1777
            for (int i = 0; i < controls.length; i++) {
1✔
1778
                controls[i] = (Control) toScriptable(scriptables[i]);
1✔
1779
            }
1780
            _controls = (ElementArray) Context.getCurrentContext().newObject(this, "ElementArray");
1✔
1781
            _controls.initialize(controls);
1✔
1782
        }
1✔
1783

1784
        /**
1785
         * Gets the delegate.
1786
         *
1787
         * @return the delegate
1788
         */
1789
        private WebForm.Scriptable getDelegate() {
1790
            return (WebForm.Scriptable) _scriptable;
1✔
1791
        }
1792

1793
    }
1794

1795
    /**
1796
     * Javascript support for any control.
1797
     */
1798
    static public class Control extends JavaScriptEngine {
1✔
1799

1800
        /** The Constant serialVersionUID. */
1801
        private static final long serialVersionUID = 1L;
1802

1803
        /** The form. */
1804
        private Form _form;
1805

1806
        @Override
1807
        public String getClassName() {
1808
            return "Control";
1✔
1809
        }
1810

1811
        /**
1812
         * Js get form.
1813
         *
1814
         * @return the form
1815
         */
1816
        public Form jsGet_form() {
1817
            return _form;
1✔
1818
        }
1819

1820
        /**
1821
         * Js function focus.
1822
         */
1823
        public void jsFunction_focus() {
1824
        }
×
1825

1826
        /**
1827
         * Js function select.
1828
         */
1829
        public void jsFunction_select() {
1830
        }
×
1831

1832
        /**
1833
         * click via javascript.
1834
         *
1835
         * @throws IOException
1836
         *             Signals that an I/O exception has occurred.
1837
         * @throws SAXException
1838
         *             the SAX exception
1839
         */
1840
        public void jsFunction_click() throws IOException, SAXException {
1841
            getDelegate().click();
1✔
1842
        }
1✔
1843

1844
        /**
1845
         * Gets the delegate.
1846
         *
1847
         * @return the delegate
1848
         */
1849
        private Input getDelegate() {
1850
            return (Input) _scriptable;
1✔
1851
        }
1852

1853
        /**
1854
         * Support getting value of arbitrary attribute.
1855
         *
1856
         * @param attributeName
1857
         *            the attribute name
1858
         *
1859
         * @return the object
1860
         *
1861
         * @throws JavaScriptException
1862
         *             the java script exception
1863
         */
1864
        public Object jsFunction_getAttribute(String attributeName) throws JavaScriptException {
1865
            return getDelegate().get(attributeName);
1✔
1866
        }
1867

1868
        /**
1869
         * Support getting value of arbitrary attribute.
1870
         *
1871
         * @param attributeName
1872
         *            the attribute name
1873
         * @param value
1874
         *            the value
1875
         *
1876
         * @throws JavaScriptException
1877
         *             the java script exception
1878
         */
1879
        public void jsFunction_setAttribute(String attributeName, Object value) throws JavaScriptException {
1880
            getDelegate().setAttribute(attributeName, value);
1✔
1881
        }
1✔
1882

1883
        /**
1884
         * Support getting value of arbitrary attribute.
1885
         *
1886
         * @param attributeName
1887
         *            the attribute name
1888
         *
1889
         * @throws JavaScriptException
1890
         *             the java script exception
1891
         */
1892
        public void jsFunction_removeAttribute(String attributeName) throws JavaScriptException {
1893
            getDelegate().removeAttribute(attributeName);
×
1894
        }
×
1895

1896
        /**
1897
         * Allow calling onchange() from within a JavaScript function.
1898
         *
1899
         * @throws JavaScriptException
1900
         *             the java script exception
1901
         */
1902
        public void jsFunction_onchange() throws JavaScriptException {
1903
            Input myInput = this.getDelegate();
1✔
1904
            myInput.sendOnChangeEvent();
1✔
1905
        }
1✔
1906

1907
        @Override
1908
        void initialize(JavaScriptEngine parent, ScriptableDelegate scriptable)
1909
                throws JavaScriptException, EvaluatorException, SAXException {
1910
            super.initialize(parent, scriptable);
1✔
1911
            if (parent instanceof Form) {
1✔
1912
                _form = (Form) parent;
1✔
1913
            }
1914
        }
1✔
1915

1916
    }
1917

1918
    /**
1919
     * The Class Options.
1920
     */
1921
    static public class Options extends JavaScriptEngine {
1✔
1922

1923
        /** The Constant serialVersionUID. */
1924
        private static final long serialVersionUID = 1L;
1925

1926
        @Override
1927
        public String getClassName() {
1928
            return "Options";
1✔
1929
        }
1930

1931
        /**
1932
         * Js get length.
1933
         *
1934
         * @return the int
1935
         */
1936
        public int jsGet_length() {
1937
            return getDelegate().getLength();
1✔
1938
        }
1939

1940
        /**
1941
         * Js set length.
1942
         *
1943
         * @param length
1944
         *            the length
1945
         */
1946
        public void jsSet_length(int length) {
1947
            getDelegate().setLength(length);
1✔
1948
        }
1✔
1949

1950
        @Override
1951
        public void put(int i, Scriptable scriptable, Object object) {
1952
            if (object == null) {
1✔
1953
                getDelegate().put(i, null);
1✔
1954
            } else {
1955
                if (!(object instanceof Option)) {
1!
1956
                    throw new IllegalArgumentException("May only add an Option to this array");
×
1957
                }
1958
                Option option = (Option) object;
1✔
1959
                getDelegate().put(i, option.getDelegate());
1✔
1960
            }
1961
        }
1✔
1962

1963
        /**
1964
         * Gets the delegate.
1965
         *
1966
         * @return the delegate
1967
         */
1968
        private SelectionOptions getDelegate() {
1969
            return (SelectionOptions) _scriptable;
1✔
1970
        }
1971

1972
    }
1973

1974
    /**
1975
     * The Class Option.
1976
     */
1977
    static public class Option extends JavaScriptEngine {
1✔
1978

1979
        /** The Constant serialVersionUID. */
1980
        private static final long serialVersionUID = 1L;
1981

1982
        @Override
1983
        public String getClassName() {
1984
            return "Option";
1✔
1985
        }
1986

1987
        /**
1988
         * Js constructor.
1989
         *
1990
         * @param text
1991
         *            the text
1992
         * @param value
1993
         *            the value
1994
         * @param defaultSelected
1995
         *            the default selected
1996
         * @param selected
1997
         *            the selected
1998
         */
1999
        public void jsConstructor(String text, String value, boolean defaultSelected, boolean selected) {
2000
            _scriptable = WebResponse.newDelegate("Option");
1✔
2001
            getDelegate().initialize(text, value, defaultSelected, selected);
1✔
2002
        }
1✔
2003

2004
        /**
2005
         * Js get index.
2006
         *
2007
         * @return the int
2008
         */
2009
        public int jsGet_index() {
2010
            return getDelegate().getIndex();
1✔
2011
        }
2012

2013
        /**
2014
         * Js get text.
2015
         *
2016
         * @return the string
2017
         */
2018
        public String jsGet_text() {
2019
            return getDelegate().getText();
1✔
2020
        }
2021

2022
        /**
2023
         * Js set text.
2024
         *
2025
         * @param text
2026
         *            the text
2027
         */
2028
        public void jsSet_text(String text) {
2029
            getDelegate().setText(text);
1✔
2030
        }
1✔
2031

2032
        /**
2033
         * Js get value.
2034
         *
2035
         * @return the string
2036
         */
2037
        public String jsGet_value() {
2038
            return getDelegate().getValue();
1✔
2039
        }
2040

2041
        /**
2042
         * Js set value.
2043
         *
2044
         * @param value
2045
         *            the value
2046
         */
2047
        public void jsSet_value(String value) {
2048
            getDelegate().setValue(value);
1✔
2049
        }
1✔
2050

2051
        /**
2052
         * Js get selected.
2053
         *
2054
         * @return true, if successful
2055
         */
2056
        public boolean jsGet_selected() {
2057
            return getDelegate().isSelected();
1✔
2058
        }
2059

2060
        /**
2061
         * Js set selected.
2062
         *
2063
         * @param selected
2064
         *            the selected
2065
         */
2066
        public void jsSet_selected(boolean selected) {
2067
            getDelegate().setSelected(selected);
1✔
2068
        }
1✔
2069

2070
        /**
2071
         * Js get default selected.
2072
         *
2073
         * @return true, if successful
2074
         */
2075
        public boolean jsGet_defaultSelected() {
2076
            return getDelegate().isDefaultSelected();
×
2077
        }
2078

2079
        /**
2080
         * Gets the delegate.
2081
         *
2082
         * @return the delegate
2083
         */
2084
        SelectionOption getDelegate() {
2085
            return (SelectionOption) _scriptable;
1✔
2086
        }
2087
    }
2088

2089
}
2090

2091
/**
2092
 * special exception for the Rhino Javscript engine
2093
 */
2094
class RhinoException extends RuntimeException {
2095

2096
    private static final long serialVersionUID = 1L;
2097
    private Exception _cause;
2098

2099
    public RhinoException(Exception cause) {
×
2100
        _cause = cause;
×
2101
    }
×
2102

2103
    @Override
2104
    public String getMessage() {
2105
        return "Rhino exception: " + _cause;
×
2106
    }
2107
}
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