Repository navigation
Conversation
flatten_dom_to_str wrote text nodes and attribute values out as-is and then re-parsed the result with BeautifulSoup. Page text that looks like markup (e.g. a code sample) became real elements in the observation, a `"` inside an attribute value split it into bogus attributes, and CDATA content was mangled. Escape text and CDATA content with html.escape(quote=False) and attribute values with html.escape(). CDATA is now printed as escaped text, since the HTML parser used here doesn't support CDATA sections. Adds tests with an HTML page (text, attributes) and an XHTML page (CDATA).
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
flatten_dom_to_strwrites text nodes and attribute values out as-is, then re-parses the result with BeautifulSoup. So page text that looks like markup turns into real elements in the HTML observation, a"inside an attribute value splits it into bogus attributes, and CDATA content gets mangled. This picks up where #178 left off, with the before/after example and the test that were asked for there.Before (
prune_html(flatten_dom_to_str(...))on the new test page):and a CDATA section
x < y && "z" > 1(on an XHTML page) comes out as1]]>.After:
and the CDATA text comes through as
x < y && "z" > 1.The fix escapes text and CDATA content with
html.escape(..., quote=False)and attribute values withhtml.escape(...). CDATA is printed as escaped text because the lxml HTML parser used here mangles<![CDATA[...]]>sections either way. (On HTML pages Chrome already turns CDATA inside SVG into plain text, so CDATA nodes only show up in XHTML/XML documents.)Tests:
test_dom_to_text_special_characters(text and attributes, checked on both theflatten_dom_to_strandprune_htmloutput) andtest_dom_to_text_cdata(XHTML page). Both fail on main and pass here. The existing observation tests are unchanged and still pass.