def _inline_css(e, source_dir, dom, files): """Inlines CSS""" # Only use CSS stylesheet tags if e.nodeType != Node.ELEMENT_NODE \ or e.tagName != 'link' \ or e.getAttribute('rel') != 'stylesheet' \ or e.getAttribute('type') != 'text/css': return # Read the stylesheet and inline it href_path = _src_to_path( source_dir, e.getAttribute('href')) with open(href_path, 'rb') as href: cdata = CDATASection() cdata.replaceWholeText(href.read().decode('utf-8')) e.appendChild(cdata) # Remove all attributes except for type for i in range(e.attributes.length): attribute = e.attributes.item(i) if not attribute: continue if attribute.name == 'type': continue e.removeAttribute(attribute.name) # Change the tag name e.tagName = 'style' files.append(href_path)
def _inline_script(e, source_dir, dom, files): """Inlines script tags""" # Only use script tags with src attribute if e.nodeType != Node.ELEMENT_NODE \ or e.tagName != 'script' \ or not e.hasAttribute('src') \ or e.getAttribute('x-no-inline') == 'true': return # Read the script source and inline it src_path = _src_to_path( source_dir, e.getAttribute('src')) with open(src_path, 'rb') as src: cdata = CDATASection() cdata.replaceWholeText(src.read().decode('utf-8')) e.appendChild(cdata) # Make sure to remove the src attribute e.removeAttribute('src') files.append(src_path)
def cdata(self, text): cdata = CDATASection() cdata.replaceWholeText(text) return cdata