diff --git a/mwparserfromhell/compat.py b/mwparserfromhell/compat.py index bb81513..864605c 100644 --- a/mwparserfromhell/compat.py +++ b/mwparserfromhell/compat.py @@ -15,14 +15,12 @@ py3k = sys.version_info[0] == 3 if py3k: bytes = bytes str = str - basestring = str maxsize = sys.maxsize import html.entities as htmlentities else: bytes = str str = unicode - basestring = basestring maxsize = sys.maxint import htmlentitydefs as htmlentities diff --git a/mwparserfromhell/nodes/extras/attribute.py b/mwparserfromhell/nodes/extras/attribute.py index ad282af..05860a0 100644 --- a/mwparserfromhell/nodes/extras/attribute.py +++ b/mwparserfromhell/nodes/extras/attribute.py @@ -48,7 +48,7 @@ class Attribute(StringMixIn): def __unicode__(self): base = self.pad_first + str(self.name) + self.pad_before_eq - if self.value: + if self.value is not None: if self.quoted: return base + '="' + self.pad_after_eq + str(self.value) + '"' return base + "=" + self.pad_after_eq + str(self.value) @@ -100,7 +100,7 @@ class Attribute(StringMixIn): @value.setter def value(self, newval): - self._value = parse_anything(newval) + self._value = None if newval is None else parse_anything(newval) @quoted.setter def quoted(self, value): diff --git a/mwparserfromhell/nodes/template.py b/mwparserfromhell/nodes/template.py index c326b65..a6b1665 100644 --- a/mwparserfromhell/nodes/template.py +++ b/mwparserfromhell/nodes/template.py @@ -26,7 +26,7 @@ import re from . import HTMLEntity, Node, Text from .extras import Parameter -from ..compat import basestring, str +from ..compat import str from ..utils import parse_anything __all__ = ["Template"] @@ -84,7 +84,7 @@ class Template(Node): replacement = str(HTMLEntity(value=ord(char))) for node in code.filter_text(recursive=False): if char in node: - code.replace(node, node.replace(char, replacement)) + code.replace(node, node.replace(char, replacement), False) def _blank_param_value(self, value): """Remove the content from *value* while keeping its whitespace. @@ -170,9 +170,9 @@ class Template(Node): With *ignore_empty*, ``False`` will be returned even if the template contains a parameter with the name *name*, if the parameter's value is empty. Note that a template may have multiple parameters with the - same name. + same name, but only the last one is read by the MediaWiki parser. """ - name = name.strip() if isinstance(name, basestring) else str(name) + name = str(name).strip() for param in self.params: if param.name.strip() == name: if ignore_empty and not param.value.strip(): @@ -191,7 +191,7 @@ class Template(Node): parameters can have the same name, we'll return the last match, since the last parameter is the only one read by the MediaWiki parser. """ - name = name.strip() if isinstance(name, basestring) else str(name) + name = str(name).strip() for param in reversed(self.params): if param.name.strip() == name: return param @@ -294,7 +294,7 @@ class Template(Node): the first instance if none have dependents, otherwise the one with dependents will be kept). """ - name = name.strip() if isinstance(name, basestring) else str(name) + name = str(name).strip() removed = False to_remove = [] for i, param in enumerate(self.params): diff --git a/mwparserfromhell/parser/tokens.py b/mwparserfromhell/parser/tokens.py index 8c2ea87..0ffac86 100644 --- a/mwparserfromhell/parser/tokens.py +++ b/mwparserfromhell/parser/tokens.py @@ -30,7 +30,7 @@ into the :py:class`~.Wikicode` tree by the :py:class:`~.Builder`. from __future__ import unicode_literals -from ..compat import basestring, py3k +from ..compat import py3k, str __all__ = ["Token"] @@ -43,7 +43,7 @@ class Token(object): def __repr__(self): args = [] for key, value in self._kwargs.items(): - if isinstance(value, basestring) and len(value) > 100: + if isinstance(value, str) and len(value) > 100: args.append(key + "=" + repr(value[:97] + "...")) else: args.append(key + "=" + repr(value)) diff --git a/tests/test_attribute.py b/tests/test_attribute.py index 8dd84cb..dbf3145 100644 --- a/tests/test_attribute.py +++ b/tests/test_attribute.py @@ -40,6 +40,8 @@ class TestAttribute(TreeEqualityTestCase): self.assertEqual(' foo="bar"', str(node2)) node3 = Attribute(wraptext("a"), wraptext("b"), False, "", " ", " ") self.assertEqual("a = b", str(node3)) + node4 = Attribute(wraptext("a"), wrap([]), False, " ", "", " ") + self.assertEqual(" a= ", str(node4)) def test_name(self): """test getter/setter for the name attribute""" @@ -56,6 +58,8 @@ class TestAttribute(TreeEqualityTestCase): self.assertIs(value, node.value) node.value = "{{bar}}" self.assertWikicodeEqual(wrap([Template(wraptext("bar"))]), node.value) + node.value = None + self.assertIs(None, node.value) def test_quoted(self): """test getter/setter for the quoted attribute"""