def test_lxml_attr_is_consistent(): body_text = html.div( html.p(u"foo")).encode('utf-8') agent = TestAgent(wz.Response([body_text])) page = agent.get(u'/') div_element = page.one('//div') assert page.lxml == div_element.lxml
def test_html_returns_unicode(): body_text = html.div( html.p(u"£")).encode('utf-8') agent = TestAgent(wz.Response([body_text])) page = agent.get(u'/') assert page.html() == body_text.decode('utf-8') assert page.html('utf-8') == body_text
def test_unicode_chars(): body_text = html.div( html.p(u"£")).encode('utf-8') agent = TestAgent(wz.Response([body_text])) page = agent.get(u'/') assert page.body == body_text assert tostring(page.lxml) == body_text assert page.html().encode('utf-8') == body_text div_element = page.one('//div') assert div_element.html().encode('utf-8') == body_text assert tostring(div_element.lxml) == body_text
def html(self, **kwargs): if not self.children: return '' if self.value: # 判断是否有参数,不判断会有AttributeError生成 direction = self.value.strip() else: direction = 'left' # 默认左浮 if not self.html_float.match(direction): style = '<!-- XSS removed -->' else: style = 'bbcode-float-' + direction return html_builder.div(self.children_html(**dict(kwargs, br=False)), class_=style)
def test_tables(): header_values = ["foo", "bar", "baz"] table_text = html.div( html.table( html.thead( html.tr( *[html.th(html.span(i+" ")) for i in header_values])), html.tbody( html.tr( *[html.td(i) for i in [1, 2, 3]]), html.tr( *[html.td(i) for i in [4, 5, 6]]))), html.table( html.thead( html.tr( *[html.th(html.span(i+" ")) for i in header_values])), html.tbody( html.tr( *[html.td(i) for i in [1, 2, 3]]), html.tr( *[html.td(i) for i in [4, 5, 6]])))) agent = TestAgent(wz.Response([table_text])).get(u'/') table = agent.all(u"//table")[0] rows = [row.to_dict() for row in table.rows()] headers = table.headers() assert len(headers) == 3 assert headers == header_values assert len(rows) == 2 for i, row in enumerate(rows): for j, header in enumerate(header_values): index = (i * 3) + (j + 1) assert row[header] == str(index) assert row[header] == type(row[header])(index) for j, cell in enumerate(row.values()): index = (i * 3) + (j + 1) assert cell == str(index) lists = [ ['1', '2', '3'], [4, 5, 6], ] for row, l in zip(table.rows(), lists): row.assert_is(l) assert_raises( AssertionError, table.rows()[0].assert_is, ['flim', 'flam', 'flooble'] )
def test_form_getitem(): form_text = html.div( html.p( html.input(type="text", name="foo", value="flam")), html.p( html.select( html.option(value="a", selected=True), html.option(value="b"), name="bar")) ) form_page = TestAgent(FormApp(form_text)).get(u'/') form = form_page.one(u'//form') assert form['foo'] == "flam" assert form['bar'] == "a" form["foo"] = u"flibble" form["bar"] = u"a" assert form.one(u'//input').value == u'flibble' assert form.one(u'//select').value == u'a' # Test checkboxes form_page = TestAgent(TestApp()).get('/form-checkbox') form = form_page.one('//form') assert form['a'] == [] assert form['b'] == ["A"]
def html(self, **kwargs): if not self.children: return '' return html_builder.div(self.children_html(**dict(kwargs, br=False)), class_="bbcode-indent")