Skip to content

Commit

Permalink
Upgrade bleach (bug 638666)
Browse files Browse the repository at this point in the history
  • Loading branch information
gkoberger committed Mar 8, 2011
1 parent 0dc68ca commit a4ed2ac
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 15 deletions.
52 changes: 49 additions & 3 deletions apps/addons/tests/test_models.py
Expand Up @@ -543,24 +543,70 @@ def test_newlines_attribute_nestedquotes_singledouble(self):

eq_(self.newlines_helper(before), after)

def test_newlines_malformed_b(self):
def test_newlines_unclosed_b(self):
before = ("<b>test")
after = ("<b>test</b>")

eq_(self.newlines_helper(before), after)

def test_newlines_malformed_b_wrapped(self):
def test_newlines_unclosed_b_wrapped(self):
before = ("This is a <b>test")
after = ("This is a <b>test</b>")

eq_(self.newlines_helper(before), after)

def test_newlines_malformed_li(self):
def test_newlines_unclosed_li(self):
before = ("<ul><li>test</ul>")
after = ("<ul><li>test</li></ul>")

eq_(self.newlines_helper(before), after)

def test_newlines_malformed_faketag(self):
before = "<madonna"
after = ""

eq_(self.newlines_helper(before), after)

def test_newlines_correct_faketag(self):
before = "<madonna>"
after = "&lt;madonna&gt;"

eq_(self.newlines_helper(before), after)

def test_newlines_malformed_tag(self):
before = "<strong"
after = ""

eq_(self.newlines_helper(before), after)

def test_newlines_malformed_faketag_surrounded(self):
before = "This is a <test of bleach"
after = 'This is a &lt;test of="" bleach=""&gt;'

# Output is ugly, but not much we can do. Bleach+html5lib is adamant
# this is a tag.
eq_(self.newlines_helper(before), after)

def test_newlines_malformed_tag_surrounded(self):
before = "This is a <strong of bleach"
after = "This is a <strong></strong>"

# Bleach interprets 'of' and 'bleach' as attributes, and strips them.
# Good? No. Any way around it? Not really.
eq_(self.newlines_helper(before), after)

def test_newlines_less_than(self):
before = "3 < 5"
after = "3 &lt; 5"

eq_(self.newlines_helper(before), after)

def test_newlines_less_than_tight(self):
before = "abc 3<5 def"
after = "abc 3&lt;5 def"

eq_(self.newlines_helper(before), after)

def test_app_categories(self):
addon = lambda: Addon.objects.get(pk=3615)

Expand Down
19 changes: 7 additions & 12 deletions apps/translations/models.py
Expand Up @@ -3,7 +3,7 @@
from django.db import models, connection
from django.utils import encoding

from bleach import Bleach
import bleach
import html5lib
from html5lib.serializer.htmlserializer import HTMLSerializer

Expand All @@ -12,14 +12,6 @@
from . import utils


class MyBleach(Bleach):
def filter_url(self, url):
"""Pass auto-linked URLs through the redirector."""
return urlresolvers.get_outgoing_url(url)

bleach = MyBleach()


class Translation(amo.models.ModelBase):
"""
Translation model.
Expand Down Expand Up @@ -128,8 +120,10 @@ def __html__(self):

def clean(self):
super(PurifiedTranslation, self).clean()
string = self.clean_nl(self.localized_string)
self.localized_string_clean = bleach.bleach(string)
cleaned = bleach.clean(self.localized_string)
linkified = bleach.linkify(cleaned, nofollow=True,
filter_url=urlresolvers.get_outgoing_url)
self.localized_string_clean = self.clean_nl(linkified).strip()

def clean_nl(self, string):
""" This will clean up newlines so that nl2br can properly
Expand Down Expand Up @@ -182,7 +176,8 @@ class Meta:
proxy = True

def clean(self):
linkified = bleach.linkify(self.localized_string)
linkified = bleach.linkify(self.localized_string,
filter_url=urlresolvers.get_outgoing_url)
clean = bleach.clean(linkified, tags=['a'],
attributes={'a': ['href', 'rel']})
self.localized_string_clean = clean
Expand Down

0 comments on commit a4ed2ac

Please sign in to comment.