def assert_validate_attributes(self, attrs_dict, actions, expected_attrs_dict, issues=None): attrs = [] start = 3 for ident in sorted(attrs_dict): value = attrs_dict[ident] raw = '{}="{}"'.format(ident, value) attrs.append( HTMLAttribute(raw=raw, start=start, ident=ident, value=value)) start += len(raw) + 1 attrs_raw = ' '.join(attr.raw for attr in attrs) html_attrs = HTMLAttributes(raw=attrs_raw, start=3, attributes=attrs) if attrs_raw: tag_raw = '<a {}>'.format(attrs_raw) else: tag_raw = '<a>' tag = HTMLOpenTag(raw=tag_raw, tag='a', attributes=html_attrs, attribute_actions=actions) attributes = tag.attributes.as_dict() self.assertEqual(expected_attrs_dict, attributes) self.assertEqual(issues or [], tag.issues)
def test_str_attrs(self): attr_text = 'href="http://example.com" title="W3C Example"' attr1 = HTMLAttribute(raw='href="http://example.com"', ident='href', value='http://example.com') attr2 = HTMLAttribute(raw='title="W3C Example"', start=17, ident='title', value='W3C Example') attrs = HTMLAttributes(raw=attr_text, attributes=[attr1, attr2]) self.assertEqual(attr_text, text_type(attrs))
def test_str_with_attrs(self): raw = '<p class="headline">' attr = HTMLAttribute(raw='class="headline"', start=3, ident='class', value='headline') attrs = HTMLAttributes(raw='class="headline"', start=3, attributes=[attr]) tag = HTMLOpenTag(raw='<a href="http://example.com">', tag='p', attributes=attrs) self.assertEqual(raw, text_type(tag))
def test_to_text(self): raw = '<p>A <strong>Text</strong> Element</p>' strong_attrs = HTMLAttributes(raw='', attributes=[]) strong_open_tag = HTMLOpenTag(raw='<strong>', tag='strong', attributes=strong_attrs) strong_close_tag = HTMLCloseTag(raw='</strong>', tag='strong') strong_text = HTMLText(raw='Text') strong_elem = HTMLElement(raw='<strong>Text</strong>', open_tag=strong_open_tag, close_tag=strong_close_tag, children=[strong_text]) p_attrs = HTMLAttributes(raw='', attributes=[]) p_open_tag = HTMLOpenTag(raw='<p>', tag='p', attributes=p_attrs) p_close_tag = HTMLCloseTag(raw='</p>', tag='p') text1 = HTMLText(raw='A ') text2 = HTMLText(raw=' Element') p_elem = HTMLElement(raw=raw, open_tag=p_open_tag, close_tag=p_close_tag, children=[text1, strong_elem, text2]) self.assertEqual('A Text Element', p_elem.to_text())
def test_to_text(self): raw = '<p>A <strong>Text</strong> Element</p>' strong_attrs = HTMLAttributes(raw="", attributes=[]) strong_open_tag = HTMLOpenTag(raw="<strong>", tag="strong", attributes=strong_attrs) strong_close_tag = HTMLCloseTag(raw="</strong>", tag="strong") strong_text = HTMLText(raw="Text") strong_elem = HTMLElement(raw="<strong>Text</strong>", open_tag=strong_open_tag, close_tag=strong_close_tag, children=[strong_text]) p_attrs = HTMLAttributes(raw="", attributes=[]) p_open_tag = HTMLOpenTag(raw="<p>", tag="p", attributes=p_attrs) p_close_tag = HTMLCloseTag(raw="</p>", tag="p") text1 = HTMLText(raw="A ") text2 = HTMLText(raw=" Element") p_elem = HTMLElement(raw=raw, open_tag=p_open_tag, close_tag=p_close_tag, children=[text1, strong_elem, text2]) self.assertEqual('A Text Element', p_elem.to_text())
def test_str(self): raw = '<p class="first">A Text Element</p>' attr = HTMLAttribute(raw='class="first"', start=3, ident='class', value='first') attrs = HTMLAttributes(raw='class="first"', start=3, attributes=[attr]) open_tag = HTMLOpenTag(raw='<p class="first">', tag='p', attributes=attrs) close_tag = HTMLCloseTag(raw='</p>', start=31, tag='p') children = [HTMLInterval(raw='A Text Element', start=17)] element = HTMLElement(raw=raw, open_tag=open_tag, close_tag=close_tag, children=children) self.assertEqual(raw, str(element))
def test_as_dict_attrs(self): attr = HTMLAttribute(raw='foo=bar', ident='foo', value='bar') attrs = HTMLAttributes(raw='foo=bar', attributes=[attr]) self.assertEqual(attrs.as_dict(), {'foo': 'bar'})
def test_as_dict_empty(self): attrs = HTMLAttributes(raw=' ') self.assertEqual(attrs.as_dict(), {})
def test_str_empty(self): attrs = HTMLAttributes(raw=' ') self.assertEqual('', text_type(attrs))
def test_br_to_html_drop_tag(self): raw = '<br>' attrs = HTMLAttributes() open_tag = HTMLOpenTag(raw=raw, tag='br', attributes=attrs) element = HTMLElement(raw=raw, open_tag=open_tag, drop_tag=True) self.assertEqual('', element.to_html())
def test_str_without_attrs(self): raw = '<strong>' attrs = HTMLAttributes(start=3) tag = HTMLOpenTag(raw='<strong>', tag='strong', attributes=attrs) self.assertEqual(raw, text_type(tag))