示例#1
0
 def setUp(self):
     customElements.clear()
     self.elm = Element('tag')
     self.c1 = Element('c1')
     self.c2 = Element('c2')
     self.c1.classList.add('c1')
     self.c2.classList.add('c2')
示例#2
0
    def send_keys(self, element: Element, keys: str) -> None:
        """Send ``keys`` to ``element`` one-by-one.

        Safer than using ``element.send_keys`` method.
        """
        for k in keys:
            element.send_keys(k)
示例#3
0
 def setUp(self):
     customElements.clear()
     self.elm = Element('tag')
     self.c1 = Element('c1')
     self.c2 = Element('c2')
     self.c1.classList.add('c1')
     self.c2.classList.add('c2')
示例#4
0
    def test_constructor(self):
        elm = Element('a')
        self.assertEqual(elm.nodeName, 'A')
        self.assertEqual(elm.tagName, 'A')
        self.assertEqual(elm.localName, 'a')

        self.assertFalse(elm.hasChildNodes())
        self.assertFalse(elm.hasAttributes())
示例#5
0
 def test_reference_del_id(self):
     gc.collect()
     elm = Element('a', id='a')
     self.assertIn('a', Element._elements_with_id)
     self.assertIn(elm, Element._elements_with_id.values())
     elm.removeAttribute('id')
     self.assertNotIn('a', Element._elements_with_id)
     self.assertNotIn(elm, Element._elements_with_id.values())
示例#6
0
 def test_create_attr(self):
     a = self.doc.createAttribute('src')
     a.value = 'a'
     self.assertEqual(type(a), Attr)
     self.assertEqual(a.html, 'src="a"')
     tag = Element('tag')
     tag.setAttributeNode(a)
     self.assertEqual(tag.html, '<tag src="a"></tag>')
示例#7
0
 def test_reference_del_id(self):
     gc.collect()
     elm = Element('a', id='a')
     self.assertIn('a', Element._elements_with_id)
     self.assertIn(elm, Element._elements_with_id.values())
     elm.removeAttribute('id')
     self.assertNotIn('a', Element._elements_with_id)
     self.assertNotIn(elm, Element._elements_with_id.values())
示例#8
0
    def test_constructor(self):
        elm = Element('a')
        self.assertEqual(elm.nodeName, 'A')
        self.assertEqual(elm.tagName, 'A')
        self.assertEqual(elm.localName, 'a')

        self.assertFalse(elm.hasChildNodes())
        self.assertFalse(elm.hasAttributes())
示例#9
0
    def test_init_class(self):
        elm = Element('a', class_='a')
        self.assertEqual(elm.html, '<a class="a"></a>')
        self.assertEqual(elm.classList.length, 1)
        self.assertIn('a', elm.classList)

        elm2 = Element('a', **{'class': 'b'})
        self.assertEqual(elm2.html, '<a class="b"></a>')
        self.assertEqual(elm2.classList.length, 1)
        self.assertIn('b', elm2.classList)
示例#10
0
 def test_children(self):
     self.p.appendChild(Text('a'))
     self.p.appendChild(Comment('b'))
     self.assertEqual(self.p.children.length, 0)
     elm1 = Element('c1')
     self.p.appendChild(elm1)
     self.p.appendChild(Text('d'))
     self.assertEqual(self.p.children.length, 1)
     self.assertIs(self.p.firstElementChild, elm1)
     self.assertIs(self.p.lastElementChild, elm1)
     elm2 = Element('c2')
     self.p.appendChild(elm2)
     self.p.appendChild(Text('e'))
     self.assertIs(self.p.firstElementChild, elm1)
     self.assertIs(self.p.lastElementChild, elm2)
示例#11
0
 def test_init_class_multi_list(self):
     elm = Element('a', class_=['a1', 'a2'])
     self.assertEqual(elm.html, '<a class="a1 a2"></a>')
     self.assertEqual(elm.classList.length, 2)
     self.assertIn('a1', elm.classList)
     self.assertIn('a2', elm.classList)
     self.assertNotIn('a1 a2', elm.classList)
示例#12
0
 def test_add_invlalid(self):
     with self.assertRaises(TypeError):
         self.tokens.add(1)
     with self.assertRaises(TypeError):
         self.tokens.add(Element('a'))
     self.assertEqual(len(self.tokens), 0)
     self.assertFalse(bool(self.tokens))
     self.assertEqual('', self.tokens.toString())
示例#13
0
 def test_reference(self):
     gc.collect()
     elm = Element('a')
     _id = id(elm)
     self.assertIn(elm, Element._element_buffer)
     del elm
     gc.collect()  # run gc
     for elm in Element._element_buffer:
         assert id(elm) != _id
示例#14
0
    def test_inner_html(self):
        self.assertEqual(self.elm.innerHTML, '')
        self.elm.appendChild(Element('a'))
        self.assertEqual(self.elm.innerHTML, '<a></a>')

        self.elm.innerHTML = '<b></b>'
        self.assertEqual(self.elm.innerHTML, '<b></b>')
        self.assertEqual(self.elm.firstChild.tag, 'b')
        self.assertTrue(isinstance(self.elm.firstChild, Element))
示例#15
0
 def setUp(self):
     self.elm = Element('tag')
     self.c1 = Element('c1')
     self.c2 = Element('c1')
     self.c3 = Element('c3')
     self.c1.setAttribute('a', 'a')
     self.c2.setAttribute('b', 'b')
     self.elm.append(self.c1, self.c2, self.c3)
示例#16
0
 def test_reference_with_id(self):
     gc.collect()
     elm = Element('a', id='a')
     _id = id(elm)
     self.assertIn(elm.id, Element._elements_with_id)
     del elm
     gc.collect()
     self.assertNotIn('a', Element._elements_with_id)
     for elm in Element._element_buffer:
         assert id(elm) != _id
示例#17
0
    def test_is_attr(self):
        '''``is`` is a reserved word for python, so use ``is_`` in constructor.
        '''
        elm = Element('tag', is_='elm')
        self.assertIn('is', elm.attributes)
        self.assertNotIn('is_', elm.attributes)
        self.assertEqual('elm', elm.getAttribute('is'))
        self.assertIsNone(elm.getAttribute('is_'))

        # ``is_`` is not treated as special at setAttribute
        elm.setAttribute('is_', 'new')
        self.assertEqual('elm', elm.getAttribute('is'))
        self.assertEqual('new', elm.getAttribute('is_'))
示例#18
0
 def test_reference_add_id(self):
     gc.collect()
     elm = Element('a')
     _id = id(elm)
     self.assertNotIn(elm, Element._elements_with_id.values())
     elm.id = 'a'
     self.assertIn('a', Element._elements_with_id)
     self.assertIn(elm, Element._elements_with_id.values())
     elm.id = 'b'
     self.assertNotIn('a', Element._elements_with_id)
     self.assertIn('b', Element._elements_with_id)
     self.assertIn(elm, Element._elements_with_id.values())
     elm.setAttribute('id', 'c')
     self.assertNotIn('b', Element._elements_with_id)
     self.assertIn('c', Element._elements_with_id)
     self.assertIn(elm, Element._elements_with_id.values())
     del elm
     gc.collect()
     self.assertNotIn('c', Element._elements_with_id)
     for elm in Element._elements:
         assert id(elm) != _id
