Remove support for beautifulsoup 3. ref #664
This commit is contained in:
@@ -11,27 +11,15 @@ class BeautifulSoupParser(ParserBase):
|
|||||||
super(BeautifulSoupParser, self).__init__(content)
|
super(BeautifulSoupParser, self).__init__(content)
|
||||||
try:
|
try:
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
self.use_bs4 = True
|
|
||||||
self.soup = BeautifulSoup(self.content, "html.parser")
|
self.soup = BeautifulSoup(self.content, "html.parser")
|
||||||
except ImportError:
|
except ImportError:
|
||||||
try:
|
raise ImproperlyConfigured("Error while importing BeautifulSoup: %s" % err)
|
||||||
from BeautifulSoup import BeautifulSoup
|
|
||||||
self.use_bs4 = False
|
|
||||||
self.soup = BeautifulSoup(self.content)
|
|
||||||
except ImportError as err:
|
|
||||||
raise ImproperlyConfigured("Error while importing BeautifulSoup: %s" % err)
|
|
||||||
|
|
||||||
def css_elems(self):
|
def css_elems(self):
|
||||||
if self.use_bs4:
|
return self.soup.find_all({'link': True, 'style': True})
|
||||||
return self.soup.find_all({'link': True, 'style': True})
|
|
||||||
else:
|
|
||||||
return self.soup.findAll({'link': True, 'style': True})
|
|
||||||
|
|
||||||
def js_elems(self):
|
def js_elems(self):
|
||||||
if self.use_bs4:
|
return self.soup.find_all('script')
|
||||||
return self.soup.find_all('script')
|
|
||||||
else:
|
|
||||||
return self.soup.findAll('script')
|
|
||||||
|
|
||||||
def elem_attribs(self, elem):
|
def elem_attribs(self, elem):
|
||||||
attrs = dict(elem.attrs)
|
attrs = dict(elem.attrs)
|
||||||
|
|||||||
@@ -4,12 +4,7 @@ import re
|
|||||||
from tempfile import mkdtemp
|
from tempfile import mkdtemp
|
||||||
from shutil import rmtree, copytree
|
from shutil import rmtree, copytree
|
||||||
|
|
||||||
try:
|
from bs4 import BeautifulSoup
|
||||||
from bs4 import BeautifulSoup
|
|
||||||
use_bs4 = True
|
|
||||||
except ImportError:
|
|
||||||
from BeautifulSoup import BeautifulSoup
|
|
||||||
use_bs4 = False
|
|
||||||
|
|
||||||
from django.core.cache.backends import locmem
|
from django.core.cache.backends import locmem
|
||||||
from django.test import SimpleTestCase
|
from django.test import SimpleTestCase
|
||||||
@@ -26,17 +21,7 @@ from compressor.storage import DefaultStorage
|
|||||||
|
|
||||||
|
|
||||||
def make_soup(markup):
|
def make_soup(markup):
|
||||||
if use_bs4:
|
return BeautifulSoup(markup, "html.parser")
|
||||||
return BeautifulSoup(markup, "html.parser")
|
|
||||||
else:
|
|
||||||
return BeautifulSoup(markup)
|
|
||||||
|
|
||||||
|
|
||||||
def soup_find_all(markup, name):
|
|
||||||
if use_bs4:
|
|
||||||
return make_soup(markup).find_all(name)
|
|
||||||
else:
|
|
||||||
return make_soup(markup).findAll(name)
|
|
||||||
|
|
||||||
|
|
||||||
def css_tag(href, **kwargs):
|
def css_tag(href, **kwargs):
|
||||||
@@ -302,7 +287,7 @@ class CssMediaTestCase(SimpleTestCase):
|
|||||||
|
|
||||||
def test_css_output(self):
|
def test_css_output(self):
|
||||||
css_node = CssCompressor(self.css)
|
css_node = CssCompressor(self.css)
|
||||||
links = soup_find_all(css_node.output(), 'link')
|
links = make_soup(css_node.output()).find_all('link')
|
||||||
media = ['screen', 'print', 'all', None]
|
media = ['screen', 'print', 'all', None]
|
||||||
self.assertEqual(len(links), 4)
|
self.assertEqual(len(links), 4)
|
||||||
self.assertEqual(media, [l.get('media', None) for l in links])
|
self.assertEqual(media, [l.get('media', None) for l in links])
|
||||||
@@ -311,7 +296,7 @@ class CssMediaTestCase(SimpleTestCase):
|
|||||||
css = self.css + '<style type="text/css" media="print">p { border:10px solid red;}</style>'
|
css = self.css + '<style type="text/css" media="print">p { border:10px solid red;}</style>'
|
||||||
css_node = CssCompressor(css)
|
css_node = CssCompressor(css)
|
||||||
media = ['screen', 'print', 'all', None, 'print']
|
media = ['screen', 'print', 'all', None, 'print']
|
||||||
links = soup_find_all(css_node.output(), 'link')
|
links = make_soup(css_node.output()).find_all('link')
|
||||||
self.assertEqual(media, [l.get('media', None) for l in links])
|
self.assertEqual(media, [l.get('media', None) for l in links])
|
||||||
|
|
||||||
@override_settings(COMPRESS_PRECOMPILERS=(
|
@override_settings(COMPRESS_PRECOMPILERS=(
|
||||||
@@ -323,7 +308,7 @@ class CssMediaTestCase(SimpleTestCase):
|
|||||||
<link rel="stylesheet" href="/static/css/two.css" type="text/css" media="screen">
|
<link rel="stylesheet" href="/static/css/two.css" type="text/css" media="screen">
|
||||||
<style type="text/foobar" media="screen">h1 { border:5px solid green;}</style>"""
|
<style type="text/foobar" media="screen">h1 { border:5px solid green;}</style>"""
|
||||||
css_node = CssCompressor(css)
|
css_node = CssCompressor(css)
|
||||||
output = soup_find_all(css_node.output(), ['link', 'style'])
|
output = make_soup(css_node.output()).find_all(['link', 'style'])
|
||||||
self.assertEqual(['/static/css/one.css', '/static/css/two.css', None],
|
self.assertEqual(['/static/css/one.css', '/static/css/two.css', None],
|
||||||
[l.get('href', None) for l in output])
|
[l.get('href', None) for l in output])
|
||||||
self.assertEqual(['screen', 'screen', 'screen'],
|
self.assertEqual(['screen', 'screen', 'screen'],
|
||||||
@@ -363,11 +348,8 @@ class JsAsyncDeferTestCase(SimpleTestCase):
|
|||||||
return 'defer'
|
return 'defer'
|
||||||
js_node = JsCompressor(self.js)
|
js_node = JsCompressor(self.js)
|
||||||
output = [None, 'async', 'defer', None, 'async', None]
|
output = [None, 'async', 'defer', None, 'async', None]
|
||||||
scripts = soup_find_all(js_node.output(), 'script')
|
scripts = make_soup(js_node.output()).find_all('script')
|
||||||
if use_bs4:
|
attrs = [extract_attr(s) for s in scripts]
|
||||||
attrs = [extract_attr(i) for i in scripts]
|
|
||||||
else:
|
|
||||||
attrs = [s.get('async') or s.get('defer') for s in scripts]
|
|
||||||
self.assertEqual(output, attrs)
|
self.assertEqual(output, attrs)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user