You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is there a way I can wrap content that doesn't already happen to be wrapped in an HTML tag? Here's a sample fragment:
<p>A paragraph here.</p>
Naked text (not in an element for some reason).
<p>Another paragraph.</p>
I'd love to be able to wrap the second line in the example above in some tag (<p> in this example). Would a custom post-processor be able to handle this? I'm not sure how your parser handles untagged elements...
Thanks for this fantastic package!
The text was updated successfully, but these errors were encountered:
Right now, there's nothing in html-sanitizer which would help you with that. The naked text is the .tail of the <p> tag before it and is left alone since it contains more than only whitespace.
I'd parse your fragment with beautifulsoup4, loop through all top-level elements and wrap text-only elements. Something like this (code follows because the problem was interesting 😄)
import bs4
soup = bs4.BeautifulSoup('<p>a</p>b<p>c</p>', 'html.parser')
for node in soup:
if isinstance(node, bs4.element.NavigableString):
tag = soup.new_tag('p')
tag.append(str(node))
node.replace_with(tag)
print(soup)
Is there a way I can wrap content that doesn't already happen to be wrapped in an HTML tag? Here's a sample fragment:
I'd love to be able to wrap the second line in the example above in some tag (
<p>
in this example). Would a custom post-processor be able to handle this? I'm not sure how your parser handles untagged elements...Thanks for this fantastic package!
The text was updated successfully, but these errors were encountered: