(write, elem, encoding, qnames, namespaces)
| 126 | |
| 127 | |
| 128 | def _serialize_html(write, elem, encoding, qnames, namespaces): |
| 129 | tag = elem.tag |
| 130 | text = elem.text |
| 131 | if tag is Comment: |
| 132 | write("<!--%s-->" % _escape_cdata(text, encoding)) |
| 133 | elif tag is ProcessingInstruction: |
| 134 | write("<?%s?>" % _escape_cdata(text, encoding)) |
| 135 | else: |
| 136 | tag = qnames[tag] |
| 137 | if tag is None: |
| 138 | if text: |
| 139 | write(_escape_cdata(text, encoding)) |
| 140 | for e in elem: |
| 141 | _serialize_html(write, e, encoding, qnames, None) |
| 142 | else: |
| 143 | write("<" + tag) |
| 144 | items = list(elem.items()) |
| 145 | if items or namespaces: |
| 146 | items.sort() # lexical order |
| 147 | for k, v in items: |
| 148 | if isinstance(k, QName): |
| 149 | k = k.text |
| 150 | if isinstance(v, QName): |
| 151 | v = qnames[v.text] |
| 152 | else: |
| 153 | v = _escape_attrib_html(v, encoding) |
| 154 | # FIXME: handle boolean attributes |
| 155 | write(" %s=\"%s\"" % (qnames[k], v)) |
| 156 | if namespaces: |
| 157 | items = list(namespaces.items()) |
| 158 | items.sort(key=lambda x: x[1]) # sort on prefix |
| 159 | for v, k in items: |
| 160 | if k: |
| 161 | k = ":" + k |
| 162 | write(" xmlns%s=\"%s\"" % ( |
| 163 | k.encode(encoding), |
| 164 | _escape_attrib(v, encoding) |
| 165 | )) |
| 166 | write(">") |
| 167 | tag = tag.lower() |
| 168 | if text: |
| 169 | if tag == "script" or tag == "style": |
| 170 | write(_encode(text, encoding)) |
| 171 | else: |
| 172 | write(_escape_cdata(text, encoding)) |
| 173 | for e in elem: |
| 174 | _serialize_html(write, e, encoding, qnames, None) |
| 175 | if tag not in HTML_EMPTY: |
| 176 | write("</" + tag + ">") |
| 177 | if elem.tail: |
| 178 | write(_escape_cdata(elem.tail, encoding)) |
| 179 | |
| 180 | def write_html(root, f, |
| 181 | # keyword arguments |
no test coverage detected