Skip to content

Commit

Permalink
fix(formatter): fixed greedy regex issue when finding script and styl…
Browse files Browse the repository at this point in the history
…e tags
  • Loading branch information
Christopher Pickering committed Jul 28, 2022
1 parent 99b5ee2 commit ca7ff3a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
4 changes: 4 additions & 0 deletions requirements.txt
Expand Up @@ -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")
Expand All @@ -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"
Expand Down
6 changes: 5 additions & 1 deletion src/djlint/formatter/css.py
Expand Up @@ -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
Expand All @@ -30,7 +34,7 @@ def launch_formatter(config: Config, match: re.Match) -> str:

return re.sub(
re.compile(
r"([ ]*?)(<style\b.*?>)(.+?)(?=</style>)",
r"([ ]*?)(<(?:style)\b(?:\"[^\"]*\"|'[^']*'|{[^}]*}|[^'\">{}])*>)(.*?)(?=</style>)",
re.IGNORECASE | re.DOTALL,
),
func,
Expand Down
5 changes: 4 additions & 1 deletion src/djlint/formatter/js.py
Expand Up @@ -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)
Expand All @@ -31,7 +34,7 @@ def launch_formatter(config: Config, match: re.Match) -> str:

return re.sub(
re.compile(
r"([ ]*?)(<script\b.*?>)(.+?)(?=</script>)",
r"([ ]*?)(<(?:script)\b(?:\"[^\"]*\"|'[^']*'|{[^}]*}|[^'\">{}])*>)(.*?)(?=</script>)",
re.IGNORECASE | re.DOTALL,
),
func,
Expand Down
4 changes: 4 additions & 0 deletions tests/test_config/test_scripts_styles/html.html
Expand Up @@ -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>

0 comments on commit ca7ff3a

Please sign in to comment.