Skip to content

Commit

Permalink
fix(attributes): fixed outer quotes being stripped from attribute values
Browse files Browse the repository at this point in the history
closes #471
  • Loading branch information
christopherpickering committed Dec 15, 2022
1 parent ddb3da0 commit dff02dc
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -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)

Expand Down
18 changes: 15 additions & 3 deletions src/djlint/formatter/attributes.py
Expand Up @@ -128,15 +128,28 @@ 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
):
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
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/djlint/helpers.py
Expand Up @@ -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(
Expand Down
15 changes: 15 additions & 0 deletions tests/test_html/test_attributes.py
Expand Up @@ -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 = (
Expand Down
7 changes: 5 additions & 2 deletions tests/test_html/test_tag_textarea.py
Expand Up @@ -69,8 +69,11 @@ def test_textarea_tag(runner: CliRunner, tmp_file: TextIO) -> None:
)

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
Expand Down

0 comments on commit dff02dc

Please sign in to comment.