fix(formatter): fixed greedy regex issue when finding script and style tags

This commit is contained in:
Christopher Pickering 2022-07-28 14:24:47 -05:00
parent 99b5ee2c1f
commit ca7ff3a1c7
No known key found for this signature in database
GPG key ID: E14DB3B0A0FACF84
4 changed files with 17 additions and 2 deletions

View file

@ -4,12 +4,15 @@ black==22.1.0; python_full_version >= "3.6.2"
click==8.0.3; python_version >= "3.6"
colorama==0.4.4; (python_version >= "2.7" and python_full_version < "3.0.0") or (python_full_version >= "3.5.0")
coverage==6.4.2; python_version >= "3.7"
cssbeautifier==1.14.4
editorconfig==0.12.3
execnet==1.9.0; python_version >= "3.6" and python_full_version < "3.0.0" or python_full_version >= "3.5.0" and python_version >= "3.6"
html-tag-names==0.1.2; python_version >= "3.7" and python_version < "4.0"
html-void-elements==0.1.0; python_version >= "3.7" and python_version < "4.0"
importlib-metadata==4.11.0; python_version >= "3.7"
iniconfig==1.1.1; python_version >= "3.7"
isort==5.10.1; python_full_version >= "3.6.1" and python_version < "4.0"
jsbeautifier==1.14.4
mypy-extensions==0.4.3; python_full_version >= "3.6.2"
packaging==21.3; python_version >= "3.7"
pathspec==0.9.0; (python_version >= "2.7" and python_full_version < "3.0.0") or (python_full_version >= "3.5.0")
@ -23,6 +26,7 @@ pytest-xdist==2.5.0; python_version >= "3.6"
pytest==7.1.2; python_version >= "3.7"
pyyaml==6.0; python_version >= "3.6"
regex==2022.1.18
six==1.16.0; python_version >= "2.7" and python_full_version < "3.0.0" or python_full_version >= "3.3.0"
tomli==2.0.1; python_full_version <= "3.11.0a6" and python_version >= "3.7" and python_full_version >= "3.6.2" or python_version < "3.11"
tqdm==4.62.3; (python_version >= "2.7" and python_full_version < "3.0.0") or (python_full_version >= "3.4.0")
typed-ast==1.4.3; python_version < "3.8" and implementation_name == "cpython" and python_full_version >= "3.6.2"

View file

@ -13,9 +13,13 @@ def format_css(html: str, config: Config) -> str:
def launch_formatter(config: Config, match: re.Match) -> str:
"""Add break after if not in ignored block."""
if not match.group(3).strip():
return match.group()
indent = len(match.group(1)) * " "
inner_indent = indent + config.indent
opts = BeautifierOptions(config.css_config)
beautified = (
"\n"
+ inner_indent
@ -30,7 +34,7 @@ def format_css(html: str, config: Config) -> str:
return re.sub(
re.compile(
r"([ ]*?)(<style\b.*?>)(.+?)(?=</style>)",
r"([ ]*?)(<(?:style)\b(?:\"[^\"]*\"|'[^']*'|{[^}]*}|[^'\">{}])*>)(.*?)(?=</style>)",
re.IGNORECASE | re.DOTALL,
),
func,

View file

@ -13,6 +13,9 @@ def format_js(html: str, config: Config) -> str:
def launch_formatter(config: Config, match: re.Match) -> str:
"""Add break after if not in ignored block."""
if not match.group(3).strip():
return match.group()
indent = len(match.group(1)) * " "
inner_indent = indent + config.indent
opts = BeautifierOptions(config.js_config)
@ -31,7 +34,7 @@ def format_js(html: str, config: Config) -> str:
return re.sub(
re.compile(
r"([ ]*?)(<script\b.*?>)(.+?)(?=</script>)",
r"([ ]*?)(<(?:script)\b(?:\"[^\"]*\"|'[^']*'|{[^}]*}|[^'\">{}])*>)(.*?)(?=</script>)",
re.IGNORECASE | re.DOTALL,
),
func,

View file

@ -15,3 +15,7 @@
width: 12px
}
</style>
<script src="{% static '/js/shared.min.js' %}"></script>
<script src="{% static '/js/alive.min.js' %}"></script>
<script src="{% static '/js/search.min.js' %}" defer async></script>
<script src="{% static '/js/utility.min.js' %}" defer async></script>