Repository navigation
🐛 fix(etree): match lxml on iter, nsmap, to_lxml - #2552
Conversation
ElementTree gains iter(), which walks from the root element as lxml's _ElementTree.iter does. ElementView gains nsmap, an empty mapping as on lxml.html elements. to_lxml and to_lxml_html now strip the characters lxml refuses (C0 controls other than tab, LF and CR, surrogates, U+FFFE, U+FFFF) from text, tails and attribute values; before, text raised ValueError and such an attribute was dropped whole.
Merging this PR will improve performance by 8.95%2 warnings
Performance Changes
Tip Curious why performance improved? Comment Comparing Footnotes
|
turbohtml.etreediverged fromlxml.htmlin three places that block extraction code from using the C views instead of a Python adapter. Each now matches lxml.ElementTree.itergetroottree()returned anElementTreewith onlygetroot(), sotree.getroottree().iter("p")raisedAttributeError. It now hasiter(*tags), which walks from the root element like lxml's_ElementTree.iterand takes the same tag filters asElementView.iter:ElementView.nsmapThe attribute was missing. It now returns a new empty dict, as
lxml.htmldoes for every element. Views name foreign elements such assvgby their local name, so there are no namespace declarations to report.XML-illegal characters in
to_lxmllxml refuses strings holding C0 controls other than tab, LF and CR, surrogates, U+FFFE and U+FFFF.
to_lxmlandto_lxml_htmlpassed text straight through, so:\x01ValueError: All strings must be XML compatible<a href="/z\x01">,<img alt="p\x0bq">hrefandaltdropped entirelyhref="/z",alt="pq"The scan returns the original string when nothing needs stripping, so a clean tree copies as before. No benchmark in
tools/benchcallsto_lxml, so no CodSpeed row reaches the change.Not changed:
drop_treetaillxml leaves the dropped element's tail on it, while turbohtml leaves it only in the former parent. #1935 made that choice on purpose to keep repeated drops linear (#1727), and the reference documents it, so it stays.