Skip to content

Commit

Permalink
Eliminate the double call when there are children but no attributes (g…
Browse files Browse the repository at this point in the history
  • Loading branch information
msullivan committed Jun 24, 2019
1 parent 7d779f0 commit 96e9fc6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
14 changes: 10 additions & 4 deletions pyxl/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,18 @@ class x_base(object, metaclass=x_base_metaclass):
'onunload': str,
}

def __init__(self, **kwargs):
def __init__(self, *args, **kwargs):
self.__attributes__ = {}
self.__children__ = []

for name, value in kwargs.items():
self.set_attr(x_base._fix_attribute_name(name), value)
if args and kwargs:
raise PyxlException('Element constructor can take children or attributes, not both')

if args:
self.append_children(args)
else:
for name, value in kwargs.items():
self.set_attr(x_base._fix_attribute_name(name), value)

def __call__(self, *children):
self.append_children(children)
Expand All @@ -103,7 +109,7 @@ def children(self, selector=None, exclude=False):

# filter by class
if selector[0] == '.':
select = lambda x: selector[1:] in x.get_class()
select = lambda x: selector[1:] in x.get_class()

# filter by id
elif selector[0] == '#':
Expand Down
13 changes: 9 additions & 4 deletions pyxl/codec/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def handle_starttag(self, tag, attrs, call=True):
if 'cond' not in attrs:
raise ParseError("if tag must contain the 'cond' attr", self.end)

self.output.append('html.x_frag()(')
self.output.append('html.x_frag(')
self.last_thing_was_python = False
self.last_thing_was_close_if_tag = False
return
Expand All @@ -221,7 +221,7 @@ def handle_starttag(self, tag, attrs, call=True):
raise ParseError("<else> tag must come right after </if>", self.end)

self.delete_last_comma()
self.output.append('else html.x_frag()(')
self.output.append('else html.x_frag(')
self.last_thing_was_python = False
self.last_thing_was_close_if_tag = False
return
Expand All @@ -234,7 +234,10 @@ def handle_starttag(self, tag, attrs, call=True):

if hasattr(html, x_tag):
self.output.append('html.')
self.output.append('%s(' % x_tag)
self.output.append(x_tag)

if attrs or not call:
self.output.append('(')

first_attr = True
for attr_name, attr_value in attrs.items():
Expand All @@ -245,7 +248,9 @@ def handle_starttag(self, tag, attrs, call=True):
self.output.append('=')
self._handle_attr_value(attr_value)

self.output.append(')')
if attrs or not call:
self.output.append(')')

if call:
# start call to __call__
self.output.append('(')
Expand Down

0 comments on commit 96e9fc6

Please sign in to comment.