Repository: facelessuser/soupsieve

Issue:
HTML detection for `type` selector not working correctly
Fix issue noted here: https://github.com/MechanicalSoup/MechanicalSoup/pull/263.

We were checking against the html namespace for type case insensitivity instead of whether the document was XML or not.


Discussion:

Patch:
diff --git a/docs/src/markdown/about/changelog.md b/docs/src/markdown/about/changelog.md
index 78afda9..3afed4f 100644
--- a/docs/src/markdown/about/changelog.md
+++ b/docs/src/markdown/about/changelog.md
@@ -2,6 +2,7 @@
 
 ## 1.7.2
 
+- **FIX**: Fix HTML detection `type` selector.
 - **FIX**: Provide a way for Beautiful Soup to parse selectors in a quirks mode to mimic some of the quirks of the old
   select method prior to Soup Sieve, but with warnings. This is to help old scripts to not break during the transitional
   period with newest Beautiful Soup. In the future, these quirks will raise an exception as Soup Sieve requires
diff --git a/soupsieve/css_match.py b/soupsieve/css_match.py
index 6a9b485..d1a5c35 100644
--- a/soupsieve/css_match.py
+++ b/soupsieve/css_match.py
@@ -547,7 +547,7 @@ class CSSMatch(Document, object):
         if attributes:
             for a in attributes:
                 value = self.match_attribute_name(el, a.attribute, a.prefix)
-                pattern = a.xml_type_pattern if not self.html_namespace and a.xml_type_pattern else a.pattern
+                pattern = a.xml_type_pattern if self.is_xml and a.xml_type_pattern else a.pattern
                 if isinstance(value, list):
                     value = ' '.join(value)
                 if value is None:


Test patch:
diff --git a/tests/test_level2.py b/tests/test_level2.py
index b83b4cf..f8a5a6a 100644
--- a/tests/test_level2.py
+++ b/tests/test_level2.py
@@ -337,6 +337,20 @@ class TestLevel2(util.TestCase):
             flags=util.HTML5
         )
 
+        self.assert_selector(
+            markup,
+            '[type="test"]',
+            ["0", '2'],
+            flags=util.HTML
+        )
+
+        self.assert_selector(
+            markup,
+            '[type="test"]',
+            ["0", '2'],
+            flags=util.PYHTML
+        )
+
         self.assert_selector(
             markup,
             '[type="test"]',
@@ -344,6 +358,35 @@ class TestLevel2(util.TestCase):
             flags=util.XML
         )
 
+        markup = """
+        <?xml version="1.0" encoding="UTF-8"?>
+        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
+            "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
+        <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
+        <head>
+        </head>
+        <body>
+        <div id="div">
+        <p type="TEST" id="0">Some text <span id="1"> in a paragraph</span>.</p>
+        <a type="test" id="2" href="http://google.com">Link</a>
+        <span id="3">Direct child</span>
+        <pre id="pre">
+        <span id="4">Child 1</span>
+        <span id="5">Child 2</span>
+        <span id="6">Child 3</span>
+        </pre>
+        </div>
+        </body>
+        </html>
+        """
+
+        self.assert_selector(
+            markup,
+            '[type="test"]',
+            ["0", '2'],
+            flags=util.XML
+        )
+
     def test_attribute_start_dash(self):
         """Test attribute whose dash separated value starts with the specified value."""
 
diff --git a/tests/util.py b/tests/util.py
index 0734a95..958873a 100644
--- a/tests/util.py
+++ b/tests/util.py
@@ -12,6 +12,7 @@ HTML5 = 1
 HTML = 2
 XHTML = 4
 XML = 8
+PYHTML = 10
 
 
 def skip_quirks(func):
@@ -66,7 +67,9 @@ class TestCase(unittest.TestCase):
         """Assert selector."""
 
         mode = flags & 0x0F
-        if mode == HTML:
+        if mode == PYHTML:
+            bs_mode = 'html.parser'
+        elif mode == HTML:
             bs_mode = 'lxml'
         elif mode in (HTML5, 0):
             bs_mode = 'html5lib'