Example #1
0
    def test_self_closing_tag(self):
        self.assertEqual(Tag('img').render(), '<img />')

        Tag.XHTML = False
        self.assertEqual(Tag('img').render(), '<img>')

        Tag.XHTML = True  #Reset
Example #2
0
 def test_get_attribute(self):
     tag = Tag('img',
               src='http://i.imgur.com/iWHK2.gif',
               alt='Cats are liquid, not solid.')
     self.assertEqual(tag.get_attribute('src'),
                      'http://i.imgur.com/iWHK2.gif')
     self.assertEqual(tag['alt'], 'Cats are liquid, not solid.')
Example #3
0
    def test_classes_as_list(self):
        self.assertEqual(
            Tag('p', class_=['paragraph', 'bold']).render(),
            '<p class="paragraph bold"></p>')

        tag = Tag('p', class_=['paragraph'])
        tag.classes.append('bold')
        self.assertEqual(tag.render(), '<p class="paragraph bold"></p>')
Example #4
0
    def test_invalid_get_attribute(self):
        tag = Tag('img', src='http://i.imgur.com/iWHK2.gif',
                alt='Cats are liquid, not solid.')

        with self.assertRaises(KeyError):
            tag.get_attribute('href')

        with self.assertRaises(KeyError):
            tag['title']
Example #5
0
 def test_get_attribute(self):
     tag = Tag('img', src='http://i.imgur.com/iWHK2.gif',
             alt='Cats are liquid, not solid.')
     self.assertEqual(
             tag.get_attribute('src'),
             'http://i.imgur.com/iWHK2.gif')
     self.assertEqual(
             tag['alt'],
             'Cats are liquid, not solid.')
Example #6
0
    def test_invalid_get_attribute(self):
        tag = Tag('img',
                  src='http://i.imgur.com/iWHK2.gif',
                  alt='Cats are liquid, not solid.')

        with self.assertRaises(KeyError):
            tag.get_attribute('href')

        with self.assertRaises(KeyError):
            tag['title']
Example #7
0
    def test_boss(self):
        field = Tag('input', type_='text', value='*****@*****.**',
                placeholder='Required', id_='my_field')
        label = Tag('label', ['Email: ', field], for_='my_field')
        soup = BeautifulSoup(label.render())

        self.assertNotEqual(soup.label, None)
        self.assertNotEqual(soup.label.input, None)
        self.assertEqual(soup.label['for'], 'my_field')
        self.assertEqual(soup.label.get_text(), 'Email: ')
Example #8
0
    def test_basic_tag(self):
        self.assertEqual(Tag('p').render(), '<p></p>')

        self.assertEqual(Tag('DIV').render(), '<div></div>')

        with self.assertRaises(ValueError):
            Tag('div`').render()

        #Make sure the HTML alias also works.
        self.assertEqual(Tag('span').__html__(), '<span></span>')
Example #9
0
    def test_classes_as_list(self):
        self.assertEqual(
                Tag('p', class_=['paragraph', 'bold']).render(),
                '<p class="paragraph bold"></p>')

        tag = Tag('p', class_=['paragraph'])
        tag.classes.append('bold')
        self.assertEqual(
                tag.render(),
                '<p class="paragraph bold"></p>')
Example #10
0
    def test_set_attributes(self):
        #And then the set_attributes method
        tag = Tag('img')
        tag.set_attributes({
            'src': 'http://i.imgur.com/iWHK2.gif',
            'alt': 'Cats are liquid, not solid.'
        })
        soup = BeautifulSoup(tag.render())

        self.assertEqual(soup.img.name, 'img')
        self.assertEqual(soup.img['src'], 'http://i.imgur.com/iWHK2.gif')
        self.assertEqual(soup.img['alt'], 'Cats are liquid, not solid.')
Example #11
0
    def test_set_attributes(self):
        #And then the set_attributes method
        tag = Tag('img')
        tag.set_attributes({
            'src': 'http://i.imgur.com/iWHK2.gif',
            'alt': 'Cats are liquid, not solid.'
            })
        soup = BeautifulSoup(tag.render())

        self.assertEqual(soup.img.name, 'img')
        self.assertEqual(soup.img['src'], 'http://i.imgur.com/iWHK2.gif')
        self.assertEqual(soup.img['alt'], 'Cats are liquid, not solid.')
