from types import DictionaryType, ListType
import string
try:
from WebUtils.Funcs import htmlEncode
except ImportError:
from Webware.WebUtils.Funcs import htmlEncode
class HTMLElement:
def __init__(self, name='', label='', attributes={}, languageStandard='XHTML'):
'''The constructor basically just sets up a bunch of containers
the label is a string that is used by some classes, but not others, mainly for things
like button labels; attributes is a dictionary of parameters that will be added to
the HTML tag; languageStandard is whether you want HTML or XHTML tags returned'''
self._name = name
self._label = label
if not self._label:
self._label = name
self._attributes = {}
self._idAttribute = ''
self._languageStandard = languageStandard
self._value = None
self._style = { }
self._styleClass = []
self.addAttributes(attributes)
def addAttributes(self, attributes):
'''Public method for adding tag attributes; merges in a dictionary of tag attributes'''
assert type(attributes) == DictionaryType, "You must pass in attributes as a dictionary."
for key in attributes.keys():
self._attributes[key.lower()] = attributes[key]
def id(self):
'''returns the dom id attribute of this htmlElement'''
if self._idAttribute:
return self._idAttribute
else:
return self._generateID()
def setID(self,string):
'''Public utility method to surgically override the id tag attribute'''
self._idAttribute = string
def setTabIndex(self,num):
'''Public method for setting the tab index of a field... pass an integer'''
self.addAttributes( { 'tabindex': num } )
def setAccessKey(self,key):
'''Public method for setting the tab index of a field... pass a single character string (a letter)'''
self.addAttributes( { 'accesskey': key } )
def addStyle(self, dict):
'''Public method for adding a style attribute to a tag; takes a dictionary;
the key is the directive, the value is the parameter.
Duplicate key names override their predecessor.
Input like this:
{ 'background-color': 'rgb(255,255,255)',
'font-size: 11px' }
( trailing semicolon can be used, or not )
set a key to None or empty string to remove it from the set
'''
assert type(dict) == DictionaryType, "You must pass in styles as a dictionary."
for key,value in dict.items():
if value[-1] != ";":
dict[key] = value+";"
self._style.update(dict)
def addClass(self,klass):
'''Public method for appending a CSS class name to this tag; expects a string'''
self._styleClass.append(klass)
def clearClasses(self):
'''removes all CSS classes from the tag'''
self._styleClass = []
def _closeTag(self):
'''Private method that returns the " /" to close up a tag, if we're in XHTML land'''
if self._languageStandard.upper() == 'XHTML':
return " /"
else:
return ""
def _tagAttributes(self):
'''Private method that converts the attribute dictionary into a string of tag
attributes. E.g., size="12" '''
atts = ''
for attribute in self.attributes().keys():
attribute = attribute.lower()
if attribute in ['checked','selected','disabled','readonly']:
if self.languageStandard() == 'XHTML':
atts += self._makeTagAttribute( attribute, attribute )
else:
atts += self._makeTagAttribute( attribute, 1, 0 )
else:
atts += self._makeTagAttribute( attribute, self.attributes()[attribute] )
styleBits = ""
for key,value in self._style.items():
if value:
styleBits += "%s:%s" % ( key,value )
if styleBits:
atts += self._makeTagAttribute ( 'style',styleBits )
if self._styleClass:
atts += self._makeTagAttribute ( 'class', string.join(self._styleClass) )
if atts and atts[0] != ' ':
atts = " %s" % atts
return atts
def _makeTagAttribute(self, attribute, value, termDef=1):
'''Private method that formats dictionary pair data as tag attributes'''
if not value:
return ''
elif termDef:
return ' %s="%s"' % (attribute.lower(), htmlEncode(str(value)))
else:
return ' ' + attribute.lower()
def name(self):
'''Generic name parameter access method'''
return self._name
def label(self):
'''Generic label parameter access method'''
return self._label
def value(self):
'''Generic field value access method'''
return self._value
def languageStandard(self):
'''Generic parameter access method'''
return self._languageStandard
def attributes(self):
'''Returns a dictionary of field element attributes'''
return self._attributes
def dump(self):
'''Quick and dirty method that returns the label and tag in that order'''
return "%s : %s" % ( self.label(), self.tag() )
def tag(self):
''' '''
raise "Abstract class HTMLElement does not define tag"