示例#19
0
    def test_is_attr(self):
        '''``is`` is a reserved word for python, so use ``is_`` in constructor.
        '''
        elm = Element('tag', is_='elm')
        self.assertIn('is', elm.attributes)
        self.assertNotIn('is_', elm.attributes)
        self.assertEqual('elm', elm.getAttribute('is'))
        self.assertIsNone(elm.getAttribute('is_'))

        # ``is_`` is not treated as special at setAttribute
        elm.setAttribute('is_', 'new')
        self.assertEqual('elm', elm.getAttribute('is'))
        self.assertEqual('new', elm.getAttribute('is_'))
示例#20
0
 def test_reference_add_id(self):
     gc.collect()
     elm = Element('a')
     _id = id(elm)
     self.assertNotIn(elm, Element._elements_with_id.values())
     elm.id = 'a'
     self.assertIn('a', Element._elements_with_id)
     self.assertIn(elm, Element._elements_with_id.values())
     elm.id = 'b'
     self.assertNotIn('a', Element._elements_with_id)
     self.assertIn('b', Element._elements_with_id)
     self.assertIn(elm, Element._elements_with_id.values())
     elm.setAttribute('id', 'c')
     self.assertNotIn('b', Element._elements_with_id)
     self.assertIn('c', Element._elements_with_id)
     self.assertIn(elm, Element._elements_with_id.values())
     del elm
     gc.collect()
     self.assertNotIn('c', Element._elements_with_id)
     for elm in Element._element_buffer:
         assert id(elm) != _id
示例#21
0
 def setUp(self):
     self.p = P()
     self.c1 = NDTC()
     self.c2 = NDTC()
     self.e1 = Element()
     self.e2 = Element()
