Snippet List
My admin allows editing of some html fields using TinyMCE, so I end up with horrible code that contains lots of nested `<p>`, `<div>`, `<span>` tags, and style properties which destroy my layout and consistence.
This tag based on lxml tries to kill as much unneeded tags as possible, and style properties. These properties can be customized by adapting the regex to your needs.
- html
- wysiwyg
- tinymce
- lxml
- dirty
- cleanup
If you have this as your base class for all unit tests you can do the following:
class TestViews(BaseTestCase):
def test_generated_stats(self):
"test that certain stuff in the response"
...create some content for testing or use fixtures...
response = self.client.get('/some/page/')
# At this point response.content is a huge string filled with HTML tags and
# "junk" that you don't need for testing the content thus making it difficult
# to debug the generated HTML because it so huge.
# So we can zoom in on the <div id="stats>...</div> node
html = self._zoom_html(response.content, '#stats')
# the variable 'html' will now be something like this:
"""
<div id="stats">
<p>
<strong>2</strong> students<br/>
<em>1</em> warning.
</p>
</div>
"""
# This makes it easier to debug the response and easier to test
# against but the HTML might still be in the way so this would fail:
self.assertTrue('2 students' in html) # will fail
# To strip away all html use _strip_html()
content = self._strip_html(html)
# Now this will work
self.assertTrue('2 students' in content) # will work
It works for me and I find this very useful so I thought I'd share it.
- css
- test
- client
- lxml
- lxml.html
2 snippets posted so far.