def test_extract_strings(self): template, strings = extract_strings(""" <p><b>Bread</b>\xa0is a\xa0<a href="https://en.wikipedia.org/wiki/Staple_food">staple food</a>\xa0prepared from a\xa0<a href="https://en.wikipedia.org/wiki/Dough">dough</a>\xa0of\xa0<a href="https://en.wikipedia.org/wiki/Flour">flour</a>\xa0and\xa0<a href="https://en.wikipedia.org/wiki/Water">water</a>, usually by\xa0<a href="https://en.wikipedia.org/wiki/Baking">baking</a>. Throughout recorded history it has been popular around the world and is one of the oldest artificial foods, having been of importance since the dawn of\xa0<a href="https://en.wikipedia.org/wiki/Agriculture#History">agriculture</a>.</p> <p>Proportions of types of flour and other ingredients vary widely, as do modes of preparation. As a result, types, shapes, sizes, and textures of breads differ around the world. Bread may be\xa0<a href="https://en.wikipedia.org/wiki/Leaven">leavened</a>\xa0by processes such as reliance on naturally occurring\xa0<a href="https://en.wikipedia.org/wiki/Sourdough">sourdough</a>\xa0microbes, chemicals, industrially produced yeast, or high-pressure aeration. Some bread is cooked before it can leaven, including for traditional or religious reasons. Non-cereal ingredients such as fruits, nuts and fats may be included. Commercial bread commonly contains additives to improve flavor, texture, color, shelf life, and ease of manufacturing.</p> """) self.assertHTMLEqual( template, """ <p><text position="0"></text></p> <p><text position="1"></text></p> """, ) self.assertEqual( strings, [ StringValue.from_html( '<b>Bread</b>\xa0is a\xa0<a href="https://en.wikipedia.org/wiki/Staple_food">staple food</a>\xa0prepared from a\xa0<a href="https://en.wikipedia.org/wiki/Dough">dough</a>\xa0of\xa0<a href="https://en.wikipedia.org/wiki/Flour">flour</a>\xa0and\xa0<a href="https://en.wikipedia.org/wiki/Water">water</a>, usually by\xa0<a href="https://en.wikipedia.org/wiki/Baking">baking</a>. Throughout recorded history it has been popular around the world and is one of the oldest artificial foods, having been of importance since the dawn of\xa0<a href="https://en.wikipedia.org/wiki/Agriculture#History">agriculture</a>.' ), StringValue.from_html( 'Proportions of types of flour and other ingredients vary widely, as do modes of preparation. As a result, types, shapes, sizes, and textures of breads differ around the world. Bread may be\xa0<a href="https://en.wikipedia.org/wiki/Leaven">leavened</a>\xa0by processes such as reliance on naturally occurring\xa0<a href="https://en.wikipedia.org/wiki/Sourdough">sourdough</a>\xa0microbes, chemicals, industrially produced yeast, or high-pressure aeration. Some bread is cooked before it can leaven, including for traditional or religious reasons. Non-cereal ingredients such as fruits, nuts and fats may be included. Commercial bread commonly contains additives to improve flavor, texture, color, shelf life, and ease of manufacturing.' ), ], )
def test_block_tag_in_inline_tag(self): # If an inline tag contains a block tag. The inline tag must be in the template. # Testing for issue https://github.com/mozilla/donate-wagtail/issues/586 template, strings = extract_strings("<p><i>Foo <p>Bar</p></i></p>") self.assertHTMLEqual( template, '<p><i><text position="0"></text> <p><text position="1"></text></p></i></p>', ) self.assertEqual( strings, [StringValue.from_html("Foo"), StringValue.from_html("Bar")])
def test_string_from_html(self): string, attrs = StringValue.from_html( '<b>Bread</b>\xa0is a\xa0<a href="https://en.wikipedia.org/wiki/Staple_food">staple food</a>\xa0prepared from a\xa0<a href="https://en.wikipedia.org/wiki/Dough">dough</a>\xa0of\xa0<a href="https://en.wikipedia.org/wiki/Flour">flour</a>\xa0and\xa0<a href="https://en.wikipedia.org/wiki/Water">water</a>' ) self.assertEqual( string.data, '<b>Bread</b> is a <a id="a1">staple food</a> prepared from a <a id="a2">dough</a> of <a id="a3">flour</a> and <a id="a4">water</a>', ) self.assertEqual( attrs, { 'a1': { 'href': 'https://en.wikipedia.org/wiki/Staple_food' }, 'a2': { 'href': 'https://en.wikipedia.org/wiki/Dough' }, 'a3': { 'href': 'https://en.wikipedia.org/wiki/Flour' }, 'a4': { 'href': 'https://en.wikipedia.org/wiki/Water' }, })
def test_empty_inline_tag(self): template, strings = extract_strings("<p><i></i>Foo</p>") self.assertHTMLEqual(template, '<p><i></i><text position="0"></text></p>') self.assertEqual(strings, [StringValue.from_html("Foo")])
def test_br_tag_is_removed_when_it_appears_at_end_of_segment(self): template, strings = extract_strings("<p><i>Foo</i><br/></p>") self.assertHTMLEqual(template, '<p><i><text position="0"></text></i><br/></p>') self.assertEqual(strings, [StringValue.from_html("Foo")])
def test_br_tag_is_treated_as_inline_tag(self): template, strings = extract_strings( "<p><b>Foo <i>Bar<br/>Baz</i></b></p>") self.assertHTMLEqual(template, '<p><b><text position="0"></text></b></p>') self.assertEqual(strings, [StringValue.from_html("Foo <i>Bar<br/>Baz</i>")])
def test_extract_strings_2(self): template, strings = extract_strings(""" <h1>Foo bar baz</h1> <p>This is a paragraph. <b>This is some bold <i>and now italic</i></b> text</p> <p><script> this should be interpreted as text.</p> <ul> <li>List item one</li> <li><b>List item two</li> </ul> <img src="foo" alt="This bit isn't translatable"> """) self.assertHTMLEqual( template, """ <h1><text position="0"></text></h1> <p><text position="1"></text></p> <p><text position="2"></text></p> <ul> <li><text position="3"></text></li> <li><b><text position="4"></text></b></li> </ul> <img alt="This bit isn\'t translatable" src="foo"> """, ) self.assertEqual( strings, [ StringValue.from_html("Foo bar baz"), StringValue.from_html( "This is a paragraph. <b>This is some bold <i>and now italic</i></b> text" ), StringValue.from_html( "<script> this should be interpreted as text."), StringValue.from_html("List item one"), StringValue.from_html("List item two"), ], )