@@ -420,6 +420,45 @@ def test_input_xhtml(self):
420420
421421# common markdown test cases
422422class MarkdownTests :
423+ def mangleMarkdown2 (self , s ):
424+ ''' markdown2's rel=nofollow support on 'a' tags isn't programmable.
425+ So we are using it's builtin nofollow support. Mangle the string
426+ so that it matches the test case.
427+
428+ turn: <a rel="nofollow" href="foo"> into
429+ <a href="foo" rel="nofollow noopener">
430+
431+ Also if it is a mailto url, we don't expect rel="nofollow",
432+ so delete it.
433+
434+ turn: <a rel="nofollow" href="mailto:foo"> into
435+ <a href="mailto:foo">
436+
437+ Also when a title is present it is put in a different place
438+ from markdown, so fix it to normalize.
439+
440+ turn:
441+
442+ <a rel="nofollow" href="http://example.com/" title="a title">
443+ into
444+ <a href="http://example.com/" rel="nofollow noopener" title="a title">
445+ '''
446+ if type (self ) == Markdown2TestCase and s .find ('a rel="nofollow"' ) != - 1 :
447+ if s .find ('href="mailto:' ) == - 1 :
448+ # not a mailto url
449+ if 'rel="nofollow"' in s :
450+ if 'title="' in s :
451+ s = s .replace (' rel="nofollow" ' , ' ' ).replace (' title=' , ' rel="nofollow noopener" title=' )
452+ else :
453+ s = s .replace (' rel="nofollow" ' , ' ' ).replace ('">' , '" rel="nofollow noopener">' )
454+
455+ return s
456+ else :
457+ # a mailto url
458+ return s .replace (' rel="nofollow" ' , ' ' )
459+ return s
460+
461+
423462 def test_string_markdown (self ):
424463 p = StringHTMLProperty (self .client , 'test' , '1' , None , 'test' , u2s (u'A string with <br> *embedded* \u00df ' ))
425464 self .assertEqual (p .markdown ().strip (), u2s (u'<p>A string with <br> <em>embedded</em> \u00df </p>' ))
@@ -437,7 +476,10 @@ def test_string_markdown_link(self):
437476 html_unescape = HTMLParser ().unescape
438477
439478 p = StringHTMLProperty (
self .
client ,
'test' ,
'1' ,
None ,
'test' ,
u2s (
u'A link <[email protected] >' ))
440- self .
assertEqual (
html_unescape (
p .
markdown ().
strip ()),
u2s (
u'<p>A link <a href="mailto:[email protected] ">[email protected] </a></p>' ))
479+ m = html_unescape (p .markdown ().strip ())
480+ m = self .mangleMarkdown2 (m )
481+
482+ self .
assertEqual (
m ,
u2s (
u'<p>A link <a href="mailto:[email protected] ">[email protected] </a></p>' ))
441483
442484 def test_string_markdown_javascript_link (self ):
443485 # make sure we don't get a "javascript:" link
@@ -457,6 +499,7 @@ def test_string_markdown_forced_line_break(self):
457499 # Rather than using a different result for each
458500 # renderer, look for '<br' and require three of them.
459501 m = p .markdown ()
502+ print (m )
460503 self .assertEqual (3 , m .count ('<br' ))
461504
462505 def test_string_markdown_code_block (self ):
@@ -498,14 +541,23 @@ def test_markdown_hyperlinked_url(self):
498541 # so rstrip \n.
499542 p = StringHTMLProperty (self .client , 'test' , '1' , None , 'test' , u2s (u'http://example.com/' ))
500543 m = p .markdown (hyperlink = 1 )
501- self .assertEqual (m .rstrip ('\n ' ), '<p><a href="http://example.com/">http://example.com/</a></p>' )
544+ m = self .mangleMarkdown2 (m )
545+ print (m )
546+ self .assertEqual (m .rstrip ('\n ' ), '<p><a href="http://example.com/" rel="nofollow noopener">http://example.com/</a></p>' )
502547
503548 p = StringHTMLProperty (self .client , 'test' , '1' , None , 'test' , u2s (u'<http://example.com/>' ))
504549 m = p .markdown (hyperlink = 1 )
505- self .assertEqual (m .rstrip ('\n ' ), '<p><a href="http://example.com/">http://example.com/</a></p>' )
550+ m = self .mangleMarkdown2 (m )
551+ self .assertEqual (m .rstrip ('\n ' ), '<p><a href="http://example.com/" rel="nofollow noopener">http://example.com/</a></p>' )
552+
553+ p = StringHTMLProperty (self .client , 'test' , '1' , None , 'test' , u2s (u'[label](http://example.com/ "a title")' ))
554+ m = p .markdown (hyperlink = 1 )
555+ m = self .mangleMarkdown2 (m )
556+ self .assertEqual (m .rstrip ('\n ' ), '<p><a href="http://example.com/" rel="nofollow noopener" title="a title">label</a></p>' )
506557
507558 p = StringHTMLProperty (self .client , 'test' , '1' , None , 'test' , u2s (u'' ))
508559 m = p .markdown (hyperlink = 1 )
560+ m = self .mangleMarkdown2 (m )
509561 self .assertIn (m , [
510562 '<p><img src="http://example.com/" alt=""/></p>\n ' ,
511563 '<p><img src="http://example.com/" alt="" /></p>\n ' ,
@@ -515,11 +567,13 @@ def test_markdown_hyperlinked_url(self):
515567
516568 p = StringHTMLProperty (self .client , 'test' , '1' , None , 'test' , u2s (u'An URL http://example.com/ with text' ))
517569 m = p .markdown (hyperlink = 1 )
518- self .assertEqual (m .rstrip ('\n ' ), '<p>An URL <a href="http://example.com/">http://example.com/</a> with text</p>' )
570+ m = self .mangleMarkdown2 (m )
571+ self .assertEqual (m .rstrip ('\n ' ), '<p>An URL <a href="http://example.com/" rel="nofollow noopener">http://example.com/</a> with text</p>' )
519572
520573 p = StringHTMLProperty (self .client , 'test' , '1' , None , 'test' , u2s (u'An URL https://example.com/path with text' ))
521574 m = p .markdown (hyperlink = 1 )
522- self .assertEqual (m .rstrip ('\n ' ), '<p>An URL <a href="https://example.com/path">https://example.com/path</a> with text</p>' )
575+ m = self .mangleMarkdown2 (m )
576+ self .assertEqual (m .rstrip ('\n ' ), '<p>An URL <a href="https://example.com/path" rel="nofollow noopener">https://example.com/path</a> with text</p>' )
523577
524578@skip_mistune
525579class MistuneTestCase (TemplatingTestCase , MarkdownTests ) :
0 commit comments