Skip to content

Commit de13384

Browse files
committed
initial htmx 2.0.7 support
1 parent 3773067 commit de13384

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright (c) 2002-2025 Gargoyle Software Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* https://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
package org.htmlunit.javascript.preprocessor;
16+
17+
import org.apache.commons.lang3.StringUtils;
18+
import org.htmlunit.ScriptPreProcessor;
19+
import org.htmlunit.html.HtmlElement;
20+
import org.htmlunit.html.HtmlPage;
21+
22+
/**
23+
* PreProzessor to fix one default parameter method.
24+
*
25+
* @author Ronald Brill
26+
*/
27+
public class HtmxTwoZeroSevenScriptPreProcessor implements ScriptPreProcessor {
28+
29+
private final ScriptPreProcessor nextScriptPreProcessor_;
30+
31+
/**
32+
* Ctor.
33+
*/
34+
public HtmxTwoZeroSevenScriptPreProcessor() {
35+
nextScriptPreProcessor_ = null;
36+
}
37+
38+
/**
39+
* Ctor.
40+
* @param nextScriptPreProcessor the next {@link ScriptPreProcessor}
41+
*/
42+
public HtmxTwoZeroSevenScriptPreProcessor(final ScriptPreProcessor nextScriptPreProcessor) {
43+
nextScriptPreProcessor_ = nextScriptPreProcessor;
44+
}
45+
46+
/**
47+
* {@inheritDoc}
48+
*/
49+
@Override
50+
public String preProcess(final HtmlPage htmlPage, final String sourceCode, final String sourceName,
51+
final int lineNumber, final HtmlElement htmlElement) {
52+
53+
String patchedSourceCode = sourceCode;
54+
55+
if (sourceName.contains("/htmx.js") && !sourceName.contains("/htmx.js#")) {
56+
patchedSourceCode = StringUtils.replace(
57+
sourceCode,
58+
"result.push(...toArray(rootNode.querySelectorAll(standardSelector)))",
59+
"result.push.apply(result, toArray(rootNode.querySelectorAll(standardSelector)))");
60+
61+
patchedSourceCode = StringUtils.replace(
62+
patchedSourceCode,
63+
"result.push(...findAttributeTargets(eltToInheritFrom, attrName))",
64+
"result.push.apply(result, findAttributeTargets(eltToInheritFrom, attrName))");
65+
66+
patchedSourceCode = StringUtils.replace(
67+
patchedSourceCode,
68+
"for (const preservedElt of [...pantry.children]) {",
69+
"for (const preservedElt of Array.from(pantry.children)) {");
70+
}
71+
else if (sourceName.contains("/htmx.min.js") && !sourceName.contains("/htmx.min.js#")) {
72+
patchedSourceCode = StringUtils.replace(
73+
sourceCode,
74+
"i.push(...F(c.querySelectorAll(e)))",
75+
"i.push.apply(i,F(c.querySelectorAll(e)))");
76+
77+
patchedSourceCode = StringUtils.replace(
78+
patchedSourceCode,
79+
"r.push(...we(i,n))",
80+
"r.push.apply(r,we(i,n))");
81+
82+
patchedSourceCode = StringUtils.replace(
83+
patchedSourceCode,
84+
"for(const t of[...e.children]){",
85+
"for(const t of Array.from(e.children)){");
86+
}
87+
88+
if (nextScriptPreProcessor_ != null) {
89+
return nextScriptPreProcessor_.preProcess(htmlPage, patchedSourceCode, sourceName, lineNumber, htmlElement);
90+
}
91+
92+
return patchedSourceCode;
93+
}
94+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (c) 2002-2025 Gargoyle Software Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* https://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
package org.htmlunit.libraries;
16+
17+
import org.htmlunit.WebClient;
18+
import org.htmlunit.javascript.preprocessor.HtmxTwoZeroSevenScriptPreProcessor;
19+
import org.htmlunit.junit.annotation.Alerts;
20+
import org.htmlunit.junit.annotation.HtmlUnitNYI;
21+
import org.junit.jupiter.api.Test;
22+
23+
/**
24+
* Tests for <a href="https://htmx.org/">htmx</a>.
25+
*
26+
* @author Ronald Brill
27+
*/
28+
public class HtmxTest2x0x7 extends HtmxTest {
29+
30+
/**
31+
* @throws Exception if an error occurs
32+
*/
33+
@Test
34+
@Alerts(DEFAULT = "passes:814failures:1",
35+
FF = "passes:810failures:1",
36+
FF_ESR = "passes:809failures:2")
37+
@HtmlUnitNYI(
38+
CHROME = "passes:822failures:0",
39+
EDGE = "passes:822failures:0")
40+
public void htmx() throws Exception {
41+
htmx("htmx-2.0.7");
42+
}
43+
44+
@Override
45+
protected void setupWebClient(final WebClient webClient) {
46+
super.setupWebClient(webClient);
47+
48+
webClient.setScriptPreProcessor(new HtmxTwoZeroSevenScriptPreProcessor());
49+
webClient.getOptions().setThrowExceptionOnScriptError(false);
50+
}
51+
}

src/test/resources/libraries/htmx/htmx-2.0.7/src/htmx.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)