Example #12
0
    def test_boss(self):
        field = Tag('input',
                    type_='text',
                    value='*****@*****.**',
                    placeholder='Required',
                    id_='my_field')
        label = Tag('label', ['Email: ', field], for_='my_field')
        soup = BeautifulSoup(label.render())

        self.assertNotEqual(soup.label, None)
        self.assertNotEqual(soup.label.input, None)
        self.assertEqual(soup.label['for'], 'my_field')
        self.assertEqual(soup.label.get_text(), 'Email: ')
Example #13
0
 def prerender(self):
     """Return a Tag or TagContent object."""
     tag = Tag('input', type_='text', name=self.name, value=self.value,
             id=self._id)
     tag.set_attributes(self.attributes)
     if self.label:
         if self.label.align == 'left':
             args = [self.label.label(self), ' ', tag]
         else:
             args = [tag, ' ', self.label.label(self)]
         tag = TagContent(*args)
     if self.wrapper:
         tag = self.wrapper.wrap(tag, self)
     return tag
Example #14
0
    def test_init_attributes(self):
        self.assertEqual(
                Tag('img', SRC='http://i.imgur.com/iWHK2.gif').render(),
                '<img src="http://i.imgur.com/iWHK2.gif" />')

        #Now we need to start parsing the HTML, because the attributes are
        #stored in a dict, the order of which is not consistant across all
        #platforms and implementations.
        tag = Tag('img', src='http://i.imgur.com/iWHK2.gif',
                alt='Cats are liquid, not solid.')
        soup = BeautifulSoup(tag.render())

        self.assertEqual(soup.img.name, 'img')
        self.assertEqual(soup.img['src'], 'http://i.imgur.com/iWHK2.gif')
        self.assertEqual(soup.img['alt'], 'Cats are liquid, not solid.')
Example #15
0
    def test_init_attributes(self):
        self.assertEqual(
            Tag('img', SRC='http://i.imgur.com/iWHK2.gif').render(),
            '<img src="http://i.imgur.com/iWHK2.gif" />')

        #Now we need to start parsing the HTML, because the attributes are
        #stored in a dict, the order of which is not consistant across all
        #platforms and implementations.
        tag = Tag('img',
                  src='http://i.imgur.com/iWHK2.gif',
                  alt='Cats are liquid, not solid.')
        soup = BeautifulSoup(tag.render())

        self.assertEqual(soup.img.name, 'img')
        self.assertEqual(soup.img['src'], 'http://i.imgur.com/iWHK2.gif')
        self.assertEqual(soup.img['alt'], 'Cats are liquid, not solid.')
Example #16
0
 def prerender(self):
     """Return a Tag or TagContent object."""
     tag = Tag('input',
               type_='text',
               name=self.name,
               value=self.value,
               id=self._id)
     tag.set_attributes(self.attributes)
     if self.label:
         if self.label.align == 'left':
             args = [self.label.label(self), ' ', tag]
         else:
             args = [tag, ' ', self.label.label(self)]
         tag = TagContent(*args)
     if self.wrapper:
         tag = self.wrapper.wrap(tag, self)
     return tag
Example #17
0
    def test_set_attribute(self):
        #Now we'll repeat the test, using the set_attribute method.
        tag = Tag('img')
        tag.set_attribute('src', 'http://i.imgur.com/iWHK2.gif')
        tag.set_attribute('alt', 'Cats are liquid, not solid.')
        soup = BeautifulSoup(tag.render())

        self.assertEqual(soup.img.name, 'img')
        self.assertEqual(soup.img['src'], 'http://i.imgur.com/iWHK2.gif')
        self.assertEqual(soup.img['alt'], 'Cats are liquid, not solid.')
Example #18
0
    def test_delete_attribute(self):
        tag = Tag('img',
                  src='http://i.imgur.com/iWHK2.gif',
                  alt='Cats are liquid, not solid.')
        tag.delete_attribute('alt')
        self.assertEqual(tag.render(),
                         '<img src="http://i.imgur.com/iWHK2.gif" />')

        tag = Tag('img',
                  src='http://i.imgur.com/iWHK2.gif',
                  alt='Cats are liquid, not solid.')
        del tag['alt']
        self.assertEqual(tag.render(),
                         '<img src="http://i.imgur.com/iWHK2.gif" />')
Example #19
0
    def test_set_attribute(self):
        #Now we'll repeat the test, using the set_attribute method.
        tag = Tag('img')
        tag.set_attribute('src', 'http://i.imgur.com/iWHK2.gif')
        tag.set_attribute('alt', 'Cats are liquid, not solid.')
        soup = BeautifulSoup(tag.render())

        self.assertEqual(soup.img.name, 'img')
        self.assertEqual(soup.img['src'], 'http://i.imgur.com/iWHK2.gif')
        self.assertEqual(soup.img['alt'], 'Cats are liquid, not solid.')
