Micro optimization

This commit is contained in:
Kovid Goyal 2019-09-11 18:05:16 +05:30
parent 1b679ae5c8
commit 1668365156
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 12 additions and 8 deletions

View File

@ -443,6 +443,9 @@ def map_epub_type(epub_type, attribs, elem):
attribs[i] = ['role', role] attribs[i] = ['role', role]
known_tags = ('img', 'script', 'link', 'image', 'style')
def serialize_elem(elem, nsmap): def serialize_elem(elem, nsmap):
ns, name = split_name(elem.tag) ns, name = split_name(elem.tag)
nl = name.lower() nl = name.lower()
@ -450,7 +453,7 @@ def serialize_elem(elem, nsmap):
ns, name = None, 'epub-' + name ns, name = None, 'epub-' + name
if nl == 'meta': if nl == 'meta':
return # Filter out <meta> tags as they have unknown side-effects return # Filter out <meta> tags as they have unknown side-effects
if nl in {'img', 'script', 'link', 'image', 'style'}: if nl in known_tags:
name = nl name = nl
ans = {'n':name} ans = {'n':name}
if elem.text: if elem.text:

View File

@ -164,7 +164,7 @@ def finalize_resources(book, root_name, resource_data):
return root_data, mathjax, blob_url_map return root_data, mathjax, blob_url_map
js_types = set('text/javascript text/ecmascript application/javascript application/ecmascript'.split(' ')) js_types = {k: True for k in 'text/javascript text/ecmascript application/javascript application/ecmascript'.split(' ')}
resource_tag_names = {'script':'src', 'link':'href', 'img':'src', 'image':'xlink:href'} resource_tag_names = {'script':'src', 'link':'href', 'img':'src', 'image':'xlink:href'}
ns_rmap = {'http://www.w3.org/2000/svg':'svg', 'http://www.w3.org/1999/xlink':'xlink', 'http://www.w3.org/1998/Math/MathML':'math', 'http://www.w3.org/XML/1998/namespace': 'xml'} ns_rmap = {'http://www.w3.org/2000/svg':'svg', 'http://www.w3.org/1999/xlink':'xlink', 'http://www.w3.org/1998/Math/MathML':'math', 'http://www.w3.org/XML/1998/namespace': 'xml'}
ns_count = 0 ns_count = 0
@ -201,11 +201,12 @@ def process_stack(stack, tag_map, ns_map, load_required, onload, resource_urls):
if attr: if attr:
if attr.indexOf(':') != -1: if attr.indexOf(':') != -1:
attr = attr.replace('xlink:', '') attr = attr.replace('xlink:', '')
for a in (src.a or v'[]'): if src.a:
if a[0] is attr: for a in src.a:
loadable = a[1].startswith('blob:') if a[0] is attr:
resource_urls[a[1]] = True loadable = a[1].startswith('blob:')
break resource_urls[a[1]] = True
break
if loadable: if loadable:
load_required.add(node[0]) load_required.add(node[0])
elem.addEventListener('load', onload.bind(node[0])) elem.addEventListener('load', onload.bind(node[0]))
@ -214,7 +215,7 @@ def process_stack(stack, tag_map, ns_map, load_required, onload, resource_urls):
apply_attributes(src, elem, ns_map) apply_attributes(src, elem, ns_map)
parent.appendChild(elem) parent.appendChild(elem)
if src.x: if src.x:
if src.n is 'script' and (elem.getAttribute('type') or 'text/javascript').toLowerCase() in js_types: if src.n is 'script' and js_types[(elem.getAttribute('type') or 'text/javascript').toLowerCase()] is True:
elem.text = src.x elem.text = src.x
else: else:
elem.appendChild(document.createTextNode(src.x)) elem.appendChild(document.createTextNode(src.x))