From 3b9aee43a2242f57c111e9f18d6bbea39076d695 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Sat, 17 Dec 2016 17:28:19 +0900 Subject: [PATCH] Fix #185: References to section title including raw node has broken --- CHANGES | 1 + sphinx/util/nodes.py | 2 ++ tests/test_util_nodes.py | 14 +++++++++++++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index 92949158d..375701166 100644 --- a/CHANGES +++ b/CHANGES @@ -13,6 +13,7 @@ Bugs fixed * #3246: xapian search adapter crashes * #3253: In Py2 environment, building another locale with a non-captioned toctree produces ``None`` captions +* #185: References to section title including raw node has broken Release 1.5.1 (released Dec 13, 2016) ===================================== diff --git a/sphinx/util/nodes.py b/sphinx/util/nodes.py index b5216eb51..1da3f69d8 100644 --- a/sphinx/util/nodes.py +++ b/sphinx/util/nodes.py @@ -216,6 +216,8 @@ def clean_astext(node): node = node.deepcopy() for img in node.traverse(nodes.image): img['alt'] = '' + for raw in node.traverse(nodes.raw): + raw.parent.remove(raw) return node.astext() diff --git a/tests/test_util_nodes.py b/tests/test_util_nodes.py index e7a4b7e90..ac8c3645d 100644 --- a/tests/test_util_nodes.py +++ b/tests/test_util_nodes.py @@ -15,7 +15,7 @@ from docutils.parsers import rst from docutils.utils import new_document from docutils import frontend -from sphinx.util.nodes import extract_messages +from sphinx.util.nodes import extract_messages, clean_astext from sphinx.transforms import ApplySourceWorkaround @@ -150,3 +150,15 @@ def test_extract_messages_without_rawsource(): _transform(document) assert_node_count(extract_messages(document), nodes.TextElement, 1) assert [m for n, m in extract_messages(document)][0], 'text sentence' + + +def test_clean_astext(): + node = nodes.paragraph(text='hello world') + assert 'hello world' == clean_astext(node) + + node = nodes.image(alt='hello world') + assert '' == clean_astext(node) + + node = nodes.paragraph(text='hello world') + node += nodes.raw('', 'raw text', format='html') + assert 'hello world' == clean_astext(node)