Ensure root tag is in correct namespace

This commit is contained in:
Kovid Goyal 2019-10-29 18:24:39 +05:30
parent ddb96c0eed
commit 9f02e70317
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -755,11 +755,14 @@ def ensure_body(root):
def html_as_json(root): def html_as_json(root):
ns, name = split_name(root.tag)
if ns not in (None, XHTML_NS):
raise ValueError('HTML tag must be in empty or XHTML namespace')
ensure_body(root)
try: try:
serialize = plugins['html_as_json'][0].serialize serialize = plugins['html_as_json'][0].serialize
except KeyError: except KeyError:
return as_bytes(json.dumps(html_as_dict(root), ensure_ascii=False, separators=(',', ':'))) return as_bytes(json.dumps(html_as_dict(root), ensure_ascii=False, separators=(',', ':')))
ensure_body(root)
for child in tuple(root.iterchildren('*')): for child in tuple(root.iterchildren('*')):
if child.tag.partition('}')[-1] not in ('head', 'body'): if child.tag.partition('}')[-1] not in ('head', 'body'):
root.remove(child) root.remove(child)
@ -768,7 +771,6 @@ def html_as_json(root):
def html_as_dict(root): def html_as_dict(root):
ensure_body(root)
for child in tuple(root.iterchildren('*')): for child in tuple(root.iterchildren('*')):
if child.tag.partition('}')[-1] not in ('head', 'body'): if child.tag.partition('}')[-1] not in ('head', 'body'):
root.remove(child) root.remove(child)