示例#22
0
class TestElement(TestCase):
    def setUp(self):
        customElements.clear()
        self.elm = Element('tag')
        self.c1 = Element('c1')
        self.c2 = Element('c2')
        self.c1.classList.add('c1')
        self.c2.classList.add('c2')

    def tearDown(self):
        del self.elm
        del self.c1
        del self.c2
        super().tearDown()

    def test_constructor(self):
        elm = Element('a')
        self.assertEqual(elm.nodeName, 'A')
        self.assertEqual(elm.tagName, 'A')
        self.assertEqual(elm.localName, 'a')

        self.assertFalse(elm.hasChildNodes())
        self.assertFalse(elm.hasAttributes())

    def test_init_parent(self):
        elm = Element('a', parent=self.elm)
        self.assertIs(elm.parentNode, self.elm)

    def test_init_attrs(self):
        elm = Element('a', src='b', href='c')
        self.assertFalse(elm.hasChildNodes())
        self.assertTrue(elm.hasAttributes())
        self.assertTrue(elm.getAttribute('src'), 'b')
        self.assertTrue(elm.getAttribute('href'), 'c')

    def test_attrs(self):
        self.assertFalse(self.elm.hasAttributes())
        self.assertFalse(self.elm.hasAttribute('src'))
        self.assertNotIn('src', self.elm.attributes)
        self.assertIsNone(self.elm.getAttribute('src'))
        self.assertEqual(self.elm.html, '<tag></tag>')
        self.elm.setAttribute('src', 'a')
        self.assertTrue(self.elm.hasAttributes())
        self.assertTrue(self.elm.hasAttribute('src'))
        self.assertIn('src', self.elm.attributes)
        self.assertEqual(self.elm.getAttribute('src'), 'a')
        self.assertEqual(self.elm.html, '<tag src="a"></tag>')

        self.elm.removeAttribute('src')
        self.assertFalse(self.elm.hasAttributes())
        self.assertFalse(self.elm.hasAttribute('src'))
        self.assertNotIn('src', self.elm.attributes)
        self.assertIsNone(self.elm.getAttribute('src'))
        self.assertEqual(self.elm.html, '<tag></tag>')

    def test_id(self):
        self.assertEqual(self.elm.id, '')
        self.elm.setAttribute('id', 'a')
        self.assertEqual(self.elm.getAttribute('id'), 'a')
        self.assertEqual(self.elm.id, 'a')
        self.elm.id = 'b'
        self.assertEqual(self.elm.getAttribute('id'), 'b')
        self.assertEqual(self.elm.id, 'b')

    def test_class_list_str(self):
        self.assertIsNone(self.elm.getAttribute('class'))
        self.assertFalse(self.elm.hasAttribute('class'))
        self.assertFalse(self.elm.hasAttributes())

        self.elm.setAttribute('class', 'a')
        self.assertEqual(self.elm.getAttribute('class'), 'a')
        self.assertTrue(self.elm.hasAttribute('class'))
        self.assertTrue(self.elm.hasAttributes())

        self.elm.removeAttribute('class')
        self.assertIsNone(self.elm.getAttribute('class'))
        self.assertFalse(self.elm.hasAttribute('class'))
        self.assertFalse(self.elm.hasAttributes())

    def test_class_list_list(self):
        self.elm.setAttribute('class', ['a', 'b'])
        self.assertEqual(self.elm.getAttribute('class'), 'a b')
        self.assertTrue(self.elm.hasAttribute('class'))
        self.assertTrue(self.elm.hasAttributes())

    def test_class_name(self):
        self.assertEqual(self.elm.className, '')
        self.elm.className = 'a'
        self.assertEqual(self.elm.className, 'a')
        self.assertEqual(self.elm.getAttribute('class'), 'a')
        self.elm.className = 'b c'
        self.assertEqual(self.elm.className, 'b c')
        self.assertEqual(self.elm.getAttribute('class'), 'b c')
        self.assertEqual(self.elm.classList.length, 2)
        self.elm.className = 'd'
        self.assertEqual(self.elm.className, 'd')
        self.assertEqual(self.elm.getAttribute('class'), 'd')
        self.assertEqual(self.elm.classList.length, 1)
        with self.assertRaises(TypeError):
            self.elm.className = ['d']

    def test_start_tag(self):
        self.assertEqual(self.elm.start_tag, '<tag>')
        self.elm.setAttribute('src', 'a')
        self.assertEqual(self.elm.start_tag, '<tag src="a">')

        self.elm.setAttribute('class', 'b')
        self.assertEqual(self.elm.start_tag, '<tag src="a" class="b">')

        self.elm.id = 'c'
        self.assertIn('src="a"', self.elm.start_tag)
        self.assertIn('id="c"', self.elm.start_tag)

    def test_inner_html(self):
        self.assertEqual(self.elm.innerHTML, '')
        self.elm.appendChild(Element('a'))
        self.assertEqual(self.elm.innerHTML, '<a></a>')

        self.elm.innerHTML = '<b></b>'
        self.assertEqual(self.elm.innerHTML, '<b></b>')
        self.assertEqual(self.elm.firstChild.tag, 'b')
        self.assertTrue(isinstance(self.elm.firstChild, Element))

    def test_inner_html_nest(self):
        html = '<b><c>d</c>e</b>'
        self.elm.innerHTML = html
        self.assertEqual(self.elm.innerHTML, html)
        self.assertEqual(self.elm.firstChild.html, html)
        self.assertEqual(self.elm.firstChild.firstChild.html, '<c>d</c>')
        self.assertEqual(self.elm.firstChild.firstChild.innerHTML, 'd')
        self.assertEqual(self.elm.firstChild.lastChild.html, 'e')
        self.assertTrue(isinstance(self.elm.firstChild.lastChild, Text))

    def test_parse_html_text(self):
        html = '''
        <a>a1</a1>
        b
        '''
        self.elm.innerHTML = html
        # fisrt node is empty (\n+spaces) text node
        self.assertEqual(self.elm.childNodes.length, 3)
        self.assertTrue(isinstance(self.elm.firstChild, Text))
        self.assertTrue(isinstance(self.elm.lastChild, Text))
        self.assertTrue(isinstance(self.elm.firstElementChild, Element))
        self.assertTrue(isinstance(self.elm.lastElementChild, Element))

    def test_insert_adjacent_html(self):
        self.elm.appendChild(self.c1)
        self.c1.insertAdjacentHTML('beforebegin', '<a></a>')
        self.assertEqual(self.elm.childNodes.length, 2)
        self.assertIs(self.elm.lastElementChild, self.c1)
        self.c1.insertAdjacentHTML('afterend', 'text')
        self.assertEqual(self.elm.childNodes.length, 3)
        self.assertIs(self.elm.lastElementChild, self.c1)
        self.c1.insertAdjacentHTML('afterBegin', '<b></b>')
        self.assertEqual(self.c1.childNodes.length, 1)
        self.c1.insertAdjacentHTML('BeforeEnd', '<c></c>')
        self.assertEqual(self.c1.childNodes.length, 2)
        with self.assertRaises(ValueError):
            self.c1.insertAdjacentHTML('a', 'b')

    def test_end_tag(self):
        self.assertEqual(self.elm.end_tag, '</tag>')

    def test_html(self):
        self.assertEqual(self.elm.html, '<tag></tag>')

    def test_append_string(self):
        with self.assertRaises(TypeError):
            self.elm.appendChild('a')
        self.assertFalse(self.elm.hasChildNodes())

    def test_get_elements_by_tagname(self):
        self.elm.appendChild(self.c1)
        self.elm.appendChild(self.c2)
        c1_tags = self.elm.getElementsByTagName('c1')
        c2_tags = self.elm.getElementsByTagName('c2')
        self.assertEqual(len(c1_tags), 1)
        self.assertEqual(len(c2_tags), 1)
        self.assertIs(c1_tags[0], self.c1)
        self.assertIs(c2_tags[0], self.c2)

    def test_get_elements_by_tagname_nest(self):
        self.elm.appendChild(self.c1)
        self.c1.appendChild(self.c2)
        c2_tags = self.c1.getElementsByTagName('c2')
        self.assertEqual(len(c2_tags), 1)
        self.assertIs(c2_tags[0], self.c2)
        c2_tags = self.elm.getElementsByTagName('c2')
        self.assertEqual(len(c2_tags), 1)
        self.assertIs(c2_tags[0], self.c2)

    def test_get_elements_by_tagname_self(self):
        c1_tags = self.c1.getElementsByTagName('c1')
        self.assertEqual(len(c1_tags), 0)

    def test_get_elements_by_classname(self):
        self.elm.appendChild(self.c1)
        self.elm.appendChild(self.c2)
        c1_classes = self.elm.getElementsByClassName('c1')
        c2_classes = self.elm.getElementsByClassName('c2')
        self.assertEqual(len(c1_classes), 1)
        self.assertEqual(len(c2_classes), 1)
        self.assertIs(c1_classes[0], self.c1)
        self.assertIs(c2_classes[0], self.c2)

    def test_get_elements_by_classname_multi1(self):
        self.elm.appendChild(self.c1)
        self.elm.appendChild(self.c2)
        self.c2.classList.add('c1')
        c1_classes = self.elm.getElementsByClassName('c1')
        c2_classes = self.elm.getElementsByClassName('c2')
        self.assertEqual(len(c1_classes), 2)
        self.assertEqual(len(c2_classes), 1)

    def test_get_elements_by_classname_multi2(self):
        self.elm.appendChild(self.c1)
        self.elm.appendChild(self.c2)
        self.c1.classList.add('c3')
        self.c2.classList.add('c1')
        self.c2.classList.add('c3')
        classes1 = self.elm.getElementsByClassName('c1')
        classes2 = self.elm.getElementsByClassName('c1 c2')
        classes3 = self.elm.getElementsByClassName('c1 c2 c4')
        self.assertEqual(len(classes1), 2)
        self.assertEqual(len(classes2), 1)
        self.assertEqual(len(classes3), 0)
        self.assertIs(classes2[0], self.c2)

    def test_get_elements_by_classname_nest(self):
        self.elm.appendChild(self.c1)
        self.c1.appendChild(self.c2)
        c2_classes = self.c1.getElementsByClassName('c2')
        self.assertEqual(len(c2_classes), 1)
        self.assertIs(c2_classes[0], self.c2)
        c2_classes = self.elm.getElementsByClassName('c2')
        self.assertEqual(len(c2_classes), 1)
        self.assertIs(c2_classes[0], self.c2)

    def test_clone_shallow_child(self):
        self.elm.appendChild(self.c1)
        clone = self.elm.cloneNode()
        self.assertFalse(clone.hasChildNodes())

        clone.appendChild(self.c2)
        self.assertFalse(self.c2 in self.elm)
        self.assertEqual(len(self.elm.childNodes), 1)

    def test_clone_shallow_attr(self):
        self.elm.setAttribute('src', 'a')
        clone = self.elm.cloneNode()

        self.assertTrue(clone.hasAttribute('src'))
        self.assertTrue(clone.getAttribute('src'), 'a')
        self.assertEqual(self.elm.attributes.toString(),
                         clone.attributes.toString())
        clone.setAttribute('src', 'b')
        self.assertNotEqual(self.elm.attributes.toString(),
                            clone.attributes.toString())
        self.assertTrue(self.elm.getAttribute('src'), 'a')
        self.assertTrue(clone.getAttribute('src'), 'b')

    def test_clone_deep_child(self):
        self.elm.appendChild(self.c1)
        clone = self.elm.cloneNode(deep=True)
        self.assertTrue(clone.hasChildNodes())
        self.assertEqual(len(clone.childNodes), 1)
        self.assertTrue(self.c1 in self.elm)
        self.assertFalse(self.c1 in clone)
        self.assertIsNot(self.c1, clone.firstChild)

        clone.appendChild(self.c2)
        self.assertFalse(self.c2 in self.elm)
        self.assertEqual(len(self.elm.childNodes), 1)
        self.assertEqual(len(clone.childNodes), 2)

    def test_clone_deep_attr(self):
        self.elm.setAttribute('src', 'a')
        self.elm.appendChild(self.c1)
        self.c1.setAttribute('src', 'c1')
        clone = self.elm.cloneNode(deep=True)

        self.assertTrue(clone.hasAttribute('src'))
        self.assertTrue(clone.getAttribute('src'), 'a')
        self.assertTrue(clone.firstChild.hasAttribute('src'))
        self.assertTrue(clone.firstChild.getAttribute('src'), 'c1')
        self.assertEqual(self.elm.attributes.toString(),
                         clone.attributes.toString())
        self.assertEqual(self.c1.attributes.toString(),
                         clone.firstChild.attributes.toString())

        clone.firstChild.setAttribute('src', 'b')
        self.assertNotEqual(self.c1.attributes.toString(),
                            clone.firstChild.attributes.toString())
        self.assertTrue(self.c1.getAttribute('src'), 'a')
        self.assertTrue(clone.firstChild.getAttribute('src'), 'b')

    def test_clone_style(self):
        self.elm.setAttribute('style', 'color: red;')
        clone = self.elm.cloneNode()
        self.assertEqual(clone.html, self.elm.html)

    def test_init_class(self):
        elm = Element('a', class_='a')
        self.assertEqual(elm.html, '<a class="a"></a>')
        self.assertEqual(elm.classList.length, 1)
        self.assertIn('a', elm.classList)

        elm2 = Element('a', **{'class': 'b'})
        self.assertEqual(elm2.html, '<a class="b"></a>')
        self.assertEqual(elm2.classList.length, 1)
        self.assertIn('b', elm2.classList)

    def test_init_class_multi_str(self):
        elm = Element('a', class_='a1 a2')
        self.assertEqual(elm.html, '<a class="a1 a2"></a>')
        self.assertEqual(elm.classList.length, 2)
        self.assertIn('a1', elm.classList)
        self.assertIn('a2', elm.classList)
        self.assertNotIn('a1 a2', elm.classList)

    def test_init_class_multi_list(self):
        elm = Element('a', class_=['a1', 'a2'])
        self.assertEqual(elm.html, '<a class="a1 a2"></a>')
        self.assertEqual(elm.classList.length, 2)
        self.assertIn('a1', elm.classList)
        self.assertIn('a2', elm.classList)
        self.assertNotIn('a1 a2', elm.classList)

    def test_init_class_multi_mixed(self):
        elm = Element('a', class_=['a1', 'a2 a3'])
        self.assertEqual(elm.html, '<a class="a1 a2 a3"></a>')
        self.assertEqual(elm.classList.length, 3)
        self.assertIn('a1', elm.classList)
        self.assertIn('a2', elm.classList)
        self.assertIn('a3', elm.classList)
        self.assertNotIn('a2 a3', elm.classList)

    def test_reference(self):
        gc.collect()
        elm = Element('a')
        _id = id(elm)
        self.assertIn(elm, Element._element_buffer)
        del elm
        gc.collect()  # run gc
        for elm in Element._element_buffer:
            assert id(elm) != _id

    def test_reference_with_id(self):
        gc.collect()
        elm = Element('a', id='a')
        _id = id(elm)
        self.assertIn(elm.id, Element._elements_with_id)
        del elm
        gc.collect()
        self.assertNotIn('a', Element._elements_with_id)
        for elm in Element._element_buffer:
            assert id(elm) != _id

    @skipIf(sys.implementation.name == 'pypy', 'GC not work in PyPy.')
    def test_reference_add_id(self):
        gc.collect()
        elm = Element('a')
        _id = id(elm)
        self.assertNotIn(elm, Element._elements_with_id.values())
        elm.id = 'a'
        self.assertIn('a', Element._elements_with_id)
        self.assertIn(elm, Element._elements_with_id.values())
        elm.id = 'b'
        self.assertNotIn('a', Element._elements_with_id)
        self.assertIn('b', Element._elements_with_id)
        self.assertIn(elm, Element._elements_with_id.values())
        elm.setAttribute('id', 'c')
        self.assertNotIn('b', Element._elements_with_id)
        self.assertIn('c', Element._elements_with_id)
        self.assertIn(elm, Element._elements_with_id.values())
        del elm
        gc.collect()
        self.assertNotIn('c', Element._elements_with_id)
        for elm in Element._element_buffer:
            assert id(elm) != _id

    def test_reference_del_id(self):
        gc.collect()
        elm = Element('a', id='a')
        self.assertIn('a', Element._elements_with_id)
        self.assertIn(elm, Element._elements_with_id.values())
        elm.removeAttribute('id')
        self.assertNotIn('a', Element._elements_with_id)
        self.assertNotIn(elm, Element._elements_with_id.values())

    def test_is_attr(self):
        '''``is`` is a reserved word for python, so use ``is_`` in constructor.
        '''
        elm = Element('tag', is_='elm')
        self.assertIn('is', elm.attributes)
        self.assertNotIn('is_', elm.attributes)
        self.assertEqual('elm', elm.getAttribute('is'))
        self.assertIsNone(elm.getAttribute('is_'))

        # ``is_`` is not treated as special at setAttribute
        elm.setAttribute('is_', 'new')
        self.assertEqual('elm', elm.getAttribute('is'))
        self.assertEqual('new', elm.getAttribute('is_'))

    class NewTag(HTMLElement):
        pass

    def test_custom_tag(self):
        self.elm.innerHTML = '<new-tag></new-tag>'
        child = self.elm.firstChild
        self.assertEqual(child.__class__, Element)
        customElements.define('new-tag', self.NewTag)
        self.assertEqual(child.__class__, self.NewTag)

    def test_custom_tag_registered(self):
        customElements.define('new-tag', self.NewTag)
        self.elm.innerHTML = '<new-tag></new-tag>'
        self.assertEqual(self.elm.firstChild.__class__, self.NewTag)

    def test_custom_tag_is(self):
        self.elm.innerHTML = '<a is="my-a"></a>'
        child = self.elm.firstChild
        self.assertEqual(child.__class__, Element)
        self.assertEqual(child.getAttribute('is'), 'my-a')
        customElements.define('my-a', self.NewTag, {'extends': 'a'})
        self.assertEqual(self.elm.firstChild.__class__, self.NewTag)

    def test_custom_tag_is_registered(self):
        customElements.define('my-a', self.NewTag, {'extends': 'a'})
        self.elm.innerHTML = '<a is="my-a"></a>'
        self.assertEqual(self.elm.firstChild.__class__, self.NewTag)

    def test_invalid_define_args(self):
        with self.assertRaises(TypeError):
            customElements.define(1, 2, 3)
