From 722c92250cdaf129efc7d516f388e2927429b01d Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Sun, 25 Nov 2018 01:16:17 +0900 Subject: [PATCH] Fix #5648: NodeMatcher matches Text node on attribute match --- sphinx/util/nodes.py | 3 +++ tests/test_util_nodes.py | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/sphinx/util/nodes.py b/sphinx/util/nodes.py index f98c72b370..dd0a089837 100644 --- a/sphinx/util/nodes.py +++ b/sphinx/util/nodes.py @@ -67,6 +67,9 @@ class NodeMatcher: if self.classes and not isinstance(node, self.classes): return False + if self.attrs and isinstance(node, nodes.Text): + return False + for key, value in self.attrs.items(): if key not in node: return False diff --git a/tests/test_util_nodes.py b/tests/test_util_nodes.py index 2fab10c1cf..04b437a4f8 100644 --- a/tests/test_util_nodes.py +++ b/tests/test_util_nodes.py @@ -82,6 +82,10 @@ def test_NodeMatcher(): matcher = NodeMatcher(nodes.title) assert len(doctree.traverse(matcher)) == 0 + # search with Any does not match to Text node + matcher = NodeMatcher(blah=Any) + assert len(doctree.traverse(matcher)) == 0 + @pytest.mark.parametrize( 'rst,node_cls,count',