comparison genshi/output.py @ 347:ffa7dea6e8fd experimental-inline

cspeedups branch: Merged [423:426/trunk].
author cmlenz
date Fri, 10 Nov 2006 17:38:50 +0000
parents 60111a041e7c
children 3c0a97ff3924
comparison
equal deleted inserted replaced
344:bb881d1b4b5c 347:ffa7dea6e8fd
96 tagname = '%s:%s' % (prefix, tagname) 96 tagname = '%s:%s' % (prefix, tagname)
97 else: 97 else:
98 ns_attrib.append((QName('xmlns'), namespace)) 98 ns_attrib.append((QName('xmlns'), namespace))
99 buf = ['<', tagname] 99 buf = ['<', tagname]
100 100
101 for attr, value in attrib + ns_attrib: 101 for attr, value in attrib + tuple(ns_attrib):
102 attrname = attr.localname 102 attrname = attr.localname
103 if attr.namespace: 103 if attr.namespace:
104 prefix = ns_mapping.get(attr.namespace) 104 prefix = ns_mapping.get(attr.namespace)
105 if prefix: 105 if prefix:
106 attrname = '%s:%s' % (prefix, attrname) 106 attrname = '%s:%s' % (prefix, attrname)
181 'hr', 'img', 'input', 'isindex', 'link', 'meta', 181 'hr', 'img', 'input', 'isindex', 'link', 'meta',
182 'param']) 182 'param'])
183 _BOOLEAN_ATTRS = frozenset(['selected', 'checked', 'compact', 'declare', 183 _BOOLEAN_ATTRS = frozenset(['selected', 'checked', 'compact', 'declare',
184 'defer', 'disabled', 'ismap', 'multiple', 184 'defer', 'disabled', 'ismap', 'multiple',
185 'nohref', 'noresize', 'noshade', 'nowrap']) 185 'nohref', 'noresize', 'noshade', 'nowrap'])
186 _PRESERVE_SPACE = frozenset([QName('pre'), QName('textarea')]) 186 _PRESERVE_SPACE = frozenset([
187 QName('pre'), QName('http://www.w3.org/1999/xhtml}pre'),
188 QName('textarea'), QName('http://www.w3.org/1999/xhtml}textarea')
189 ])
187 190
188 def __call__(self, stream): 191 def __call__(self, stream):
189 namespace = self.NAMESPACE 192 namespace = self.NAMESPACE
190 ns_attrib = [] 193 ns_attrib = []
191 ns_mapping = {XML_NAMESPACE.uri: 'xml'} 194 ns_mapping = {XML_NAMESPACE.uri: 'xml'}
211 tagname = '%s:%s' % (prefix, tagname) 214 tagname = '%s:%s' % (prefix, tagname)
212 else: 215 else:
213 ns_attrib.append((QName('xmlns'), tagns)) 216 ns_attrib.append((QName('xmlns'), tagns))
214 buf = ['<', tagname] 217 buf = ['<', tagname]
215 218
216 for attr, value in attrib + ns_attrib: 219 for attr, value in chain(attrib, ns_attrib):
217 attrname = attr.localname 220 attrname = attr.localname
218 if attr.namespace: 221 if attr.namespace:
219 prefix = ns_mapping.get(attr.namespace) 222 prefix = ns_mapping.get(attr.namespace)
220 if prefix: 223 if prefix:
221 attrname = '%s:%s' % (prefix, attrname) 224 attrname = '%s:%s' % (prefix, attrname)
459 @param preserve: a set or sequence of tag names for which white-space 462 @param preserve: a set or sequence of tag names for which white-space
460 should be ignored. 463 should be ignored.
461 @param noescape: a set or sequence of tag names for which text content 464 @param noescape: a set or sequence of tag names for which text content
462 should not be escaped 465 should not be escaped
463 466
464 Both the `preserve` and `noescape` sets are expected to refer to 467 The `noescape` set is expected to refer to elements that cannot contain
465 elements that cannot contain further child elements. 468 further child elements (such as <style> or <script> in HTML documents).
466 """ 469 """
467 if preserve is None: 470 if preserve is None:
468 preserve = [] 471 preserve = []
469 self.preserve = frozenset(preserve) 472 self.preserve = frozenset(preserve)
470 if noescape is None: 473 if noescape is None:
474 def __call__(self, stream, ctxt=None, space=XML_NAMESPACE['space'], 477 def __call__(self, stream, ctxt=None, space=XML_NAMESPACE['space'],
475 trim_trailing_space=re.compile('[ \t]+(?=\n)').sub, 478 trim_trailing_space=re.compile('[ \t]+(?=\n)').sub,
476 collapse_lines=re.compile('\n{2,}').sub): 479 collapse_lines=re.compile('\n{2,}').sub):
477 mjoin = Markup('').join 480 mjoin = Markup('').join
478 preserve_elems = self.preserve 481 preserve_elems = self.preserve
479 preserve = False 482 preserve = 0
480 noescape_elems = self.noescape 483 noescape_elems = self.noescape
481 noescape = False 484 noescape = False
482 485
483 textbuf = [] 486 textbuf = []
484 push_text = textbuf.append 487 push_text = textbuf.append
498 if not preserve: 501 if not preserve:
499 text = collapse_lines('\n', trim_trailing_space('', text)) 502 text = collapse_lines('\n', trim_trailing_space('', text))
500 yield TEXT, Markup(text), pos 503 yield TEXT, Markup(text), pos
501 504
502 if kind is START: 505 if kind is START:
503 tag, attrib = data 506 tag, attrs = data
504 if not preserve and (tag in preserve_elems or 507 if preserve or (tag in preserve_elems or
505 attrib.get(space) == 'preserve'): 508 attrs.get(space) == 'preserve'):
506 preserve = True 509 preserve += 1
507 if not noescape and tag in noescape_elems: 510 if not noescape and tag in noescape_elems:
508 noescape = True 511 noescape = True
509 512
510 elif kind is END: 513 elif kind is END:
511 preserve = noescape = False 514 noescape = False
515 if preserve:
516 preserve -= 1
512 517
513 elif kind is START_CDATA: 518 elif kind is START_CDATA:
514 noescape = True 519 noescape = True
515 520
516 elif kind is END_CDATA: 521 elif kind is END_CDATA:
Copyright (C) 2012-2017 Edgewall Software