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

Allow class to come before before id #14

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions hamlpy/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,33 @@ def attr_wrap(self, value):
return '%s%s%s' % (self.attr_wrapper, value, self.attr_wrapper)

def _parse_haml(self):
split_tags = self.HAML_REGEX.search(self.haml).groupdict('')
split_tags = self._split_haml(self.haml)

attribute_parser = AttributeDictParser(split_tags.get('attributes'))
attribute_parser = AttributeDictParser(split_tags.get('attributes', ''))
self.attributes_dict = attribute_parser.parse()

self.tag = split_tags.get('tag').strip(self.ELEMENT) or 'div'
self.id = self._parse_id(split_tags.get('id'))
self.classes = self._parse_classes(split_tags.get('class'))
self.self_close = split_tags.get('selfclose') or self.tag in self.self_closing_tags
self.tag = split_tags.get('tag', '').strip(self.ELEMENT) or 'div'
self.id = self._parse_id(split_tags.get('id', ''))
self.classes = ('%s %s' % (split_tags.get('class', '').lstrip(self.CLASS).replace('.', ' '), self._parse_class_from_attributes_dict())).strip()
self.self_close = split_tags.get('selfclose', '') or self.tag in self.self_closing_tags

self.attributes = self._render_attributes(self.attributes_dict, self.id, self.classes)

self.nuke_inner_whitespace = split_tags.get('nuke_inner_whitespace') != ''
self.nuke_outer_whitespace = split_tags.get('nuke_outer_whitespace') != ''
self.django_variable = split_tags.get('django') != ''
self.inline_content = split_tags.get('inline').strip()
self.nuke_inner_whitespace = split_tags.get('nuke_inner_whitespace', '') != ''
self.nuke_outer_whitespace = split_tags.get('nuke_outer_whitespace', '') != ''
self.django_variable = split_tags.get('django', '') != ''
self.inline_content = split_tags.get('inline', '').strip()

def _split_haml(self, txt):
result = {}
def merge(data):
kwargs = {key: value for key, value in data.iteritems() if value}
if bool(kwargs):
result.update(kwargs)

groups = [m.groupdict() for m in self.HAML_REGEX.finditer(txt)]
map(merge, groups)
return result

def _parse_classes(self, classes):
tag_classes = classes.lstrip(self.CLASS).replace('.', ' ')
Expand Down
3 changes: 3 additions & 0 deletions hamlpy/test/test_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ def test_ids_and_classes(self):
# multiple classes
self._test('%div.someClass.anotherClass Some text', "<div class='someClass anotherClass'>Some text</div>")

# class can come before id
self._test('%div.someClass#someId', "<div id='someId' class='someClass'></div>")

def test_attribute_dictionaries(self):
# attribute dictionaries
self._test("%html{'xmlns':'http://www.w3.org/1999/xhtml', 'xml:lang':'en', 'lang':'en'}",
Expand Down