forked from html5lib/html5lib-python
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix html5lib#6: dom2sax crash by rewriting dom2sax, now using a treew…
…alker This moves the function to a new treeadapters module (where later the adapters from test_treewalker.py will get moved). dom2sax remains for backwards-compatibility, calling the new function.
- Loading branch information
Showing
5 changed files
with
71 additions
and
77 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Empty file.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from __future__ import absolute_import, division, unicode_literals | ||
|
||
from xml.sax.xmlreader import AttributesNSImpl | ||
|
||
from ..constants import adjustForeignAttributes, unadjustForeignAttributes | ||
|
||
prefix_mapping = {} | ||
for prefix, localName, namespace in adjustForeignAttributes.values(): | ||
if prefix is not None: | ||
prefix_mapping[prefix] = namespace | ||
|
||
|
||
def to_sax(walker, handler): | ||
"""Call SAX-like content handler based on treewalker walker""" | ||
handler.startDocument() | ||
for prefix, namespace in prefix_mapping.items(): | ||
handler.startPrefixMapping(prefix, namespace) | ||
|
||
for token in walker: | ||
type = token["type"] | ||
if type == "Doctype": | ||
continue | ||
elif type in ("StartTag", "EmptyTag"): | ||
attrs = AttributesNSImpl(token["data"], | ||
unadjustForeignAttributes) | ||
handler.startElementNS((token["namespace"], token["name"]), | ||
token["name"], | ||
attrs) | ||
if type == "EmptyTag": | ||
handler.endElementNS((token["namespace"], token["name"]), | ||
token["name"]) | ||
elif type == "EndTag": | ||
handler.endElementNS((token["namespace"], token["name"]), | ||
token["name"]) | ||
elif type in ("Characters", "SpaceCharacters"): | ||
handler.characters(token["data"]) | ||
elif type == "Comment": | ||
pass | ||
else: | ||
assert False, "Unknown token type" | ||
|
||
for prefix, namespace in prefix_mapping.items(): | ||
handler.endPrefixMapping(prefix) | ||
handler.endDocument() |
This file contains 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