From b3b2903c751e2dbff1e3dc70f08b8419e2180f1c Mon Sep 17 00:00:00 2001 From: Uwe Schuster Date: Sat, 25 Apr 2026 21:38:19 +0200 Subject: [PATCH] =?UTF-8?q?#3:=20inject-hashed-filenames.py=20=E2=80=94=20?= =?UTF-8?q?tag-aware=20HTML=20rewrite?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace substring `html.replace(old_src, new_src)` with a regex anchored to ' + out, n = ihf.rewrite(html, "/static/dist/app.js", "/static/dist/app.abc123.js") + assert n == 1 + assert out == '' + + +def test_rewrites_script_src_single_quoted(): + html = "" + out, n = ihf.rewrite(html, "/static/dist/app.js", "/static/dist/app.abc123.js") + assert n == 1 + assert "/static/dist/app.abc123.js" in out + + +def test_rewrites_link_href(): + html = '' + out, n = ihf.rewrite(html, "/static/dist/app.css", "/static/dist/app.abc123.css") + assert n == 1 + assert '/static/dist/app.abc123.css' in out + + +def test_does_not_rewrite_inside_html_comment(): + html = '' + out, n = ihf.rewrite(html, "/static/dist/app.js", "/static/dist/app.abc123.js") + assert n == 0 + assert "/static/dist/app.js" in out + assert "/static/dist/app.abc123.js" not in out + + +def test_does_not_rewrite_inside_json_literal(): + html = '
{ "src": "/static/dist/app.js" }
' + out, n = ihf.rewrite(html, "/static/dist/app.js", "/static/dist/app.abc123.js") + assert n == 0 + assert out == html + + +def test_does_not_rewrite_unrelated_attribute(): + html = '' + out, n = ihf.rewrite(html, "/static/dist/app.js", "/static/dist/app.abc123.js") + assert n == 0 + assert out == html + + +def test_does_not_rewrite_anchor_href(): + # Even though is a `href` attribute, it isn't a . + html = 'debug link' + out, n = ihf.rewrite(html, "/static/dist/app.js", "/static/dist/app.abc123.js") + assert n == 0 + assert out == html + + +def test_rewrites_with_extra_attributes_around_src(): + html = '' + out, n = ihf.rewrite(html, "/static/dist/app.js", "/static/dist/app.abc123.js") + assert n == 1 + assert '/static/dist/app.abc123.js' in out + assert 'type="module"' in out and 'defer' in out