示例#23
0
class TestGetElements(TestCase):
    def setUp(self):
        self.elm = Element('tag')
        self.c1 = Element('c1')
        self.c2 = Element('c1')
        self.c3 = Element('c3')
        self.c1.setAttribute('a', 'a')
        self.c2.setAttribute('b', 'b')
        self.elm.append(self.c1, self.c2, self.c3)

    def test_get_elements_by_attr1(self):
        attr_elms = getElementsBy(self.elm, lambda n: n.hasAttributes())
        self.assertEqual(attr_elms.length, 2)
        self.assertIn(self.c1, attr_elms)
        self.assertIn(self.c2, attr_elms)
        self.assertNotIn(self.c3, attr_elms)

    def test_get_elements_by_attr2(self):
        attr_elms = getElementsBy(self.elm, lambda n: n.hasAttribute('a'))
        self.assertEqual(attr_elms.length, 1)
        self.assertIs(attr_elms[0], self.c1)
        self.assertNotIn(self.c2, attr_elms)
        self.assertNotIn(self.c3, attr_elms)

    def test_get_elements_by_tagname1(self):
        attr_elms = getElementsByTagName(self.elm, 'c1')
        self.assertEqual(attr_elms.length, 2)
        self.assertIn(self.c1, attr_elms)
        self.assertIn(self.c2, attr_elms)
        self.assertNotIn(self.c3, attr_elms)

    def test_get_elements_by_tagname2(self):
        attr_elms = getElementsByTagName(self.elm, 'c3')
        self.assertEqual(attr_elms.length, 1)
        self.assertNotIn(self.c1, attr_elms)
        self.assertNotIn(self.c2, attr_elms)
        self.assertIs(self.c3, attr_elms[0])

    def test_get_elements_by_classname1(self):
        self.c1.className = 'c1'
        attr_elms = getElementsByClassName(self.elm, 'c1')
        self.assertEqual(attr_elms.length, 1)
        self.assertIn(self.c1, attr_elms)
        self.assertNotIn(self.c2, attr_elms)
        self.assertNotIn(self.c3, attr_elms)

    def test_get_elements_by_classname2(self):
        self.c1.className = 'c1'
        self.c2.className = 'c1'
        attr_elms = getElementsByClassName(self.elm, 'c1')
        self.assertEqual(attr_elms.length, 2)
        self.assertIn(self.c1, attr_elms)
        self.assertIn(self.c2, attr_elms)
        self.assertNotIn(self.c3, attr_elms)

    def test_get_elements_by_classname3(self):
        self.c1.className = 'c1'
        self.c2.className = 'c1 c2'
        self.c3.className = 'c2 c1'
        attr_elms = getElementsByClassName(self.elm, 'c2 c1')
        self.assertEqual(attr_elms.length, 2)
        self.assertNotIn(self.c1, attr_elms)
        self.assertIn(self.c2, attr_elms)
        self.assertIn(self.c3, attr_elms)
