Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong order of tail and children #536

Merged
merged 1 commit into from
Apr 2, 2024
Merged
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
13 changes: 13 additions & 0 deletions tests/unit_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,19 @@ def test_htmlprocessing():
trafilatura.htmlprocessing.process_node(node, options)
assert node.text == "some text"
assert node.tail == "tail"
node = etree.fromstring("<p><ref target='url'><hi rend='#b'>bold</hi>inner</ref>outer</p>")[0]
processed = trafilatura.htmlprocessing.handle_textnode(node, options)
assert processed.tail == "outer"
node = etree.fromstring("<p><ref target='url'>text</ref>tail</p>")[0]
processed = trafilatura.htmlprocessing.handle_textnode(node, options)
assert processed.tail == "tail" and processed.text == "text"
node = etree.fromstring("<p><ref target='url'></ref>tail</p>")[0]
processed = trafilatura.htmlprocessing.handle_textnode(node, options)
assert processed.tail == "" and processed.text == "tail"
node = etree.fromstring("<p><ref target='url'>text<hi rend='#b'>bold</hi></ref>tail</p>")[0]
processed = trafilatura.htmlprocessing.handle_textnode(node, options)
assert processed.tail == "tail" and processed.text == "text"



def test_extraction_options():
Expand Down
6 changes: 3 additions & 3 deletions trafilatura/htmlprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ def convert_tags(tree, options, url=None):

def handle_textnode(element, options, comments_fix=True, preserve_spaces=False):
'''Convert, format, and probe potential text elements'''
if element.text is None and element.tail is None:
if element.text is None and element.tail is None and len(element) == 0:
return None
# lb bypass
if comments_fix is False and element.tag == 'lb':
Expand All @@ -318,7 +318,7 @@ def handle_textnode(element, options, comments_fix=True, preserve_spaces=False):
# return None
# duplicate_test(subelement)?
return element
if element.text is None:
if element.text is None and len(element) == 0:
# try the tail
# LOGGER.debug('using tail for element %s', element.tag)
element.text, element.tail = element.tail, ''
Expand All @@ -332,7 +332,7 @@ def handle_textnode(element, options, comments_fix=True, preserve_spaces=False):
element.tail = trim(element.tail)
# filter content
# or not re.search(r'\w', element.text): # text_content()?
if not element.text or textfilter(element) is True:
if not element.text and textfilter(element) is True:
return None
if options.dedup and duplicate_test(element, options.config) is True:
return None
Expand Down
Loading