• 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

83.08
/src/main/java/com/meterware/httpunit/parsing/ScriptFilter.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.parsing;
9

10
import com.meterware.httpunit.HttpUnitOptions;
11

12
import java.io.IOException;
13
import java.io.StringReader;
14

15
import net.sourceforge.htmlunit.cyberneko.HTMLConfiguration;
16
import net.sourceforge.htmlunit.cyberneko.filters.DefaultFilter;
17

18
import org.apache.xerces.xni.Augmentations;
19
import org.apache.xerces.xni.QName;
20
import org.apache.xerces.xni.XMLAttributes;
21
import org.apache.xerces.xni.XMLLocator;
22
import org.apache.xerces.xni.XMLString;
23
import org.apache.xerces.xni.XNIException;
24
import org.apache.xerces.xni.parser.XMLInputSource;
25

26
/**
27
 * A filter to interpret JavaScript script blocks, based on the sample Scripts program provided by NekoHTML.
28
 **/
29
class ScriptFilter extends DefaultFilter {
30

31
    /** The NekoHTML configuration. */
32
    private HTMLConfiguration _configuration;
33

34
    /** A string buffer to collect the script. */
35
    private StringBuilder _activeScriptBlock;
36

37
    /** The name of the current script language. **/
38
    private String _scriptLanguage;
39

40
    /** The system identifier of the source document. */
41
    private String _systemID = "";
1✔
42

43
    /** The number of the current script. */
44
    private int _scriptIndex;
45

46
    /** The parser in which this filter is running. **/
47
    private ScriptHandler _scriptHandler;
48

49
    /**
50
     * Constructs a script object with the specified configuration.
51
     *
52
     * @param config
53
     *            the config
54
     */
55
    ScriptFilter(HTMLConfiguration config) {
1✔
56
        _configuration = config;
1✔
57
    }
1✔
58

59
    /**
60
     * Sets the script handler.
61
     *
62
     * @param scriptHandler
63
     *            the new script handler
64
     */
65
    public void setScriptHandler(ScriptHandler scriptHandler) {
66
        _scriptHandler = scriptHandler;
1✔
67
    }
1✔
68

69
    /**
70
     * Start document.
71
     *
72
     * @param locator
73
     *            the locator
74
     * @param encoding
75
     *            the encoding
76
     * @param augs
77
     *            the augs
78
     *
79
     * @throws XNIException
80
     *             the XNI exception
81
     */
82
    public void startDocument(XMLLocator locator, String encoding, Augmentations augs) throws XNIException {
83
        _activeScriptBlock = null;
×
84
        _systemID = locator == null ? "" : locator.getLiteralSystemId() + "_";
×
85
        _scriptIndex = 0;
×
86
        super.startDocument(locator, encoding, null, augs);
×
87
    }
×
88

89
    /**
90
     * Invoked for a start element. If the element is a <script>, overrides the normal behavior to begin collecting the
91
     * script text.
92
     */
93
    @Override
94
    public void startElement(QName element, XMLAttributes attrs, Augmentations augs) throws XNIException {
95
        if (!isSupportedScript(element, attrs)) {
1✔
96
            super.startElement(element, attrs, augs);
1✔
97
        } else {
98
            _activeScriptBlock = new StringBuilder();
1✔
99
            _scriptLanguage = getScriptLanguage(attrs);
1✔
100
            String srcAttribute = attrs.getValue("src");
1✔
101
            if (srcAttribute != null) {
1✔
102
                _activeScriptBlock.append(_scriptHandler.getIncludedScript(srcAttribute));
1✔
103
            }
104
        }
105
    }
1✔
106

107
    /**
108
     * Checks if is supported script.
109
     *
110
     * @param element
111
     *            the element
112
     * @param attrs
113
     *            the attrs
114
     *
115
     * @return true, if is supported script
116
     */
117
    private boolean isSupportedScript(QName element, XMLAttributes attrs) {
118
        if (!element.rawname.equalsIgnoreCase("script") || attrs == null) {
1!
119
            return false;
1✔
120
        }
121
        String value = getScriptLanguage(attrs);
1✔
122
        return HttpUnitOptions.isScriptingEnabled() && _scriptHandler.supportsScriptLanguage(value);
1✔
123
    }
124

125
    /**
126
     * Gets the script language.
127
     *
128
     * @param attrs
129
     *            the attrs
130
     *
131
     * @return the script language
132
     */
133
    private String getScriptLanguage(XMLAttributes attrs) {
134
        return attrs == null ? null : attrs.getValue("language");
1!
135
    }
136

137
    @Override
138
    public void emptyElement(QName element, XMLAttributes attrs, Augmentations augs) throws XNIException {
139
        if (!isSupportedScript(element, attrs)) {
1!
140
            super.emptyElement(element, attrs, augs);
1✔
141
        }
142
    }
1✔
143

144
    @Override
145
    public void characters(XMLString text, Augmentations augs) throws XNIException {
146
        if (_activeScriptBlock != null) {
1✔
147
            _activeScriptBlock.append(text.ch, text.offset, text.length);
1✔
148
        } else {
149
            super.characters(text, augs);
1✔
150
        }
151
    }
1✔
152

153
    @Override
154
    public void endElement(QName element, Augmentations augs) throws XNIException {
155
        if (_activeScriptBlock == null) {
1✔
156
            super.endElement(element, augs);
1✔
157
        } else {
158
            try {
159
                final String scriptText = _activeScriptBlock.toString();
1✔
160
                String replacementText = getTranslatedScript(_scriptLanguage, scriptText);
1✔
161
                _configuration.pushInputSource(newInputSource(replacementText));
1✔
162
            } catch (IOException e) { // ignore
×
163
            } finally {
164
                _activeScriptBlock = null;
1✔
165
            }
166
        }
167
    }
1✔
168

169
    /**
170
     * New input source.
171
     *
172
     * @param replacementText
173
     *            the replacement text
174
     *
175
     * @return the XML input source
176
     */
177
    private XMLInputSource newInputSource(String replacementText) {
178
        StringBuilder systemID = new StringBuilder(_systemID);
1✔
179
        systemID.append("script").append(++_scriptIndex);
1✔
180

181
        return new XMLInputSource(null, systemID.toString(), null, new StringReader(replacementText), "UTF-8");
1✔
182
    }
183

184
    /**
185
     * Gets the translated script.
186
     *
187
     * @param language
188
     *            the language
189
     * @param scriptText
190
     *            the script text
191
     *
192
     * @return the translated script
193
     *
194
     * @throws IOException
195
     *             Signals that an I/O exception has occurred.
196
     */
197
    protected String getTranslatedScript(final String language, final String scriptText) throws IOException {
198
        return _scriptHandler.runScript(language, scriptText);
1✔
199
    }
200

201
}
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