def html(self): header = _.header(_.h1(self.title, class_name="article__header")) content = _.section(_.p(self.content, class_name="article__content")) byline = _.section( _.p(_.i(f"By {self.author}"), class_name="article__byline") ) return _.article(header, content, byline,)
def test_render_numbers(): val = render(_.p(8)) assert val == "<p>8</p>" val = render(_.p(8.8)) assert val == "<p>8.8</p>" val = render(_.div(data_number=8)) assert val == '<div data-number="8"></div>'
def test_class_name_swapped(): """Test that class_name is swapped properly.""" val = render(_.div(class_name="myclass")) assert val == '<div class="myclass"></div>' val = render(_.p("Some Text", class_name="myclass")) assert val == '<p class="myclass">Some Text</p>'
def test_compose_elements(): """Do some simple element composing.""" my_paragraph = lambda text: _.p(text, class_name="text-content") my_header = lambda text: _.h1(text, class_name="header") assert ( _.html(my_header("Hello world!"), my_paragraph("Nice"),) .xpath("//p[@class='text-content']")[0] .text == "Nice" ) assert ( _.html(my_header("Hello world!"), my_paragraph("Nice"),) .xpath("//h1[@class='header']")[0] .text == "Hello world!" )
def simple_article(content: str, title: str = "JSX in Python"): """Creates a simple article document. From the benchmarks.""" return _.html( _.head( _.link(rel="stylesheet", type="text/css", ref="/index.css"), _.title(title), ), _.body( _.main( _.article( _.header( _.h1("Wow, it's like JSX in python!!"), _.h3("That's really cool."), ), _.p(content), ) ) ), )
def test_render_script_fails(): """Gratuitous test of the classic alert.""" value = render(_.p("<script>alert('Hello, world!')</script>")) assert value == ( "<p><script>alert('Hello, world!')</script></p>")