Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions readability/readability.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,17 @@ def summary(self, html_partial=False, keep_all_images=False):
)

if best_candidate:
# Moving a form descendant into the article would bypass
# form removal in sanitize().
if best_candidate["elem"].xpath("ancestor-or-self::form"):
outside_form = {
elem: candidate
for elem, candidate in candidates.items()
if not elem.xpath("ancestor-or-self::form")
}
if outside_form:
candidates = outside_form
best_candidate = self.select_best_candidate(candidates)
article = self.get_article(
candidates, best_candidate, html_partial=html_partial
)
Expand Down
22 changes: 22 additions & 0 deletions tests/test_article_only.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,28 @@ def test_preserves_lead_sibling_issue_159(self):
self.assertIn(lead, summary)
self.assertIn("Eligibility restrictions", summary)

def test_prefers_article_text_to_feedback_form(self):
lead = "The privacy rules explain how public agencies must handle personal information."
details = "People can request access to their records and ask an agency to correct an error."
feedback = "Tell us about your visit, your rating, your questions, your suggestions."
sample = f"""
<html><body>
<div class="article-content"><p>{lead}</p><p>{details}</p></div>
<form><div class="panel"><div class="panel-content">
<p>{feedback}</p>
<p>Did you find the guidance, examples, references, or links you needed?</p>
<p>Please describe any trouble with navigation, search, layout, or readability.</p>
<textarea name="feedback"></textarea>
</div></div></form>
</body></html>
"""

summary = Document(sample).summary()

self.assertIn(lead, summary)
self.assertIn(details, summary)
self.assertNotIn(feedback, summary)

def test_prefers_list_based_article_to_service_form(self):
list_items = "".join(
"<li>Article release note number {} with substantial useful "
Expand Down