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 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_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_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_str_bool_value(self): attr = HTMLAttribute(raw='selected', ident='selected', value=True) self.assertEqual('selected', text_type(attr))
def test_str_int_value(self): attr = HTMLAttribute(raw='selected=1', ident='selected', value=1) self.assertEqual('selected=1', text_type(attr))
def test_str_string_value(self): attr = HTMLAttribute(raw='foo="bar"', ident='foo', value='bar') self.assertEqual('foo="bar"', text_type(attr))