✨ feat(serialize): add XML/XHTML output syntax#564
Merged
Conversation
Html gained an xml field that switches serialize/encode/serialize_iter from HTML to XML output, the equivalent of lxml's tostring(method="xml"). Every empty element self-closes, a foreign SVG or MathML subtree carries the namespace declaration that makes it well-formed, and text and attribute values follow the XML escaping rules, with no HTML void-element or raw-text special casing. The serializer keeps its single tree walk and branches on the flag at the four points where the HTML and XML algorithms diverge (empty-element form, raw text, escaping, namespaces), so the HTML fast path is untouched. The flag composes with sort_attributes and an Indent layout; a Minify layout stays HTML, since its optional-tag and unquoted-attribute rules are HTML-parser rules. closes tox-dev#535
Merging this PR will not alter performance
Performance Changes
Comparing Footnotes
|
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.
turbohtml serialized HTML syntax only, so a tree bound for an XML toolchain (an XSLT or XPath 2.0 pipeline, an
XMLSerializerconsumer, anything that rejects HTML's unclosed tags) had to be round-tripped through a second library. This adds an XML/XHTML serialization mode, the equivalent of lxml'stostring(method="xml"), soserialize,encode, andserialize_itercan emit well-formed XML from the same parsed tree.Htmlgained anxmlfield. Withxml=Truethe WHATWG XML serialization rules apply: every empty element self-closes (<br/>,<div/>), a foreign SVG or MathML subtree carries the namespace declaration that makes it well-formed (xmlnson the subtree root,xmlns:xlinkon any element bearing anxlink:attribute), and text and attribute values follow the XML escaping rules (&,<,>in text, plus"and the whitespace characters in attributes, with a no-break space left literal since XML predefines no ). The HTML void-element and raw-text special casing does not apply, so a<script>body is escaped like any other text.The serializer keeps its single tree walk and branches on the flag at the four points where the HTML and XML algorithms diverge, so the HTML fast path stays intact. 🚀 The flag composes with
sort_attributesand anIndentlayout; aMinifylayout stays HTML, since its optional-tag and unquoted-attribute rules are HTML-parser rules.The
docs/migration/lxml.rstguide mapstostring(method="xml"|"xhtml")ontoserialize(Html(xml=True)), and aserialize-xmlbenchmark op compares turbohtml against lxml'smethod="xml"head to head.closes #535