fix(attributes): fixed outer quotes being stripped from attribute values

closes #471
This commit is contained in:
Christopher Pickering 2022-12-15 10:46:39 -06:00
parent ddb3da04b5
commit dff02dcdc3
No known key found for this signature in database
GPG key ID: E14DB3B0A0FACF84
5 changed files with 37 additions and 6 deletions

View file

@ -100,7 +100,7 @@ Looks like this:
## 🛠️ Can I help?
Yes!
Yes!
*Would you like to add a rule to the linter?* Take a look at the [linter docs](https://djlint.com/docs/linter/) and [source code](https://github.com/Riverside-Healthcare/djLint/blob/master/src/djlint/rules.yaml)

View file

@ -128,7 +128,6 @@ def format_attributes(config: Config, html: str, match: re.match) -> str:
attributes = []
print(match, match.group(3))
# format attributes as groups
for attr_grp in re.finditer(
config.attribute_pattern, match.group(3).strip(), re.VERBOSE
@ -136,7 +135,21 @@ def format_attributes(config: Config, html: str, match: re.match) -> str:
attrib_name = attr_grp.group(1)
is_quoted = attr_grp.group(2) and attr_grp.group(2)[0] in ["'", '"']
quote = attr_grp.group(2)[0] if is_quoted else '"'
attrib_value = attr_grp.group(2).strip("\"'") if attr_grp.group(2) else None
attrib_value = None
if attr_grp.group(2) and attr_grp.group(2)[0] == attr_grp.group(2)[-1]:
if attr_grp.group(2)[0] == "'":
attrib_value = attr_grp.group(2).strip("'")
elif attr_grp.group(2)[0] == '"':
attrib_value = attr_grp.group(2).strip('"')
else:
attrib_value = attr_grp.group(2)
else:
attrib_value = attr_grp.group(2)
standalone = attr_grp.group(3)
quote_length = 1
@ -184,7 +197,6 @@ def format_attributes(config: Config, html: str, match: re.match) -> str:
attributes.append(
(attrib_name or "") + (attrib_value or "") + (standalone or "")
)
attribute_string = ("\n" + spacing).join([x for x in attributes if x])
close = match.group(4)

View file

@ -122,6 +122,7 @@ def inside_ignored_block(config: Config, html: str, match: re.Match) -> bool:
)
)
def child_of_ignored_block(config: Config, html: str, match: re.Match) -> bool:
"""Do not add whitespace if the tag is in a non indent block."""
return any(

View file

@ -215,6 +215,21 @@ def test_boolean_attributes(runner: CliRunner, tmp_file: TextIO) -> None:
assert output.exit_code == 0
def test_attribute_quotes(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file,
runner,
b"""<button id="test"
name="test"
type="submit"
one="isTrue ? 'True' : 'False'"
two="'Test' ."
three="'Test'"></button>""",
)
assert output.exit_code == 0
# def test_attributes(runner: CliRunner, tmp_file: TextIO) -> None:
# html_in = (

View file

@ -69,8 +69,11 @@ asdf
)
output = reformat(
tmp_file,runner,b"""<div><textarea type="textarea" id="messageContent" name="adContent" maxlength="300" class="form-control class_two" rows="10">{{ adContent|default }}</textarea></div>
""")
tmp_file,
runner,
b"""<div><textarea type="textarea" id="messageContent" name="adContent" maxlength="300" class="form-control class_two" rows="10">{{ adContent|default }}</textarea></div>
""",
)
assert (
output.text