示例#24
0
 def test_init_parent(self):
     elm = Element('a', parent=self.elm)
     self.assertIs(elm.parentNode, self.elm)
示例#25
0
 def test_get_element_by_id(self):
     elm = Element(tag='a', id='a')
     self.assertIs(getElementById('a'), elm)
     self.assertIsNone(self.doc.getElementById('a'), elm)
     self.doc.appendChild(elm)
     self.assertIs(getElementById('a'), elm)
示例#26
0
 def test_init_attrs(self):
     elm = Element('a', src='b', href='c')
     self.assertFalse(elm.hasChildNodes())
     self.assertTrue(elm.hasAttributes())
     self.assertTrue(elm.getAttribute('src'), 'b')
     self.assertTrue(elm.getAttribute('href'), 'c')
示例#27
0
class TestDocumentFragment(TestCase):
    def setUp(self):
        self.df = DocumentFragment()
        self.elm = Element('a')
        self.c1 = Element('c1')
        self.c2 = Element('c2')
        self.c3 = Element('c3')

    def test_nodename(self):
        self.assertEqual(self.df.nodeName, '#document-fragment')

    def test_children(self):
        self.assertFalse(self.df.hasChildNodes())
        appended_child = self.df.appendChild(self.elm)
        self.assertEqual(self.df.html, '<a></a>')
        self.assertIs(appended_child, self.elm)

    def test_append_to_element(self):
        appended_child1 = self.df.appendChild(self.c1)
        appended_child2 = self.df.appendChild(self.c2)
        self.assertIs(self.df, self.c1.parentNode)
        self.assertIs(self.df, self.c2.parentNode)
        self.assertIs(appended_child1, self.c1)
        self.assertIs(appended_child2, self.c2)

        appended_df = self.elm.appendChild(self.df)
        self.assertIs(appended_df, self.df)
        self.assertEqual(self.df.length, 0)
        self.assertEqual(self.elm.length, 2)
        self.assertFalse(self.df.hasChildNodes())
        self.assertIsNone(self.df.parentNode)
        self.assertIs(self.elm, self.c1.parentNode)
        self.assertIs(self.elm, self.c2.parentNode)
        self.assertIsNone(self.df.parentNode)

    def test_insert_to_element(self):
        self.df.appendChild(self.c1)
        self.df.appendChild(self.c2)
        self.elm.appendChild(self.c3)

        inserted_node = self.elm.insertBefore(self.df, self.c3)
        self.assertIs(inserted_node, self.df)
        self.assertEqual(self.df.length, 0)
        self.assertEqual(self.elm.length, 3)
        self.assertFalse(self.df.hasChildNodes())
        self.assertIsNone(self.df.parentNode)
        self.assertIs(self.elm, self.c1.parentNode)
        self.assertIs(self.elm, self.c2.parentNode)

        self.assertIsNone(self.c1.previousSibling)
        self.assertIs(self.c1.nextSibling, self.c2)
        self.assertIs(self.c2.previousSibling, self.c1)
        self.assertIs(self.c2.nextSibling, self.c3)
        self.assertIs(self.c3.previousSibling, self.c2)
        self.assertIsNone(self.c3.nextSibling)
        self.assertIs(self.elm.firstChild, self.c1)
        self.assertIs(self.elm.lastChild, self.c3)

    def test_child(self):
        appended_child1 = self.df.appendChild(self.c1)
        appended_child2 = self.df.appendChild(self.c2)
        self.assertIs(appended_child1, self.c1)
        self.assertIs(appended_child2, self.c2)
        self.assertEqual(self.c1.html, '<c1></c1>')
        self.assertEqual(self.df.html, '<c1></c1><c2></c2>')

    def test_clone_node_sharrow(self):
        self.df.appendChild(self.c1)
        clone = self.df.cloneNode()
        self.assertEqual(self.df.length, 1)
        self.assertEqual(clone.length, 0)
        clone.appendChild(self.c2)
        self.assertEqual(self.df.length, 1)
        self.assertEqual(clone.length, 1)

    def test_clone_node_deep(self):
        self.df.appendChild(self.c1)
        clone = self.df.cloneNode(deep=True)
        self.assertEqual(self.df.length, 1)
        self.assertEqual(clone.length, 1)
        self.assertEqual(clone.html, '<c1></c1>')

        clone.appendChild(self.c2)
        self.assertEqual(self.df.length, 1)
        self.assertEqual(clone.length, 2)
        self.df.removeChild(self.c1)
        self.assertEqual(self.df.length, 0)
        self.assertEqual(clone.length, 2)
