from django.test import TestCase
from unittest import expectedFailure

from feincms_cleanse import Cleanse


class CleanseTestCase(TestCase):
    def run_tests(self, entries, klass=Cleanse):
        for before, after in entries:
            after = before if after is None else after
            result = klass().cleanse(before)
            self.assertEqual(result, after, u"Cleaning '%s', expected '%s' but got '%s'" % (before, after, result))

    def test_01_cleanse(self):
        entries = [
            (u'<p>&nbsp;</p>', u''),
            (u'<p>           </p>', u''),
            (u'<span style="font-weight: bold;">Something</span><p></p>',
                u'<strong>Something</strong>'),
            (u'<p>abc <span>def <em>ghi</em> jkl</span> mno</p>',
                u'<p>abc def <em>ghi</em> jkl mno</p>'),
            (u'<span style="font-style: italic;">Something</span><p></p>',
                u'<em>Something</em>'),
            (u'<p>abc<br />def</p>', u'<p>abc<br />def</p>'),
            ]

        self.run_tests(entries)

    @expectedFailure
    def test_02_a_tag(self):
        entries = (
                    ('<a href="/foo">foo</a>', None),
                    ('<a href="/foo" target="some" name="bar" title="baz" cookies="yesplease">foo</a>', '<a href="/foo" target="some" name="bar" title="baz">foo</a>'),
                    ('<a href="http://somewhere.else">foo</a>', None),
                    ('<a href="https://somewhere.else">foo</a>', None),
                    ('<a href="javascript:alert()">foo</a>', '<a href="">foo</a>'),
                    ('<a href="javascript%3Aalert()">foo</a>', '<a href="">foo</a>'),
                  )

        self.run_tests(entries)

    def test_03_merge(self):
        entries = (
                   ('<h2>foo</h2><h2>bar</h2>', '<h2>foo bar</h2>'),
                   ('<h2>foo  </h2>   <h2>   bar</h2>', '<h2>foo bar</h2>'),
                  )

        self.run_tests(entries)

    @expectedFailure
    def test_04_p_in_li(self):
        entries = (
                   ('<li><p>foo</p></li>', '<li>foo</li>'),
                   ('<li>&nbsp;<p>foo</p> &#160; </li>', '<li>foo</li>'),
                   ('<li>foo<p>bar</p>baz</li>', '<li>foo bar baz</li>'),
                  )

        self.run_tests(entries)

    @expectedFailure
    def test_05_p_in_p(self):
        entries = (
                   (u'<p><p><p>&nbsp;</p> </p><p><br /></p></p>', u' '),
                   ('<p>foo<p>bar</p>baz</p>', '<p>foo bar baz</p>'),
                  )

        self.run_tests(entries)

    @expectedFailure
    def test_06_whitelist(self):
        entries = (
                   (u'<script src="http://abc">foo</script>', u''),
                   (u'<script type="text/javascript">foo</script>', u''),
                  )

        self.run_tests(entries)

    @expectedFailure
    def test_07_configuration(self):
        class MyCleanse(Cleanse):
            allowed_tags = { 'h1': (), 'h2': () }

        entries = (
                   ('<h1>foo</h1>', None),
                   ('<h1>foo</h1><h2>bar</h2><h3>baz</h3>', '<h1>foo</h1><h2>bar</h2>baz'),
                  )

        self.run_tests(entries, klass=MyCleanse)

    def test_08_li_with_marker(self):
        entries = (
                   ('<li> - foo</li>', '<li>foo</li>'),
                   ('<li>* foo</li>', '<li>foo</li>'),
                  )

        self.run_tests(entries)

@@ -1947,37 +1947,16 @@
 tries)%0A%0A
-    @expectedFailure%0A
     def 
@@ -2038,35 +2038,37 @@
 /p%3E%3C/li%3E', '%3Cli%3E
+ 
 foo
+ 
 %3C/li%3E'),%0A       
@@ -2114,35 +2114,41 @@
 0; %3C/li%3E', '%3Cli%3E
+  
 foo
+    
 %3C/li%3E'),%0A       
@@ -2179,38 +2179,131 @@
 bar%3C
-/p%3Ebaz%3C/li%3E', '%3Cli%3Efoo bar baz
+strong%3Exx%3C/strong%3Erab%3C/p%3E%3Cstrong%3Ebaz%3C/strong%3Ea%3Cp%3Eb%3C/p%3Ec%3C/li%3E', '%3Cli%3Efoo bar %3Cstrong%3Exx%3C/strong%3Erab%3Cstrong%3Ebaz%3C/strong%3Ea b c
 %3C/li
@@ -2361,37 +2361,16 @@
 tries)%0A%0A
-    @expectedFailure%0A
     def 
@@ -2403,32 +2403,88 @@
     entries = (%0A
+                   ('%3Cp%3E%3Cp%3Efoo%3C/p%3E%3C/p%3E', '%3Cp%3Efoo%3C/p%3E'),%0A
                 
@@ -2536,24 +2536,242 @@
 p%3E', u' '),%0A
+                   # This is actually correct as the second %3Cp%3E implicitely%0A                   # closes the first paragraph, and the trailing %3C/p%3E is%0A                   # deleted because it has no matching opening %3Cp%3E%0A
             
@@ -2816,20 +2816,25 @@
 %3Efoo
- bar baz
+%3C/p%3E%3Cp%3Ebar
 %3C/p%3E
+baz
 '),%0A

Correct some tests. We should really have a more lenient way to compare results wrt white space.