Skip to content

Commit 788e4ae

Browse files
committed
Backport more changes from wabac.js
1 parent b07551c commit 788e4ae

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/zimscraperlib/rewriting/js.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,15 @@ def create_js_rules() -> list[TransformationRule]:
154154
return [
155155
# rewriting `eval(...)` - invocation
156156
(re.compile(r"(?:^|\s)\beval\s*\("), replace_prefix_from(eval_str, "eval")),
157+
(re.compile(r"\([\w]+,\s*eval\)\("), m2str(lambda _: f" {eval_str}")),
157158
# rewriting `x = eval` - no invocation
158159
(re.compile(r"[=]\s*\beval\b(?![(:.$])"), replace("eval", "self.eval")),
160+
(re.compile(r"var\s+self"), replace("var", "let")),
159161
# rewriting `.postMessage` -> `__WB_pmw(self).postMessage`
160162
(re.compile(r"\.postMessage\b\("), add_prefix(".__WB_pmw(self)")),
161163
# rewriting `location = ` to custom expression `(...).href =` assignement
162164
(
163-
re.compile(r"[^$.]?\s?\blocation\b\s*[=]\s*(?![\s\d=])"),
165+
re.compile(r"(?:^|[^$.+*/%^-])\s?\blocation\b\s*[=]\s*(?![\s\d=])"),
164166
add_suffix_non_prop(check_loc),
165167
),
166168
# rewriting `return this`

tests/rewriting/test_js_rewriting.py

+43
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,40 @@ def test_js_rewrite_post_message(simple_js_rewriter: JsRewriter):
101101
)
102102

103103

104+
@pytest.mark.parametrize(
105+
"raw_js,expected",
106+
[
107+
pytest.param("x = eval; x(a);", "x = self.eval; x(a);", id="case1"),
108+
pytest.param(
109+
" eval(a)",
110+
" WB_wombat_runEval2((_______eval_arg, isGlobal) => { var ge = eval; "
111+
"return isGlobal ? ge(_______eval_arg) : eval(_______eval_arg); })"
112+
".eval(this, (function() { return arguments })(),a)",
113+
id="case2",
114+
),
115+
pytest.param(
116+
"$eval = eval; $eval(a);", "$eval = self.eval; $eval(a);", id="case3"
117+
),
118+
pytest.param(
119+
"foo(a, eval(data));",
120+
"foo(a, WB_wombat_runEval2((_______eval_arg, isGlobal) => { var ge = eval; "
121+
"return isGlobal ? ge(_______eval_arg) : eval(_______eval_arg); })"
122+
".eval(this, (function() { return arguments })(),data));",
123+
id="case4",
124+
),
125+
pytest.param(
126+
"return(1, eval)(data);",
127+
"return WB_wombat_runEval2((_______eval_arg, isGlobal) => { var ge = eval; "
128+
"return isGlobal ? ge(_______eval_arg) : eval(_______eval_arg); })"
129+
".eval(this, (function() { return arguments })(),data);",
130+
id="case5",
131+
),
132+
],
133+
)
134+
def test_js_rewrite_evals(simple_js_rewriter: JsRewriter, raw_js: str, expected: str):
135+
assert simple_js_rewriter.rewrite(raw_js) == expected
136+
137+
104138
class WrappedTestContent(ContentForTests):
105139

106140
def __init__(
@@ -173,11 +207,20 @@ def wrap_script(text: str) -> str:
173207
input_="this. location = 'http://example.com/'",
174208
expected="this. location = 'http://example.com/'",
175209
),
210+
WrappedTestContent(
211+
input_="abc-location = http://example.com/",
212+
expected="abc-location = http://example.com/",
213+
),
214+
WrappedTestContent(
215+
input_="func(location = 0)",
216+
expected="func(location = 0)",
217+
),
176218
WrappedTestContent(
177219
input_="if (self.foo) { console.log('blah') }",
178220
expected="if (self.foo) { console.log('blah') }",
179221
),
180222
WrappedTestContent(input_="window.x = 5", expected="window.x = 5"),
223+
WrappedTestContent(input_=" var self ", expected=" let self "),
181224
]
182225
)
183226
def rewrite_wrapped_content(request: pytest.FixtureRequest):

0 commit comments

Comments
 (0)