summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--beautifulsoup/dammit.py24
-rw-r--r--beautifulsoup/element.py7
2 files changed, 18 insertions, 13 deletions
diff --git a/beautifulsoup/dammit.py b/beautifulsoup/dammit.py
index 31dfa95..bff87c6 100644
--- a/beautifulsoup/dammit.py
+++ b/beautifulsoup/dammit.py
@@ -61,17 +61,20 @@ class EntitySubstitution(object):
"&(?!#\d+;|#x[0-9a-fA-F]+;|\w+;)"
")")
- def _substitute_html_entity(self, matchobj):
- entity = self.CHARACTER_TO_HTML_ENTITY.get(matchobj.group(0))
+ @classmethod
+ def _substitute_html_entity(cls, matchobj):
+ entity = cls.CHARACTER_TO_HTML_ENTITY.get(matchobj.group(0))
return "&%s;" % entity
- def _substitute_xml_entity(self, matchobj):
+ @classmethod
+ def _substitute_xml_entity(cls, matchobj):
"""Used with a regular expression to substitute the
appropriate XML entity for an XML special character."""
- entity = self.CHARACTER_TO_XML_ENTITY[matchobj.group(0)]
+ entity = cls.CHARACTER_TO_XML_ENTITY[matchobj.group(0)]
return "&%s;" % entity
- def substitute_xml(self, value, make_quoted_attribute=False):
+ @classmethod
+ def substitute_xml(cls, value, make_quoted_attribute=False):
"""Substitute XML entities for special XML characters.
:param value: A string to be substituted. The less-than sign will
@@ -117,14 +120,15 @@ class EntitySubstitution(object):
# Escape angle brackets, and ampersands that aren't part of
# entities.
- value = self.BARE_AMPERSAND_OR_BRACKET.sub(
- self._substitute_xml_entity, value)
+ value = cls.BARE_AMPERSAND_OR_BRACKET.sub(
+ cls._substitute_xml_entity, value)
if make_quoted_attribute:
return quote_with + value + quote_with
else:
return value
- def substitute_html(self, s):
+ @classmethod
+ def substitute_html(cls, s):
"""Replace certain Unicode characters with named HTML entities.
This differs from data.encode(encoding, 'xmlcharrefreplace')
@@ -135,8 +139,8 @@ class EntitySubstitution(object):
character with "é" will make it more readable to some
people.
"""
- return self.CHARACTER_TO_HTML_ENTITY_RE.sub(
- self._substitute_html_entity, s)
+ return cls.CHARACTER_TO_HTML_ENTITY_RE.sub(
+ cls._substitute_html_entity, s)
class UnicodeDammit:
diff --git a/beautifulsoup/element.py b/beautifulsoup/element.py
index 6af27a8..61ed4ab 100644
--- a/beautifulsoup/element.py
+++ b/beautifulsoup/element.py
@@ -11,7 +11,7 @@ from util import isList
DEFAULT_OUTPUT_ENCODING = "utf-8"
-class PageElement(EntitySubstitution):
+class PageElement(object):
"""Contains the navigational information for some part of the page
(either a tag or a piece of text)"""
@@ -363,7 +363,7 @@ class NavigableString(unicode, PageElement):
def output_ready(self, substitute_html_entities=False):
if substitute_html_entities:
- output = self.substitute_html(self)
+ output = EntitySubstitution.substitute_html(self)
else:
output = self
return self.PREFIX + output + self.SUFFIX
@@ -580,7 +580,8 @@ class Tag(PageElement):
and '%SOUP-ENCODING%' in val):
val = self.substituteEncoding(val, eventual_encoding)
- decoded = key + '=' + self.substitute_xml(val, True)
+ decoded = (key + '='
+ + EntitySubstitution.substitute_xml(val, True))
attrs.append(decoded)
close = ''
closeTag = ''