Fixed LxmlParser. Closes #102.
This commit is contained in:
@@ -9,23 +9,27 @@ from compressor.utils.decorators import cached_property
|
|||||||
|
|
||||||
class LxmlParser(ParserBase):
|
class LxmlParser(ParserBase):
|
||||||
|
|
||||||
@cached_property
|
def __init__(self, content):
|
||||||
def tree(self):
|
|
||||||
content = '<root>%s</root>' % self.content
|
|
||||||
try:
|
try:
|
||||||
from lxml.html import fromstring, soupparser
|
from lxml.html import fromstring, soupparser
|
||||||
from lxml.etree import tostring
|
from lxml.etree import tostring
|
||||||
|
self.fromstring = fromstring
|
||||||
|
self.soupparser = soupparser
|
||||||
self.tostring = tostring
|
self.tostring = tostring
|
||||||
tree = fromstring(content)
|
|
||||||
try:
|
|
||||||
ignore = tostring(tree, encoding=unicode)
|
|
||||||
except UnicodeDecodeError:
|
|
||||||
tree = soupparser.fromstring(content)
|
|
||||||
except ImportError, err:
|
except ImportError, err:
|
||||||
raise ImproperlyConfigured("Error while importing lxml: %s" % err)
|
raise ImproperlyConfigured("Error while importing lxml: %s" % err)
|
||||||
except Exception, err:
|
except Exception, err:
|
||||||
raise ParserError("Error while initializing Parser: %s" % err)
|
raise ParserError("Error while initializing Parser: %s" % err)
|
||||||
else:
|
super(LxmlParser, self).__init__(content)
|
||||||
|
|
||||||
|
@cached_property
|
||||||
|
def tree(self):
|
||||||
|
content = '<root>%s</root>' % self.content
|
||||||
|
tree = self.fromstring(content)
|
||||||
|
try:
|
||||||
|
ignore = self.tostring(tree, encoding=unicode)
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
tree = self.soupparser.fromstring(content)
|
||||||
return tree
|
return tree
|
||||||
|
|
||||||
def css_elems(self):
|
def css_elems(self):
|
||||||
|
Reference in New Issue
Block a user