summaryrefslogtreecommitdiff
path: root/beautifulsoup/builder
diff options
context:
space:
mode:
Diffstat (limited to 'beautifulsoup/builder')
-rw-r--r--beautifulsoup/builder/__init__.py4
-rw-r--r--beautifulsoup/builder/lxml_builder.py15
2 files changed, 19 insertions, 0 deletions
diff --git a/beautifulsoup/builder/__init__.py b/beautifulsoup/builder/__init__.py
index cf5e6c6..5bf5929 100644
--- a/beautifulsoup/builder/__init__.py
+++ b/beautifulsoup/builder/__init__.py
@@ -25,6 +25,10 @@ class TreeBuilder(Entities):
def feed(self, markup):
raise NotImplementedError()
+ def prepare_markup(self, markup, user_specified_encoding=None,
+ document_declared_encoding=None):
+ return markup, None, None
+
def test_fragment_to_document(self, fragment):
"""Wrap an HTML fragment to make it look like a document.
diff --git a/beautifulsoup/builder/lxml_builder.py b/beautifulsoup/builder/lxml_builder.py
index 9ced9f0..a1f8c1e 100644
--- a/beautifulsoup/builder/lxml_builder.py
+++ b/beautifulsoup/builder/lxml_builder.py
@@ -1,6 +1,7 @@
from lxml import etree
from beautifulsoup.element import Comment, Doctype
from beautifulsoup.builder import HTMLTreeBuilder
+from beautifulsoup.dammit import UnicodeDammit
class LXMLTreeBuilder(HTMLTreeBuilder):
@@ -11,6 +12,20 @@ class LXMLTreeBuilder(HTMLTreeBuilder):
self.parser = parser_class(target=self)
self.soup = None
+ def prepare_markup(self, markup, user_specified_encoding=None,
+ document_declared_encoding=None):
+ """
+ :return: A 3-tuple (markup, original encoding, encoding
+ declared within markup).
+ """
+ if isinstance(markup, unicode):
+ return markup, None, None
+
+ try_encodings = [user_specified_encoding, document_declared_encoding]
+ dammit = UnicodeDammit(markup, try_encodings, isHTML=True)
+ return dammit.markup, dammit.originalEncoding, dammit.declaredHTMLEncoding
+
+
def feed(self, markup):
self.parser.feed(markup)
self.parser.close()