Skip to content

Commit

Permalink
Fix a few edge cases with attribute parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
motet-a committed Nov 5, 2018
1 parent 402130a commit c60bea7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
2 changes: 1 addition & 1 deletion jinjalint/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class Integer(Node):
has_percent = attr.ib() # bool

def __str__(self):
return self.value + ('%' if self.has_percent else '')
return str(self.value) + ('%' if self.has_percent else '')


@attr.s(frozen=True)
Expand Down
42 changes: 22 additions & 20 deletions jinjalint/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,11 +332,13 @@ def make_attribute_parser(jinja):
return (
locate(
P.seq(
interpolated(tag_name).skip(whitespace),
P.seq(
P.string('=').skip(whitespace).tag('equal'),
interpolated(attribute_value).tag('value'),
).map(dict).optional(),
interpolated(tag_name),
whitespace.then(
P.seq(
P.string('=').skip(whitespace).tag('equal'),
interpolated(attribute_value).tag('value'),
).map(dict)
).optional(),
)
)
.combine(_combine_attribute)
Expand All @@ -347,21 +349,25 @@ def make_attribute_parser(jinja):
def make_attributes_parser(config, jinja):
attribute = make_attribute_parser(jinja)

@P.generate
def attrs():
r = yield attrs_
return r

jinja_attrs = make_jinja_parser(
jinja_attr = make_jinja_parser(
config,
whitespace.then(attrs.skip(whitespace)),
interpolated(
whitespace
.then(
(attribute | jinja).sep_by(whitespace)
)
.skip(whitespace)
)
)

attrs_ = interpolated(
(jinja_attrs | attribute).sep_by(whitespace)
attrs = interpolated(
(
whitespace.then(jinja_attr) |
mandatory_whitespace.then(attribute)
).many()
)

return attrs_
return attrs


def _combine_comment(locations, text):
Expand Down Expand Up @@ -412,10 +418,6 @@ def make_opening_tag_parser(config,
tag_name_parser=None,
allow_slash=False):
attributes = make_attributes_parser(config, jinja)
whitespace_attributes = (
mandatory_whitespace.then(attributes).skip(whitespace) |
interpolated(P.string('').result([]))
)

if not tag_name_parser:
tag_name_parser = tag_name | jinja
Expand All @@ -436,7 +438,7 @@ def make_opening_tag_parser(config,
locate(P.seq(
P.string('<'),
tag_name_parser,
whitespace_attributes,
attributes.skip(whitespace),
slash,
P.string('>'),
))
Expand Down
14 changes: 14 additions & 0 deletions jinjalint/parse_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,20 @@ def test_element():
src = '<{% if a %}bcd{% endif %}></{% if a %}bcd{% endif %}>'
assert src == str(element.parse(src))

src = '<div{{ "ider" }}></div>'
assert '<div {{ "ider" }}></div>' == str(element.parse(src))

src = '<div{% if a %}foo="bar"{% endif %}></div>'
assert '<div {% if a %}foo="bar"{% endif %}></div>' == \
str(element.parse(src))

src = '<div{% if a %} foo="bar" a=2 {% endif %}></div>'
assert '<div {% if a %}foo="bar"a=2{% endif %}></div>' == \
str(element.parse(src))

src = '<colgroup></colgroup>'
assert src == str(element.parse(src))


def test_self_closing_elements():
assert element.parse('<br>') == Element(
Expand Down

0 comments on commit c60bea7

Please sign in to comment.