Xqt has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1209061?usp=email )

Change subject: Fix PagesTagParser: Support attributes with multiple words
......................................................................

Fix PagesTagParser: Support attributes with multiple words

Also add test for the related issue.

Bug: T383506
Change-Id: Ie495012d43b6a6dbd2070999cf23d94bd79dafe4
---
M pywikibot/proofreadpage.py
M tests/proofreadpage_tests.py
2 files changed, 46 insertions(+), 1 deletion(-)

Approvals:
  Xqt: Verified; Looks good to me, approved




diff --git a/pywikibot/proofreadpage.py b/pywikibot/proofreadpage.py
index c0181b7..99ba949 100644
--- a/pywikibot/proofreadpage.py
+++ b/pywikibot/proofreadpage.py
@@ -141,6 +141,11 @@
             if (value.startswith('"') != value.endswith('"')
                     or value.startswith("'") != value.endswith("'")):
                 raise ValueError(f'{value=!s} has wrong quotes.')
+
+            # Add quotes if value contains spaces and is not already quoted
+            if ' ' in value and not value.startswith(('"', "'")):
+                self._orig_value = json.dumps(value, ensure_ascii=False)
+
             value = value.strip('"\'')
             value = int(value) if value.isdigit() else value

@@ -325,7 +330,7 @@
             attr, _, value = attribute.partition('=')
             if attr == 'from':
                 attr = 'f' + attr
-            setattr(self, attr, value)
+            setattr(self, attr, value.strip())

     @classmethod
     def get_descriptors(cls):
diff --git a/tests/proofreadpage_tests.py b/tests/proofreadpage_tests.py
index 7b7ec44..88245bf 100755
--- a/tests/proofreadpage_tests.py
+++ b/tests/proofreadpage_tests.py
@@ -73,6 +73,41 @@
         self.assertEqual(str(attr), "fromsection='A123'")
         self.assertEqual(attr.value, 'A123')

+    def test_tag_attr_str_with_spaces(self) -> None:
+        """Test TagAttr for str value with spaces."""
+        attr = TagAttr('index', 'Sample index with more than two words.pdf')
+        self.assertEqual(
+            repr(attr),
+            "TagAttr('index', '\"Sample index with more than two words.pdf\"')"
+        )
+        self.assertEqual(
+            str(attr), 'index="Sample index with more than two words.pdf"')
+        self.assertEqual(
+            attr.value, 'Sample index with more than two words.pdf')
+
+    def test_tag_attr_str_with_multiple_spaces(self) -> None:
+        """Test TagAttr for str value with multiple spaces."""
+        attr = TagAttr('index', 'Sample index with   multiple   spaces.pdf')
+        self.assertEqual(
+            repr(attr),
+            "TagAttr('index', '\"Sample index with   multiple   spaces.pdf\"')"
+        )
+        self.assertEqual(
+            str(attr), 'index="Sample index with   multiple   spaces.pdf"')
+        self.assertEqual(
+            attr.value, 'Sample index with   multiple   spaces.pdf')
+
+        tag = PagesTagParser()
+        tag.index = 'Sample index with more than two words.pdf'
+        tag.ffrom = 5
+        tag.to = '6'
+        tag.fromsection = '"chapter XVI"'
+        self.assertEqual(
+            str(tag),
+            '<pages index="Sample index with more than two words.pdf" '
+            'from=5 to=6 fromsection="chapter XVI" />'
+        )
+
     def test_tag_attr_exceptions(self) -> None:
         """Test TagAttr for Exceptions."""
         with self.assertRaisesRegex(ValueError, 'has wrong quotes'):
@@ -110,6 +145,11 @@
         self.assertEqual(str(tp), """<pages from=1 to='3' step=3 />""")
         self.assertIn('step', tp)

+        text = """Text: <pages index="Index.pdf"   from="1" />"""
+        tp = PagesTagParser(text)
+        self.assertEqual(tp.index, 'Index.pdf')
+        self.assertEqual(tp.ffrom, 1)
+
     def test_pages_tag_parser_exceptions(self) -> None:
         """Test PagesTagParser Exceptions."""
         text = """Text: <pages index="Index.pdf" />"""

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1209061?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.wikimedia.org/r/settings?usp=email

Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Ie495012d43b6a6dbd2070999cf23d94bd79dafe4
Gerrit-Change-Number: 1209061
Gerrit-PatchSet: 15
Gerrit-Owner: Dumbledore <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
Gerrit-CC: Ignacio Rodríguez <[email protected]>
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to