def test_form_save_section(self): d, r = doc_rev(""" <h1 id="s1">s1</h1> <p>test</p> <p>test</p> <h1 id="s2">s2</h1> <p>test</p> <p>test</p> <h1 id="s3">s3</h1> <p>test</p> <p>test</p> """) replace_content = """ <h1 id="s2">New stuff</h1> <p>new stuff</p> """ expected = """ <h1 id="s1">s1</h1> <p>test</p> <p>test</p> <h1 id="s2">New stuff</h1> <p>new stuff</p> <h1 id="s3">s3</h1> <p>test</p> <p>test</p> """ rev_form = RevisionForm({"content": replace_content}, instance=r, section_id="s2") new_rev = rev_form.save(r.creator, d) eq_(normalize_html(expected), normalize_html(new_rev.content))
def test_form_onload_attr_filter(self): """RevisionForm should strip out any harmful onload attributes from input markup""" d, r = doc_rev(""" <svg><circle onload=confirm(3)> """) rev_form = RevisionForm(instance=r) ok_('onload' not in rev_form.initial['content'])
def test_form_loaded_with_section(self): """RevisionForm given section_id should load initial content for only one section""" d, r = doc_rev(""" <h1 id="s1">s1</h1> <p>test</p> <p>test</p> <h1 id="s2">s2</h1> <p>test</p> <p>test</p> <h1 id="s3">s3</h1> <p>test</p> <p>test</p> """) expected = """ <h1 id="s2">s2</h1> <p>test</p> <p>test</p> """ rev_form = RevisionForm(instance=r, section_id="s2") eq_(normalize_html(expected), normalize_html(rev_form.initial['content']))
def test_link_annotation(self): d, r = doc_rev("This document exists") d.save() r.save() document(title=u'Héritée', locale=u'fr', slug=u'CSS/Héritage', save=True) document(title=u'DOM/StyleSheet', locale=u'en-US', slug=u'DOM/StyleSheet', save=True) base_url = u'https://testserver' vars = dict( base_url=base_url, exist_url=d.get_absolute_url(), exist_url_with_base=urljoin(base_url, d.get_absolute_url()), uilocale_url=u'/en-US/docs/%s/%s' % (d.locale, d.slug), noexist_url=u'/en-US/docs/no-such-doc', noexist_url_with_base=urljoin(base_url, u'/en-US/docs/no-such-doc'), noexist_uilocale_url=u'/en-US/docs/en-US/blah-blah-blah', nonen_slug='/fr/docs/CSS/H%c3%a9ritage', tag_url='/en-US/docs/tag/foo', feed_url='/en-US/docs/feeds/atom/all', templates_url='/en-US/docs/templates', ) doc_src = u""" <li><a href="%(nonen_slug)s">Héritée</a></li> <li><a href="%(exist_url)s">This doc should exist</a></li> <li><a href="%(exist_url)s#withanchor">This doc should exist</a></li> <li><a href="%(exist_url_with_base)s">This doc should exist</a></li> <li><a href="%(exist_url_with_base)s#withanchor">This doc should exist</a></li> <li><a href="%(uilocale_url)s">This doc should exist</a></li> <li><a class="foobar" href="%(exist_url)s">This doc should exist, and its class should be left alone.</a></li> <li><a href="%(noexist_url)s#withanchor">This doc should NOT exist</a></li> <li><a href="%(noexist_url)s">This doc should NOT exist</a></li> <li><a href="%(noexist_url_with_base)s">This doc should NOT exist</a></li> <li><a href="%(noexist_url_with_base)s#withanchor">This doc should NOT exist</a></li> <li><a href="%(noexist_uilocale_url)s">This doc should NOT exist</a></li> <li><a class="foobar" href="%(noexist_url)s">This doc should NOT exist, and its class should be altered</a></li> <li><a href="http://mozilla.org/">This is an external link</a></li> <li><a class="foobar" name="quux">A lack of href should not cause a problem.</a></li> <li><a>In fact, a "link" with no attributes should be no problem as well.</a></li> <a href="%(tag_url)s">Tag link</a> <a href="%(feed_url)s">Feed link</a> <a href="%(templates_url)s">Templates link</a> <a href="/en-US/docs/DOM/stylesheet">Case sensitive 1</a> <a href="/en-US/docs/DOM/Stylesheet">Case sensitive 1</a> <a href="/en-US/docs/DOM/StyleSheet">Case sensitive 1</a> <a href="/en-us/docs/dom/StyleSheet">Case sensitive 1</a> <a href="/en-US/docs/dom/Styles">For good measure</a> """ % vars expected = u""" <li><a href="%(nonen_slug)s">Héritée</a></li> <li><a href="%(exist_url)s">This doc should exist</a></li> <li><a href="%(exist_url)s#withanchor">This doc should exist</a></li> <li><a href="%(exist_url_with_base)s">This doc should exist</a></li> <li><a href="%(exist_url_with_base)s#withanchor">This doc should exist</a></li> <li><a href="%(uilocale_url)s">This doc should exist</a></li> <li><a class="foobar" href="%(exist_url)s">This doc should exist, and its class should be left alone.</a></li> <li><a class="new" href="%(noexist_url)s#withanchor">This doc should NOT exist</a></li> <li><a class="new" href="%(noexist_url)s">This doc should NOT exist</a></li> <li><a class="new" href="%(noexist_url_with_base)s">This doc should NOT exist</a></li> <li><a class="new" href="%(noexist_url_with_base)s#withanchor">This doc should NOT exist</a></li> <li><a class="new" href="%(noexist_uilocale_url)s">This doc should NOT exist</a></li> <li><a class="foobar new" href="%(noexist_url)s">This doc should NOT exist, and its class should be altered</a></li> <li><a class="external" href="http://mozilla.org/">This is an external link</a></li> <li><a class="foobar" name="quux">A lack of href should not cause a problem.</a></li> <li><a>In fact, a "link" with no attributes should be no problem as well.</a></li> <a href="%(tag_url)s">Tag link</a> <a href="%(feed_url)s">Feed link</a> <a href="%(templates_url)s">Templates link</a> <a href="/en-US/docs/DOM/stylesheet">Case sensitive 1</a> <a href="/en-US/docs/DOM/Stylesheet">Case sensitive 1</a> <a href="/en-US/docs/DOM/StyleSheet">Case sensitive 1</a> <a href="/en-us/docs/dom/StyleSheet">Case sensitive 1</a> <a class="new" href="/en-US/docs/dom/Styles">For good measure</a> """ % vars # Split the markup into lines, to better see failures doc_lines = doc_src.strip().split("\n") expected_lines = expected.strip().split("\n") for idx in range(0, len(doc_lines)): doc_line = doc_lines[idx] expected_line = expected_lines[idx] result_line = (kuma.wiki.content.parse(doc_line) .annotateLinks(base_url=vars['base_url']) .serialize()) eq_(normalize_html(expected_line), normalize_html(result_line))