Skip to content

Commit

Permalink
Tests ignored classes and fixes a bug with those classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
BertrandBordage committed Jun 29, 2013
1 parent 4ee0f32 commit 99829f0
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 25 deletions.
25 changes: 15 additions & 10 deletions terms/html.py
Expand Up @@ -17,9 +17,15 @@ def build_ignored_regexp(l):

TAGS_REGEXP = build_ignored_regexp(TERMS_IGNORED_TAGS)
CLASSES_REGEXP = build_ignored_regexp(TERMS_IGNORED_CLASSES)
CLASSES_REGEXP__match = CLASSES_REGEXP.match
IDS_REGEXP = build_ignored_regexp(TERMS_IGNORED_IDS)
IDS_REGEXP__match = IDS_REGEXP.match


def valid_class(class_):
return class_ is None or CLASSES_REGEXP.match(class_) is not None


def valid_id(id_):
return id_ is None or IDS_REGEXP.match(id_) is not None


if TERMS_REPLACE_FIRST_ONLY:
Expand Down Expand Up @@ -52,16 +58,15 @@ def is_navigable_string(navigable_string):
def html_content_iterator(parent_tag, replace_regexp):
if not parent_tag.find(text=replace_regexp):
return
for tag in parent_tag.find_all(name=TAGS_REGEXP, recursive=False):
for tag in parent_tag.find_all(
name=TAGS_REGEXP, class_=valid_class, id=valid_id,
recursive=False):
if not tag.find(text=replace_regexp):
continue
class_ = tag.class_
if (not class_ or CLASSES_REGEXP__match(class_)) \
and (not tag.id or IDS_REGEXP__match(tag.id)):
if tag.find(text=replace_regexp, recursive=False):
yield tag
for sub_tag in html_content_iterator(tag, replace_regexp):
yield sub_tag
if tag.find(text=replace_regexp, recursive=False):
yield tag
for sub_tag in html_content_iterator(tag, replace_regexp):
yield sub_tag


def str_to_soup(html):
Expand Down
35 changes: 28 additions & 7 deletions terms/tests/terms/1_after.html
@@ -1,7 +1,28 @@
<p>
This is <span><em>quite</em> a
<a href="{{ term.url }}">complicated term</a></span>
</p>
<p>
Oh, you really think <strong>it's a complicated term?</strong>
</p>
<!DOCTYPE html>
<html>
<head>
<title>What a complicated term!</title>
<script>
alert('But that is such a complicated term…');
</script>
</head>
<body>
<div>
<code>
Since we are in a &lt;code&gt;,
a complicated term should not be replaced.
</code>
<div class="cms_reset">
cms_reset is in TERMS_IGNORED_CLASSES, so a complicated term should
be ignored here.
</div>
<p>
This is <span><em>quite</em> a
<a href="{{ term.url }}">complicated term</a></span>
</p>
<p>
Oh, you really think <strong>it's a complicated term?</strong>
</p>
</div>
</body>
</html>
33 changes: 27 additions & 6 deletions terms/tests/terms/1_before.html
@@ -1,6 +1,27 @@
<p>
This is <span><em>quite</em> a complicated term</span>
</p>
<p>
Oh, you really think <strong>it's a complicated term?</strong>
</p>
<!DOCTYPE html>
<html>
<head>
<title>What a complicated term!</title>
<script>
alert('But that is such a complicated term…');
</script>
</head>
<body>
<div>
<code>
Since we are in a &lt;code&gt;,
a complicated term should not be replaced.
</code>
<div class="cms_reset">
cms_reset is in TERMS_IGNORED_CLASSES, so a complicated term should
be ignored here.
</div>
<p>
This is <span><em>quite</em> a complicated term</span>
</p>
<p>
Oh, you really think <strong>it's a complicated term?</strong>
</p>
</div>
</body>
</html>
14 changes: 12 additions & 2 deletions terms/tests/terms/__init__.py
@@ -1,8 +1,9 @@
import os
from django.core.urlresolvers import reverse
from django.template import Template, Context
from django.test import TestCase
from terms.templatetags.terms import replace_terms
from terms.models import Term
from terms.templatetags.terms import replace_terms


CURRENT_PATH = os.path.abspath(os.path.dirname(__file__))
Expand Down Expand Up @@ -31,7 +32,10 @@ def setUp(self):
definition='A nice mix between a rhino and a giraffe.')

def assertDetailView(self, term, status_code=200):
response = self.client.get(term.get_absolute_url())
self.assertURL(term.get_absolute_url(), status_code=status_code)

def assertURL(self, url, status_code=200):
response = self.client.get(url)
self.assertEqual(response.status_code, status_code)

def assertCachedRegex(self):
Expand All @@ -47,6 +51,7 @@ def assertCachedRegex(self):
replace_terms(read_file(before_template))

def test1(self):
self.maxDiff = 800
self.assertHTMLEqual(
replace_terms(read_file('1_before.html')),
read_file('1_after.html', {'term': self.term1}))
Expand All @@ -61,3 +66,8 @@ def test2(self):
self.assertDetailView(self.term2)

self.assertCachedRegex()

def testAdminRendering(self):
for term in Term.objects.all():
self.assertURL(
reverse('admin:terms_term_change', args=(term.pk,)))

0 comments on commit 99829f0

Please sign in to comment.