示例#28
0
 def setUp(self):
     self.df = DocumentFragment()
     self.elm = Element('a')
     self.c1 = Element('c1')
     self.c2 = Element('c2')
     self.c3 = Element('c3')
示例#29
0
 def test_init_attrs(self):
     elm = Element('a', src='b', href='c')
     self.assertFalse(elm.hasChildNodes())
     self.assertTrue(elm.hasAttributes())
     self.assertTrue(elm.getAttribute('src'), 'b')
     self.assertTrue(elm.getAttribute('href'), 'c')
示例#30
0
class TestElement(TestCase):
    def setUp(self):
        customElements.clear()
        self.elm = Element('tag')
        self.c1 = Element('c1')
        self.c2 = Element('c2')
        self.c1.classList.add('c1')
        self.c2.classList.add('c2')

    def tearDown(self):
        del self.elm
        del self.c1
        del self.c2
        super().tearDown()

    def test_constructor(self):
        elm = Element('a')
        self.assertEqual(elm.nodeName, 'A')
        self.assertEqual(elm.tagName, 'A')
        self.assertEqual(elm.localName, 'a')

        self.assertFalse(elm.hasChildNodes())
        self.assertFalse(elm.hasAttributes())

    def test_init_parent(self):
        elm = Element('a', parent=self.elm)
        self.assertIs(elm.parentNode, self.elm)

    def test_init_attrs(self):
        elm = Element('a', src='b', href='c')
        self.assertFalse(elm.hasChildNodes())
        self.assertTrue(elm.hasAttributes())
        self.assertTrue(elm.getAttribute('src'), 'b')
        self.assertTrue(elm.getAttribute('href'), 'c')

    def test_attrs(self):
        self.assertFalse(self.elm.hasAttributes())
        self.assertFalse(self.elm.hasAttribute('src'))
        self.assertNotIn('src', self.elm.attributes)
        self.assertIsNone(self.elm.getAttribute('src'))
        self.assertEqual(self.elm.html, '<tag></tag>')
        self.elm.setAttribute('src', 'a')
        self.assertTrue(self.elm.hasAttributes())
        self.assertTrue(self.elm.hasAttribute('src'))
        self.assertIn('src', self.elm.attributes)
        self.assertEqual(self.elm.getAttribute('src'), 'a')
        self.assertEqual(self.elm.html, '<tag src="a"></tag>')

        self.elm.removeAttribute('src')
        self.assertFalse(self.elm.hasAttributes())
        self.assertFalse(self.elm.hasAttribute('src'))
        self.assertNotIn('src', self.elm.attributes)
        self.assertIsNone(self.elm.getAttribute('src'))
        self.assertEqual(self.elm.html, '<tag></tag>')

    def test_id(self):
        self.assertEqual(self.elm.id, '')
        self.elm.setAttribute('id', 'a')
        self.assertEqual(self.elm.getAttribute('id'), 'a')
        self.assertEqual(self.elm.id, 'a')
        self.elm.id = 'b'
        self.assertEqual(self.elm.getAttribute('id'), 'b')
        self.assertEqual(self.elm.id, 'b')

    def test_class_list(self):
        self.assertIsNone(self.elm.getAttribute('class'))
        self.assertFalse(self.elm.hasAttribute('class'))
        self.assertFalse(self.elm.hasAttributes())

        self.elm.setAttribute('class', 'a')
        self.assertEqual(self.elm.getAttribute('class'), 'a')
        self.assertTrue(self.elm.hasAttribute('class'))
        self.assertTrue(self.elm.hasAttributes())

        self.elm.removeAttribute('class')
        self.assertIsNone(self.elm.getAttribute('class'))
        self.assertFalse(self.elm.hasAttribute('class'))
        self.assertFalse(self.elm.hasAttributes())

    def test_start_tag(self):
        self.assertEqual(self.elm.start_tag, '<tag>')
        self.elm.setAttribute('src', 'a')
        self.assertEqual(self.elm.start_tag, '<tag src="a">')

        self.elm.setAttribute('class', 'b')
        self.assertEqual(self.elm.start_tag, '<tag src="a" class="b">')

        self.elm.id = 'c'
        self.assertIn('src="a"', self.elm.start_tag)
        self.assertIn('id="c"', self.elm.start_tag)

    def test_inner_html(self):
        self.assertEqual(self.elm.innerHTML, '')
        self.elm.appendChild(Element('a'))
        self.assertEqual(self.elm.innerHTML, '<a></a>')

        self.elm.innerHTML = '<b></b>'
        self.assertEqual(self.elm.innerHTML, '<b></b>')
        self.assertEqual(self.elm.firstChild.tag, 'b')
        self.assertTrue(isinstance(self.elm.firstChild, HTMLElement))

    def test_inner_html_nest(self):
        html = '<b><c>d</c>e</b>'
        self.elm.innerHTML = html
        self.assertEqual(self.elm.innerHTML, html)
        self.assertEqual(self.elm.firstChild.html, html)
        self.assertEqual(self.elm.firstChild.firstChild.html, '<c>d</c>')
        self.assertEqual(self.elm.firstChild.firstChild.innerHTML, 'd')
        self.assertEqual(self.elm.firstChild.lastChild.html, 'e')
        self.assertTrue(isinstance(self.elm.firstChild.lastChild, Text))

    def test_parse_html_text(self):
        html = '''
        <a>a1</a1>
        b
        '''
        self.elm.innerHTML = html
        # fisrt node is empty (\n+spaces) text node
        self.assertEqual(self.elm.childNodes.length, 3)
        self.assertTrue(isinstance(self.elm.firstChild, Text))
        self.assertTrue(isinstance(self.elm.lastChild, Text))
        self.assertTrue(isinstance(self.elm.firstElementChild, HTMLElement))
        self.assertTrue(isinstance(self.elm.lastElementChild, HTMLElement))

    def test_insert_adjacent_html(self):
        self.elm.appendChild(self.c1)
        self.c1.insertAdjacentHTML('beforebegin', '<a></a>')
        self.assertEqual(self.elm.childNodes.length, 2)
        self.assertIs(self.elm.lastElementChild, self.c1)
        self.c1.insertAdjacentHTML('afterend', 'text')
        self.assertEqual(self.elm.childNodes.length, 3)
        self.assertIs(self.elm.lastElementChild, self.c1)
        self.c1.insertAdjacentHTML('afterBegin', '<b></b>')
        self.assertEqual(self.c1.childNodes.length, 1)
        self.c1.insertAdjacentHTML('BeforeEnd', '<c></c>')
        self.assertEqual(self.c1.childNodes.length, 2)
        with self.assertRaises(ValueError):
            self.c1.insertAdjacentHTML('a', 'b')

    def test_end_tag(self):
        self.assertEqual(self.elm.end_tag, '</tag>')

    def test_html(self):
        self.assertEqual(self.elm.html, '<tag></tag>')

    def test_append_string(self):
        with self.assertRaises(TypeError):
            self.elm.appendChild('a')
        self.assertFalse(self.elm.hasChildNodes())

    def test_get_elements_by_tagname(self):
        self.elm.appendChild(self.c1)
        self.elm.appendChild(self.c2)
        c1_tags = self.elm.getElementsByTagName('c1')
        c2_tags = self.elm.getElementsByTagName('c2')
        self.assertEqual(len(c1_tags), 1)
        self.assertEqual(len(c2_tags), 1)
        self.assertIs(c1_tags[0], self.c1)
        self.assertIs(c2_tags[0], self.c2)

    def test_get_elements_by_tagname_nest(self):
        self.elm.appendChild(self.c1)
        self.c1.appendChild(self.c2)
        c2_tags = self.c1.getElementsByTagName('c2')
        self.assertEqual(len(c2_tags), 1)
        self.assertIs(c2_tags[0], self.c2)
        c2_tags = self.elm.getElementsByTagName('c2')
        self.assertEqual(len(c2_tags), 1)
        self.assertIs(c2_tags[0], self.c2)

    def test_get_elements_by_tagname_self(self):
        c1_tags = self.c1.getElementsByTagName('c1')
        self.assertEqual(len(c1_tags), 0)

    def test_get_elements_by_classname(self):
        self.elm.appendChild(self.c1)
        self.elm.appendChild(self.c2)
        c1_classes = self.elm.getElementsByClassName('c1')
        c2_classes = self.elm.getElementsByClassName('c2')
        self.assertEqual(len(c1_classes), 1)
        self.assertEqual(len(c2_classes), 1)
        self.assertIs(c1_classes[0], self.c1)
        self.assertIs(c2_classes[0], self.c2)

    def test_get_elements_by_classname_nest(self):
        self.elm.appendChild(self.c1)
        self.c1.appendChild(self.c2)
        c2_classes = self.c1.getElementsByClassName('c2')
        self.assertEqual(len(c2_classes), 1)
        self.assertIs(c2_classes[0], self.c2)
        c2_classes = self.elm.getElementsByClassName('c2')
        self.assertEqual(len(c2_classes), 1)
        self.assertIs(c2_classes[0], self.c2)

    def test_clone_shallow_child(self):
        self.elm.appendChild(self.c1)
        clone = self.elm.cloneNode()
        self.assertFalse(clone.hasChildNodes())

        clone.appendChild(self.c2)
        self.assertFalse(self.c2 in self.elm)
        self.assertEqual(len(self.elm.childNodes), 1)

    def test_clone_shallow_attr(self):
        self.elm.setAttribute('src', 'a')
        clone = self.elm.cloneNode()

        self.assertTrue(clone.hasAttribute('src'))
        self.assertTrue(clone.getAttribute('src'), 'a')
        self.assertEqual(self.elm.attributes.toString(),
                         clone.attributes.toString())
        clone.setAttribute('src', 'b')
        self.assertNotEqual(self.elm.attributes.toString(),
                            clone.attributes.toString())
        self.assertTrue(self.elm.getAttribute('src'), 'a')
        self.assertTrue(clone.getAttribute('src'), 'b')

    def test_clone_deep_child(self):
        self.elm.appendChild(self.c1)
        clone = self.elm.cloneNode(deep=True)
        self.assertTrue(clone.hasChildNodes())
        self.assertEqual(len(clone.childNodes), 1)
        self.assertTrue(self.c1 in self.elm)
        self.assertFalse(self.c1 in clone)
        self.assertIsNot(self.c1, clone.firstChild)

        clone.appendChild(self.c2)
        self.assertFalse(self.c2 in self.elm)
        self.assertEqual(len(self.elm.childNodes), 1)
        self.assertEqual(len(clone.childNodes), 2)

    def test_clone_deep_attr(self):
        self.elm.setAttribute('src', 'a')
        self.elm.appendChild(self.c1)
        self.c1.setAttribute('src', 'c1')
        clone = self.elm.cloneNode(deep=True)

        self.assertTrue(clone.hasAttribute('src'))
        self.assertTrue(clone.getAttribute('src'), 'a')
        self.assertTrue(clone.firstChild.hasAttribute('src'))
        self.assertTrue(clone.firstChild.getAttribute('src'), 'c1')
        self.assertEqual(self.elm.attributes.toString(),
                         clone.attributes.toString())
        self.assertEqual(self.c1.attributes.toString(),
                         clone.firstChild.attributes.toString())

        clone.firstChild.setAttribute('src', 'b')
        self.assertNotEqual(self.c1.attributes.toString(),
                            clone.firstChild.attributes.toString())
        self.assertTrue(self.c1.getAttribute('src'), 'a')
        self.assertTrue(clone.firstChild.getAttribute('src'), 'b')

    def test_init_class(self):
        elm = Element('a', class_='a')
        self.assertEqual(elm.html, '<a class="a"></a>')
        self.assertEqual(elm.classList.length, 1)
        self.assertIn('a', elm.classList)

        elm2 = Element('a', **{'class': 'b'})
        self.assertEqual(elm2.html, '<a class="b"></a>')
        self.assertEqual(elm2.classList.length, 1)
        self.assertIn('b', elm2.classList)

    def test_init_class_multi_str(self):
        elm = Element('a', class_='a1 a2')
        self.assertEqual(elm.html, '<a class="a1 a2"></a>')
        self.assertEqual(elm.classList.length, 2)
        self.assertIn('a1', elm.classList)
        self.assertIn('a2', elm.classList)
        self.assertNotIn('a1 a2', elm.classList)

    def test_init_class_multi_list(self):
        elm = Element('a', class_=['a1', 'a2'])
        self.assertEqual(elm.html, '<a class="a1 a2"></a>')
        self.assertEqual(elm.classList.length, 2)
        self.assertIn('a1', elm.classList)
        self.assertIn('a2', elm.classList)
        self.assertNotIn('a1 a2', elm.classList)

    def test_init_class_multi_mixed(self):
        elm = Element('a', class_=['a1', 'a2 a3'])
        self.assertEqual(elm.html, '<a class="a1 a2 a3"></a>')
        self.assertEqual(elm.classList.length, 3)
        self.assertIn('a1', elm.classList)
        self.assertIn('a2', elm.classList)
        self.assertIn('a3', elm.classList)
        self.assertNotIn('a2 a3', elm.classList)

    def test_reference(self):
        gc.collect()
        elm = Element('a')
        _id = id(elm)
        self.assertIn(elm, Element._elements)
        del elm
        gc.collect()  # run gc
        for elm in Element._elements:
            assert id(elm) != _id

    def test_reference_with_id(self):
        gc.collect()
        elm = Element('a', id='a')
        _id = id(elm)
        self.assertIn(elm.id, Element._elements_with_id)
        del elm
        gc.collect()
        self.assertNotIn('a', Element._elements_with_id)
        for elm in Element._elements:
            assert id(elm) != _id

    def test_reference_add_id(self):
        gc.collect()
        elm = Element('a')
        _id = id(elm)
        self.assertNotIn(elm, Element._elements_with_id.values())
        elm.id = 'a'
        self.assertIn('a', Element._elements_with_id)
        self.assertIn(elm, Element._elements_with_id.values())
        elm.id = 'b'
        self.assertNotIn('a', Element._elements_with_id)
        self.assertIn('b', Element._elements_with_id)
        self.assertIn(elm, Element._elements_with_id.values())
        elm.setAttribute('id', 'c')
        self.assertNotIn('b', Element._elements_with_id)
        self.assertIn('c', Element._elements_with_id)
        self.assertIn(elm, Element._elements_with_id.values())
        del elm
        gc.collect()
        self.assertNotIn('c', Element._elements_with_id)
        for elm in Element._elements:
            assert id(elm) != _id

    def test_reference_del_id(self):
        gc.collect()
        elm = Element('a', id='a')
        self.assertIn('a', Element._elements_with_id)
        self.assertIn(elm, Element._elements_with_id.values())
        elm.removeAttribute('id')
        self.assertNotIn('a', Element._elements_with_id)
        self.assertNotIn(elm, Element._elements_with_id.values())

    def test_is_attr(self):
        '''``is`` is a reserved word for python, so use ``is_`` in constructor.
        '''
        elm = Element('tag', is_='elm')
        self.assertIn('is', elm.attributes)
        self.assertNotIn('is_', elm.attributes)
        self.assertEqual('elm', elm.getAttribute('is'))
        self.assertIsNone(elm.getAttribute('is_'))

        # ``is_`` is not treated as special at setAttribute
        elm.setAttribute('is_', 'new')
        self.assertEqual('elm', elm.getAttribute('is'))
        self.assertEqual('new', elm.getAttribute('is_'))

    class NewTag(HTMLElement):
        pass

    def test_custom_tag(self):
        self.elm.innerHTML = '<new-tag></new-tag>'
        child = self.elm.firstChild
        self.assertEqual(child.__class__, HTMLElement)
        customElements.define('new-tag', self.NewTag)
        self.assertEqual(child.__class__, self.NewTag)

    def test_custom_tag_registered(self):
        customElements.define('new-tag', self.NewTag)
        self.elm.innerHTML = '<new-tag></new-tag>'
        self.assertEqual(self.elm.firstChild.__class__, self.NewTag)

    def test_custom_tag_is(self):
        self.elm.innerHTML = '<a is="my-a"></a>'
        child = self.elm.firstChild
        self.assertEqual(child.__class__, HTMLElement)
        self.assertEqual(child.getAttribute('is'), 'my-a')
        customElements.define('my-a', self.NewTag, {'extends': 'a'})
        self.assertEqual(self.elm.firstChild.__class__, self.NewTag)

    def test_custom_tag_is_registered(self):
        customElements.define('my-a', self.NewTag, {'extends': 'a'})
        self.elm.innerHTML = '<a is="my-a"></a>'
        self.assertEqual(self.elm.firstChild.__class__, self.NewTag)

    def test_invalid_define_args(self):
        with self.assertRaises(TypeError):
            customElements.define(1, 2, 3)
