8000 Failure if best_elem is root (fix #58) · Harry0201/python-readability@bf9e740 · GitHub
[go: up one dir, main page]

Skip to content

Commit bf9e740

Browse files
committed
Failure if best_elem is root (fix buriy#58)
1 parent 1d4ee9d commit bf9e740

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

readability/readability.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,9 @@ def get_article(self, candidates, best_candidate, html_partial=False):
212212
else:
213213
output = document_fromstring('<div/>')
214214
best_elem = best_candidate['elem']
215-
for sibling in best_elem.getparent().getchildren():
215+
parent = best_elem.getparent()
216+
siblings = parent.getchildren() if parent else [best_elem]
217+
for sibling in siblings:
216218
# in lxml there no concept of simple text
217219
# if isinstance(sibling, NavigableString): continue
218220
append = False

tests/test_article_only.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,14 @@ def test_wrong_link_issue_49(self):
5050
doc = Document(sample)
5151
res = doc.summary(html_partial=True)
5252
self.assertEqual('<div><div class="content__article-body ', res[0:39])
53+
54+
def test_best_elem_is_root_and_passing(self):
55+
sample = (
56+
'<html class="article" id="body">'
57+
' <body>'
58+
' <p>1234567890123456789012345</p>'
59+
' </body>'
60+
'</html>'
61+
)
62+
doc = Document(sample)
63+
doc.summary()

0 commit comments

Comments
 (0)
0