File size: 1,057 Bytes
8e45983
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from pathlib import Path

path = Path("app/product/final_product_ui.py")
text = path.read_text(encoding="utf-8-sig")
text = text.replace("\ufeff", "")

# These are inside a Python triple-quoted HTML string.
# So JS regex \n must be written as \\n in the Python source,
# otherwise Python turns it into a real newline and breaks browser JS.

replacements = {
    r"[^|\n]*page": r"[^|\\n]*page",
    r"/\n{3,}/g": r"/\\n{3,}/g",
    r'"\n\n"': r'"\\n\\n"',
    r"split(/\n+/)": r"split(/\\n+/)",
    r"[ \t]+": r"[ \\t]+",
}

for old, new in replacements.items():
    if old in text:
        text = text.replace(old, new)

# Safety: if regex line is still risky, replace that exact line with a RegExp constructor.
bad_line = r'text = text.replace(/\s+\|\s*[^|\n]*page\s*\d+/gi, "");'
safe_line = r'text = text.replace(new RegExp("\\s+\\|\\s*[^|\\n]*page\\s*\\d+", "gi"), "");'

if bad_line in text:
    text = text.replace(bad_line, safe_line)

path.write_text(text, encoding="utf-8")
print("Fixed broken JavaScript regex escapes in final_product_ui.py")