示例#31
0
 def setUp(self):
     self.df = DocumentFragment()
     self.elm = Element('a')
     self.c1 = Element('c1')
     self.c2 = Element('c2')
     self.c3 = Element('c3')
示例#32
0
class TestDocumentFragment(TestCase):
    def setUp(self):
        self.df = DocumentFragment()
        self.elm = Element('a')
        self.c1 = Element('c1')
        self.c2 = Element('c2')
        self.c3 = Element('c3')

    def test_nodename(self):
        self.assertEqual(self.df.nodeName, '#document-fragment')

    def test_children(self):
        self.assertFalse(self.df.hasChildNodes())
        appended_child = self.df.appendChild(self.elm)
        self.assertEqual(self.df.html, '<a></a>')
        self.assertIs(appended_child, self.elm)

    def test_append_to_element(self):
        appended_child1 = self.df.appendChild(self.c1)
        appended_child2 = self.df.appendChild(self.c2)
        self.assertIs(self.df, self.c1.parentNode)
        self.assertIs(self.df, self.c2.parentNode)
        self.assertIs(appended_child1, self.c1)
        self.assertIs(appended_child2, self.c2)

        appended_df = self.elm.appendChild(self.df)
        self.assertIs(appended_df, self.df)
        self.assertEqual(self.df.length, 0)
        self.assertEqual(self.elm.length, 2)
        self.assertFalse(self.df.hasChildNodes())
        self.assertIsNone(self.df.parentNode)
        self.assertIs(self.elm, self.c1.parentNode)
        self.assertIs(self.elm, self.c2.parentNode)
        self.assertIsNone(self.df.parentNode)

    def test_insert_to_element(self):
        self.df.appendChild(self.c1)
        self.df.appendChild(self.c2)
        self.elm.appendChild(self.c3)

        inserted_node = self.elm.insertBefore(self.df, self.c3)
        self.assertIs(inserted_node, self.df)
        self.assertEqual(self.df.length, 0)
        self.assertEqual(self.elm.length, 3)
        self.assertFalse(self.df.hasChildNodes())
        self.assertIsNone(self.df.parentNode)
        self.assertIs(self.elm, self.c1.parentNode)
        self.assertIs(self.elm, self.c2.parentNode)

        self.assertIsNone(self.c1.previousSibling)
        self.assertIs(self.c1.nextSibling, self.c2)
        self.assertIs(self.c2.previousSibling, self.c1)
        self.assertIs(self.c2.nextSibling, self.c3)
        self.assertIs(self.c3.previousSibling, self.c2)
        self.assertIsNone(self.c3.nextSibling)
        self.assertIs(self.elm.firstChild, self.c1)
        self.assertIs(self.elm.lastChild, self.c3)

    def test_child(self):
        appended_child1 = self.df.appendChild(self.c1)
        appended_child2 = self.df.appendChild(self.c2)
        self.assertIs(appended_child1, self.c1)
        self.assertIs(appended_child2, self.c2)
        self.assertEqual(self.c1.html, '<c1></c1>')
        self.assertEqual(self.df.html, '<c1></c1><c2></c2>')

    def test_clone_node_sharrow(self):
        self.df.appendChild(self.c1)
        clone = self.df.cloneNode()
        self.assertEqual(self.df.length, 1)
        self.assertEqual(clone.length, 0)
        clone.appendChild(self.c2)
        self.assertEqual(self.df.length, 1)
        self.assertEqual(clone.length, 1)

    def test_clone_node_deep(self):
        self.df.appendChild(self.c1)
        clone = self.df.cloneNode(deep=True)
        self.assertEqual(self.df.length, 1)
        self.assertEqual(clone.length, 1)
        self.assertEqual(clone.html, '<c1></c1>')

        clone.appendChild(self.c2)
        self.assertEqual(self.df.length, 1)
        self.assertEqual(clone.length, 2)
        self.df.removeChild(self.c1)
        self.assertEqual(self.df.length, 0)
        self.assertEqual(clone.length, 2)