1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
########################################################################
#
# File Name: HTMLTableElement.py
#
#
"""
WWW: http://4suite.com/4DOM e-mail: support@4suite.com
Copyright (c) 2000 Fourthought Inc, USA. All Rights Reserved.
See http://4suite.com/COPYRIGHT for license and copyright information
"""
from xml.dom.html.HTMLElement import HTMLElement
from xml.dom import IndexSizeErr
from xml.dom import implementation
from xml.dom.NodeFilter import NodeFilter
import string
class HTMLTableElement(HTMLElement):
"""
Operations follow the DOM spec, and the 4.0 DTD for TABLE
<!ELEMENT TABLE (CAPTION?, (COL*|COLGROUP*), THEAD?, TFOOT?, TBODY+)>
"""
def __init__(self, ownerDocument, nodeName='TABLE'):
HTMLElement.__init__(self, ownerDocument, nodeName)
### Attribute Methods ###
def _get_align(self):
return string.capitalize(self.getAttribute('ALIGN'))
def _set_align(self,align):
self.setAttribute('ALIGN',align)
def _get_bgColor(self):
return self.getAttribute('BGCOLOR')
def _set_bgColor(self,bgcolor):
self.setAttribute('BGCOLOR',bgcolor)
def _get_border(self):
return self.getAttribute('BORDER')
def _set_border(self,border):
self.setAttribute('BORDER',border)
def _get_caption(self):
nl = self.getElementsByTagName('CAPTION')
if len(nl):
return nl[0]
return None
def _set_caption(self,capt):
nl = self.getElementsByTagName('CAPTION')
if len(nl):
self.replaceChild(capt, nl[0])
else:
self.insertBefore(capt, self.firstChild)
def _get_cellPadding(self):
return self.getAttribute('CELLPADDING')
def _set_cellPadding(self,cellpadding):
self.setAttribute('CELLPADDING',cellpadding)
def _get_cellSpacing(self):
return self.getAttribute('CELLSPACING')
def _set_cellSpacing(self,cellspacing):
self.setAttribute('CELLSPACING',cellspacing)
def _get_frame(self):
return string.capitalize(self.getAttribute('FRAME'))
def _set_frame(self,frame):
self.setAttribute('FRAME',frame)
def _get_rows(self):
rows = []
tHead = self._get_tHead()
if tHead:
rows.extend(list(tHead._get_rows()))
tFoot = self._get_tFoot()
if tFoot:
rows.extend(list(tFoot._get_rows()))
for tb in self._get_tBodies():
rows.extend(list(tb._get_rows()))
return implementation._4dom_createHTMLCollection(rows)
def _get_rules(self):
return string.capitalize(self.getAttribute('RULES'))
def _set_rules(self,rules):
self.setAttribute('RULES',rules)
def _get_summary(self):
return self.getAttribute('SUMMARY')
def _set_summary(self,summary):
self.setAttribute('SUMMARY',summary)
def _get_tBodies(self):
bodies = []
for child in self.childNodes:
if child.nodeName == 'TBODY':
bodies.append(child)
return implementation._4dom_createHTMLCollection(bodies)
def _get_tFoot(self):
for child in self.childNodes:
if child.nodeName == 'TFOOT':
return child
return None
def _set_tFoot(self, newFooter):
oldFooter = self._get_tFoot()
if not oldFooter:
# TFoot goes after THead
iter = self.ownerDocument.createNodeIterator(self.firstChild,
NodeFilter.SHOW_ELEMENT,
None, 0)
ref = None
node = iter.nextNode()
while not ref and node:
tagName = node.tagName
if tagName == 'THEAD':
ref = iter.nextNode()
elif tagName == 'TBODY':
ref = node
node = iter.nextNode()
self.insertBefore(newFooter, ref)
else:
self.replaceChild(newFooter, oldFooter)
def _get_tHead(self):
for child in self.childNodes:
if child.nodeName == 'THEAD':
return child
return None
def _set_tHead(self, newHead):
oldHead = self._get_tHead()
if oldHead:
self.replaceChild(newHead, oldHead)
else:
# We need to put the new Thead in the correct spot
# Look for a TFOOT or a TBODY
iter = self.ownerDocument.createNodeIterator(self.firstChild,
NodeFilter.SHOW_ELEMENT,
None, 0)
ref = None
node = iter.nextNode()
while not ref and node:
tagName = node.tagName
if tagName == 'TFOOT':
ref = node
elif tagName == 'TBODY':
ref = node
elif tagName in ['COL','COLGROUP']:
node = iter.nextNode()
while node.tagName == tagName:
node = iter.nextNode()
ref = node
elif tagName == 'CAPTION':
ref = iter.nextNode()
node = iter.nextNode()
self.insertBefore(newHead, ref)
def _get_width(self):
return self.getAttribute('WIDTH')
def _set_width(self,width):
self.setAttribute('WIDTH',width)
### Methods ###
def createCaption(self):
#Create a new CAPTION if one does not exist
caption = self._get_caption()
if not caption:
caption = self.ownerDocument.createElement('CAPTION')
self._set_caption(caption)
return caption
def createTHead(self):
#Create a new THEAD if one does not exist
thead = self._get_tHead()
if not thead:
thead = self.ownerDocument.createElement('THEAD')
self._set_tHead(thead)
return thead
def createTFoot(self):
#Create a new TFOOT if one does not exist
tfoot = self._get_tFoot()
if not tfoot:
tfoot = self.ownerDocument.createElement('TFOOT')
self._set_tFoot(tfoot)
return tfoot
def deleteCaption(self):
caption = self._get_caption()
if caption:
self.removeChild(caption)
def deleteRow(self,index):
rows = self._get_rows()
if index < 0 or index >= len(rows):
raise IndexSizeErr()
rows[index].parentNode.removeChild(rows[index])
def deleteTHead(self):
thead = self._get_tHead()
if thead != None:
self.removeChild(thead)
def deleteTFoot(self):
tfoot = self._get_tFoot()
if tfoot:
self.removeChild(tfoot)
def insertRow(self,index):
rows = self._get_rows()
if index < 0 or index > len(rows):
raise IndexSizeErr()
newRow = self.ownerDocument.createElement('TR')
if not rows:
# An empty table, create a body in which to insert the row
body = self.ownerDocument.createElement('TBODY')
# The body is the last element according to DTD
self.appendChild(body)
parent = body
ref = None
elif index == len(rows):
parent = rows[-1].parentNode
ref = None
else:
ref = rows[index]
parent = ref.parentNode
return parent.insertBefore(newRow, ref)
### Attribute Access Mappings ###
_readComputedAttrs = HTMLElement._readComputedAttrs.copy()
_readComputedAttrs.update ({
'rows' : _get_rows,
'tBodies' : _get_tBodies,
'caption' : _get_caption,
'tHead' : _get_tHead,
'tFoot' : _get_tFoot,
'align' : _get_align,
'bgColor' : _get_bgColor,
'border' : _get_border,
'cellPadding' : _get_cellPadding,
'cellSpacing' : _get_cellSpacing,
'frame' : _get_frame,
'rules' : _get_rules,
'summary' : _get_summary,
'width' : _get_width,
})
_writeComputedAttrs = HTMLElement._writeComputedAttrs.copy()
_writeComputedAttrs.update ({
'caption' : _set_caption,
'tHead' : _set_tHead,
'tFoot' : _set_tFoot,
'align' : _set_align,
'bgColor' : _set_bgColor,
'border' : _set_border,
'cellPadding' : _set_cellPadding,
'cellSpacing' : _set_cellSpacing,
'frame' : _set_frame,
'rules' : _set_rules,
'summary' : _set_summary,
'width' : _set_width,
})
# Create the read-only list of attributes
_readOnlyAttrs = filter(lambda k,m=_writeComputedAttrs: not m.has_key(k),
HTMLElement._readOnlyAttrs + _readComputedAttrs.keys())
|