2007-01-24 22:24:01 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
2009-08-30 03:39:55 +08:00
|
|
|
import sys
|
2007-01-24 22:24:01 +08:00
|
|
|
from py.xml import Namespace, Tag
|
|
|
|
from py.__.xmlobj.visit import SimpleUnicodeVisitor
|
|
|
|
|
|
|
|
class HtmlVisitor(SimpleUnicodeVisitor):
|
|
|
|
|
|
|
|
single = dict([(x, 1) for x in
|
|
|
|
('br,img,area,param,col,hr,meta,link,base,'
|
|
|
|
'input,frame').split(',')])
|
2007-01-30 23:42:19 +08:00
|
|
|
inline = dict([(x, 1) for x in
|
|
|
|
('a abbr acronym b basefont bdo big br cite code dfn em font '
|
|
|
|
'i img input kbd label q s samp select small span strike '
|
|
|
|
'strong sub sup textarea tt u var'.split(' '))])
|
2007-01-24 22:24:01 +08:00
|
|
|
|
|
|
|
def repr_attribute(self, attrs, name):
|
|
|
|
if name == 'class_':
|
|
|
|
value = getattr(attrs, name)
|
|
|
|
if value is None:
|
|
|
|
return
|
|
|
|
return super(HtmlVisitor, self).repr_attribute(attrs, name)
|
|
|
|
|
|
|
|
def _issingleton(self, tagname):
|
|
|
|
return tagname in self.single
|
|
|
|
|
2007-01-30 23:42:19 +08:00
|
|
|
def _isinline(self, tagname):
|
|
|
|
return tagname in self.inline
|
|
|
|
|
2009-08-30 02:04:48 +08:00
|
|
|
if sys.version_info > (3, 0):
|
|
|
|
def u(s): return s
|
|
|
|
else:
|
|
|
|
def u(s): return unicode(s)
|
|
|
|
|
2007-01-24 22:24:01 +08:00
|
|
|
class HtmlTag(Tag):
|
|
|
|
def unicode(self, indent=2):
|
|
|
|
l = []
|
|
|
|
HtmlVisitor(l.append, indent, shortempty=False).visit(self)
|
2009-08-30 02:04:48 +08:00
|
|
|
return u("").join(l)
|
2007-01-24 22:24:01 +08:00
|
|
|
|
|
|
|
# exported plain html namespace
|
|
|
|
class html(Namespace):
|
|
|
|
__tagclass__ = HtmlTag
|
|
|
|
__stickyname__ = True
|
|
|
|
__tagspec__ = dict([(x,1) for x in (
|
|
|
|
'a,abbr,acronym,address,applet,area,b,bdo,big,blink,'
|
|
|
|
'blockquote,body,br,button,caption,center,cite,code,col,'
|
|
|
|
'colgroup,comment,dd,del,dfn,dir,div,dl,dt,em,embed,'
|
|
|
|
'fieldset,font,form,frameset,h1,h2,h3,h4,h5,h6,head,html,'
|
|
|
|
'i,iframe,img,input,ins,kbd,label,legend,li,link,listing,'
|
|
|
|
'map,marquee,menu,meta,multicol,nobr,noembed,noframes,'
|
|
|
|
'noscript,object,ol,optgroup,option,p,pre,q,s,script,'
|
|
|
|
'select,small,span,strike,strong,style,sub,sup,table,'
|
|
|
|
'tbody,td,textarea,tfoot,th,thead,title,tr,tt,u,ul,xmp,'
|
|
|
|
'base,basefont,frame,hr,isindex,param,samp,var'
|
|
|
|
).split(',') if x])
|
|
|
|
|
|
|
|
class Style(object):
|
|
|
|
def __init__(self, **kw):
|
|
|
|
for x, y in kw.items():
|
|
|
|
x = x.replace('_', '-')
|
|
|
|
setattr(self, x, y)
|
|
|
|
|