def test_it_compiles_regular_attr_interpolation(): assert c('<div class="x $foo"></div>') == [ im.TextNode(pos=P(1, 1), content='<div class="x '), im.InterpolateNode(pos=P(1, 15), value='foo'), im.TextNode(pos=P(1, 19), content='"></div>') ]
def test_it_compiles_choose(): assert c('<py:choose test="foo">' '<py:when test="1">one</py:when>' '<py:when test="2">two</py:when>' '<p>some text</p>' '<py:otherwise>zzz</py:otherwise>' '</py:choose>') == [ im.ChooseNode(pos=P(1, 1), test='foo', children=[ im.WhenNode(pos=P(1, 23), test='1', children=[ im.TextNode(pos=P(1, 41), content='one') ]), im.WhenNode(pos=P(1, 54), test='2', children=[ im.TextNode(pos=P(1, 72), content='two') ]), im.TextNode(pos=P(1, 85), content='<p>some text</p>'), im.OtherwiseNode(pos=P(1, 101), children=[ im.TextNode( pos=P(1, 115), content='zzz') ]), ]) ]
def test_it_compiles_tag_with_inner_directive_attrs(): assert c('<div py:block="foo">bar</div>') == [ im.TextNode(pos=P(1, 1), content='<div>'), im.BlockNode(pos=P(1, 16), name='foo', children=[im.TextNode(pos=P(1, 21), content='bar')]), im.TextNode(content='</div>', pos=P(1, 24)), ]
def test_it_compiles_18n_message_from_comment_attr(self): n = toim('<div i18n:comment="bar">foo</div>') assert n == [ im.TextNode(pos=P(1, 1), content='<div>'), im.TranslationNode( pos=P(1, 20), comment='bar', children=[im.TextNode(pos=P(1, 25), content='foo')]), im.TextNode(pos=P(1, 28), content='</div>'), ] assert n[1].get_msgstr() == 'foo'
def test_it_compiles_if_else(): assert c('<py:if test="foo">xxx</py:if>' ' ' '<py:else>zzz</py:else>') == [ im.IfNode( pos=P(1, 1), test='foo', children=[im.TextNode(pos=P(1, 19), content='xxx')], else_=im.ElseNode( children=[im.TextNode(pos=P(1, 41), content='zzz')])) ]
def test_it_compiles_i18n_message(self): n = toim('<div i18n:message="" i18n:comment="translate me">foo</div>') assert n == [ im.TextNode(pos=P(1, 1), content='<div>'), im.TranslationNode( pos=P(1, 20), message='', comment='translate me', children=[im.TextNode(pos=P(1, 50), content='foo')]), im.TextNode(pos=P(1, 53), content='</div>'), ] assert n[1].get_msgstr() == 'foo'
def test_it_compiles_pystrip(): assert c('<div py:strip=""><p>foo</p></div>') == [ im.TextNode(pos=P(1, 18), content='<p>foo</p>'), ] assert c('<div py:strip="x"><p>foo</p></div>') == [ im.IfNode(pos=P(1, 1), test='not (x)', children=[im.TextNode(pos=P(1, 1), content='<div>')]), im.TextNode(pos=P(1, 19), content='<p>foo</p>'), im.IfNode(pos=P(1, 29), test='not (x)', children=[im.TextNode(pos=P(1, 29), content='</div>')]), ]
def test_it_compiles_tag_with_outer_directive_attrs(): assert c('<div py:if="foo">bar</div>') == [ im.IfNode(pos=P(1, 13), test='foo', children=[ im.TextNode(pos=P(1, 1), content='<div>bar</div>'), ]), ]
def test_it_compiles_block(): assert c('<py:block name="foo">bar</py:block>') == [ im.BlockNode(pos=P(1, 1), name='foo', children=[ im.TextNode(pos=P(1, 22), content='bar'), ]) ]
def test_it_compiles_conditional_attr_interpolation(): assert c('<input selected="$foo"/>') == [ im.TextNode(pos=P(1, 1), content='<input '), im.WithNode(pos=P(1, 8), vars=[('__piglet_tmp', 'foo')], children=[ im.IfNode(pos=P(1, 8), test='__piglet_tmp is not None', children=[ im.TextNode(pos=P(1, 8), content='selected="'), im.InterpolateNode(pos=P(1, 18), value='__piglet_tmp'), im.TextNode(pos=P(1, 22), content='"') ]) ]), im.TextNode(pos=P(1, 23), content='/>') ]
def _compile_close_tag(node, tagname_expr=None, strip_condition=None): if not node.close_tag or (isinstance(node, Fragment) and not tagname_expr): return im.TextNode('', pos=node.close_tag_pos) if tagname_expr: closetag = im.ContainerNode(children=[ im.TextNode.factory('</', pos=node.close_tag_pos), im.InterpolateNode(value=tagname_expr, pos=node.close_tag_pos), im.TextNode.factory('>', pos=node.close_tag_pos), ]) else: closetag = im.TextNode(node.close_tag, pos=node.close_tag_pos) if strip_condition: if strip_condition in {'True', '1'}: return im.NullNode(children=[closetag]) else: return im.IfNode(test='not ({})'.format(strip_condition), children=[closetag], pos=node.close_tag_pos) return closetag
def test_it_processes_strip_whitespace(): assert c(u'<p py:whitespace="strip"> <b> foo bar </b> </p>') == [ im.TextNode(pos=P(1, 1), content=u'<p><b>foo bar</b></p>'), ] assert c(u'<p py:whitespace="strip">one <b>and</b> two <b>!</b></p>') == [ im.TextNode(pos=P(1, 1), content=u'<p>one <b>and</b> two <b>!</b></p>'), ] assert c(u'<p py:whitespace="strip">\n<b> foo bar </b>\n</p>') == [ im.TextNode(pos=P(1, 1), content=u'<p><b>foo bar</b></p>'), ] assert c(u'<p py:whitespace="strip"> <b>\u00a0 foo bar </b> </p>') == [ im.TextNode(pos=P(1, 1), content=u'<p><b>\u00a0 foo bar</b></p>'), ] assert c(u'<p py:whitespace="strip"> ' u'<b py:whitespace="preserve"> foo </b></p>') == [ im.TextNode(pos=P(1, 1), content=u'<p><b> foo </b></p>'), ]
def test_it_compiles_pycall(): assert c('<py:call function="func(1)">' '<py:keyword name="x">foo</py:keyword>' '<py:keyword name="y">bar</py:keyword>' '</py:call>') == [ im.Call(pos=P(1, 1), function="func(1)", children=[ im.CallKeyword(pos=P(1, 29), name='x', children=[ im.TextNode(pos=P(1, 50), content='foo') ]), im.CallKeyword(pos=P(1, 66), name='y', children=[ im.TextNode(pos=P(1, 87), content='bar') ]) ]) ]
def _compile_text(node): """ Compile :class:`piglet.parse.Text` object ``node`` into intermediate Text and Interpolation nodes. :param node: the :class:`piglet.parse.Text` object """ items = interpolate.parse_interpolations(node.content) container = im.ContainerNode() pos = node.pos for i in items: if isinstance(i, (str, ustr)): container.append(im.TextNode(i, pos=pos)) pos = pos.advance(i) elif isinstance(i, interpolate.Interpolation): container.append( im.InterpolateNode.factory(value=unescape(i.value), pos=pos, autoescape=i.autoescape and (not node.cdata))) pos = pos.advance(i.source) return container
def test_it_processes_strip_whitespace_and_keeps_line_numbering(): assert c(u'<p py:whitespace="strip"> ' u'<py:if test="True"> hello! </py:if></p>') == [ im.TextNode(pos=P(1, 1), content='<p>'), im.IfNode( pos=P(1, 27), test='True', children=[im.TextNode(pos=P(1, 47), content='hello!')]), im.TextNode(pos=P(1, 62), content='</p>') ] assert c(u'<html><py:whitespace value="strip"> ' u'<p><py:if test="True"> hello! </py:if></p>' u'</py:whitespace></html>') == [ im.TextNode(pos=P(1, 1), content='<html><p>'), im.IfNode( pos=P(1, 40), test='True', children=[im.TextNode(pos=P(1, 60), content='hello!')]), im.TextNode(pos=P(1, 75), content='</p></html>') ]
def test_it_strips_whitespace_around_text(): assert c('<p py:whitespace="strip"> foo </p>') == [ im.TextNode(pos=P(1, 1), content='<p>foo</p>') ]
def test_it_compiles_inline_interpolation(): assert c('<div>$a</div>') == [ im.TextNode(pos=P(1, 1), content='<div>'), im.InterpolateNode(pos=P(1, 6), value='a'), im.TextNode(pos=P(1, 8), content='</div>') ]
def _compile_open_tag(node, tagname_expr=None, extra_attrs=None, strip_condition=None): """ Compile the open tag attributes. This needs special casing when interpolations are present, eg in the case: <option selected="1 if item == 'foo' else None"> The selected attribute should be entirely omitted if the expression evaluates to None. :param node: the parse.Element node to compile :param tagname_expr: a python expression to replace the tag name :param extra_attrs: A list of python source expressions, each returning a list of attributes :param strip_condition: a python boolean expression to decide whether to output the open tag at all """ if isinstance(node, Fragment) and not tagname_expr: return im.ContainerNode(pos=node.pos) container = wn = im.ContainerNode(pos=node.pos) if strip_condition: if strip_condition in {'True', '1'}: container.append(im.NullNode(pos=node.pos)) else: container.append( im.IfNode(test='not ({})'.format(strip_condition), children=[], pos=node.pos)) wn = container.tip() wn.append(im.TextNode.factory('<', pos=node.pos)) if tagname_expr: wn.append(im.InterpolateNode(value=tagname_expr, pos=node.end_pos)) else: wn.append(im.TextNode.factory(node.qname, pos=node.pos)) wn.append(im.TextNode.factory(node.space, pos=node.pos)) if extra_attrs: wn.append(im.InlineCodeNode(pysrc='__piglet_attrs = {}', pos=node.pos)) for item in extra_attrs: wn.append( im.InlineCodeNode( pysrc='__piglet_attrs.update({})'.format(item), pos=node.pos)) for a in node.attrs.values(): if node.attrs and extra_attrs: wn.append( im.IfNode(test="'{}' not in __piglet_attrs".format(a.name), pos=a.pos)) _compile_attr(a, wn.tip()) else: _compile_attr(a, wn) if extra_attrs: for_ = im.ForNode(each=('__piglet_attr_k, __piglet_attr_v ' 'in __piglet_attrs.items()'), pos=node.end_pos, children=[]) if_ = im.IfNode(test='__piglet_attr_v is not None', pos=node.end_pos, children=[ im.TextNode(' ', pos=node.end_pos), im.InterpolateNode(value='__piglet_attr_k', pos=node.end_pos), im.TextNode('="', pos=node.end_pos), im.InterpolateNode(value='__piglet_attr_v', pos=node.end_pos), im.TextNode('"', pos=node.end_pos) ]) for_.children.append(if_) wn.append(for_) wn.append(im.TextNode(content=node.end, pos=node.end_pos)) return container
def test_it_compiles_entity(): assert c('&') == [im.TextNode(pos=P(1, 1), content='&')]
def test_it_preserves_ordering_of_namespaced_attributes(): assert c('<html lang="en" xml:lang="en"></html>') == [ im.TextNode(pos=P(line=1, char=1), content='<html lang="en" xml:lang="en"></html>') ]
def test_it_strips_whitespace_between_elements(): assert c('<p py:whitespace="strip"> \n <a/>\n<b/> \n </p>') == [ im.TextNode(pos=P(1, 1), content='<p><a/><b/></p>') ]
def test_it_strips_whitespace_inside_tags(): assert c('<p py:whitespace="strip"> <a/> </p>') == [ im.TextNode(pos=P(1, 1), content='<p><a/></p>') ]
def test_it_compiles_content(): assert c('<div py:content="body">foo</div>') == [ im.TextNode(pos=P(1, 1), content='<div>'), im.InterpolateNode(pos=P(1, 18), value='body'), im.TextNode(pos=P(1, 27), content='</div>') ]
def test_it_compiles_element(): assert c('<a href="foo">bar</a>') == [ im.TextNode(pos=P(1, 1), content='<a href="foo">bar</a>'), ]