Example #20
0
    def test_delete_attribute(self):
        tag = Tag('img', src='http://i.imgur.com/iWHK2.gif',
                alt='Cats are liquid, not solid.')
        tag.delete_attribute('alt')
        self.assertEqual(
                tag.render(),
                '<img src="http://i.imgur.com/iWHK2.gif" />')

        tag = Tag('img', src='http://i.imgur.com/iWHK2.gif',
                alt='Cats are liquid, not solid.')
        del tag['alt']
        self.assertEqual(
                tag.render(),
                '<img src="http://i.imgur.com/iWHK2.gif" />')
Example #21
0
 def test_set_attribute_shorthand(self):
     tag = Tag('img')
     tag['src'] = 'http://i.imgur.com/iWHK2.gif'
     self.assertEqual(
             tag.render(),
             '<img src="http://i.imgur.com/iWHK2.gif" />')
Example #22
0
 def wrap(self, tag, field):
     if self.align == 'left':
         args = [self.text, ' ', tag]
     else:
         args = [tag, ' ', self.text]
     return Tag('label', TagContent(*args), for_=field.id)
Example #23
0
 def test_invalid_attributes(self):
     tag = Tag('img')
     with self.assertRaises(ValueError):
         tag.set_attribute('src`', 'http://i.imgur.com/iWHK2.gif')
Example #24
0
 def test_tag_with_tagcontent(self):
     self.assertEqual(
         Tag('p', TagContent('Hello world')).render(), '<p>Hello world</p>')
Example #25
0
 def test_escaping_attribute_values(self):
     self.assertEqual(
         Tag('a', href='<">').render(), '<a href="<&quot;>"></a>')
Example #26
0
 def test_set_attribute_shorthand(self):
     tag = Tag('img')
     tag['src'] = 'http://i.imgur.com/iWHK2.gif'
     self.assertEqual(tag.render(),
                      '<img src="http://i.imgur.com/iWHK2.gif" />')
Example #27
0
 def label(self, field):
     return Tag('label', self.text, for_=field.id)
Example #28
0
 def test_classes_as_string_and_list(self):
     tag = Tag('p', class_='paragraph bold')
     tag.classes.append('italic')
     self.assertEqual(
             tag.render(),
             '<p class="paragraph bold italic"></p>')
Example #29
0
 def test_classes_as_string(self):
     self.assertEqual(
         Tag('p', class_='paragraph bold').render(),
         '<p class="paragraph bold"></p>')
Example #30
0
 def test_invalid_name_attribute(self):
     with self.assertRaises(ValueError):
         Tag('p', name='#paragraph')
Example #31
0
File: text.py Project: luhn/formula
 def prerender(self):
     """Return a Tag or TagContent object."""
     tag = Tag('input', type_='text', name=self.name, value=self.value,
             id=self.id)
     tag.set_attributes(self.attributes)
     return tag
Example #32
0
 def test_set_attribute_as_none(self):
     self.assertEqual(Tag('p', color=None).render(), '<p></p>')
Example #33
0
 def test_classes_as_string_and_list(self):
     tag = Tag('p', class_='paragraph bold')
     tag.classes.append('italic')
     self.assertEqual(tag.render(), '<p class="paragraph bold italic"></p>')
Example #34
0
 def test_invalid_attributes(self):
     tag = Tag('img')
     with self.assertRaises(ValueError):
         tag.set_attribute('src`', 'http://i.imgur.com/iWHK2.gif')
Example #35
0
 def test_invalid_class_name(self):
     with self.assertRaises(ValueError):
         Tag('p', class_='-h`')
Example #36
0
 def test_non_string_attributes(self):
     self.assertEqual(Tag('p', title=15).render(), '<p title="15"></p>')
Example #37
0
 def test_tag_with_string(self):
     self.assertEqual(
         Tag('p', 'Hello world!').render(), '<p>Hello world!</p>')
Example #38
0
 def test_id_attribute(self):
     self.assertEqual(
         Tag('p', id_='paragraph').render(), '<p id="paragraph"></p>')
Example #39
0
 def test_tag_with_list(self):
     self.assertEqual(
         Tag('p', ['Hello ', Tag('span', 'w'), 'orld.']).render(),
         '<p>Hello <span>w</span>orld.</p>')