Skip to content
This repository has been archived by the owner on Aug 17, 2023. It is now read-only.

Commit

Permalink
Node parsing should treat native django tags as plain text - i.e. don…
Browse files Browse the repository at this point in the history
…'t try to parse %} as element
  • Loading branch information
rowanseymour committed Dec 13, 2016
1 parent b83c677 commit 90894b1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
6 changes: 2 additions & 4 deletions hamlpy/parser/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,8 @@ def read_node(stream, prev, compiler):
if stream.text[stream.ptr] == FILTER_PREFIX:
return read_filter_node(stream, indent, compiler)

# peek ahead to differentiate between variable node starting #{.. and element node starting #\w...
is_variable = stream.ptr < stream.length - 1 and stream.text[stream.ptr:stream.ptr+2] == '#{'

if stream.text[stream.ptr] in ELEMENT_PREFIXES and not is_variable:
# peek ahead to so we don't try to parse an element from a variable node starting #{ or a Django tag ending %}
if stream.text[stream.ptr] in ELEMENT_PREFIXES and stream.text[stream.ptr:stream.ptr+2] not in ('#{', '%}'):
element = read_element(stream)
return ElementNode(element, indent, compiler)

Expand Down
7 changes: 7 additions & 0 deletions hamlpy/test/test_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ def test_tags(self):
# tags can have xml namespaces
self._test("%fb:tag\n content", "<fb:tag>\n content\n</fb:tag>")

# tags can have dashes
self._test("%ng-tag\n content", "<ng-tag>\n content\n</ng-tag>")

def test_ids_and_classes(self):
# id on tag
self._test('%div#someId Some text', "<div id='someId'>Some text</div>")
Expand Down Expand Up @@ -129,6 +132,10 @@ def test_plain_text(self):
self._test("This should be plain text\n This should be indented",
"This should be plain text\n This should be indented")

# native Django tags {% %} should be treated as plain text
self._test("text {%\n trans ''\n%}", "text {%\n trans ''\n%}")
self._test("text\n {%\n trans ''\n%}", "text\n {%\n trans ''\n%}")

def test_plain_filter(self):
# with indentation
self._test(":plain\n -This should be plain text\n .This should be more\n This should be indented",
Expand Down
3 changes: 3 additions & 0 deletions hamlpy/test/test_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ def test_grabs_inline_tag_content(self):
element = read_element(Stream("%div Some Text"))
assert element.inline_content == 'Some Text'

element = read_element(Stream("%div {% trans 'hello' %}"))
assert element.inline_content == "{% trans 'hello' %}"

def test_multiline_attributes(self):
element = read_element(Stream("""%link{'rel': 'stylesheet', 'type': 'text/css',
'href': '/long/url/to/stylesheet/resource.css'}"""))
Expand Down

0 comments on commit 90894b1

Please sign in to comment.