1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 """
18 ================
19 DTD Collection
20 ================
21
22 This module provides a collection of DTD query classes to be used with
23 the soup parser.
24 """
25 __author__ = u"Andr\xe9 Malo"
26 __docformat__ = "restructuredtext en"
27
28 from tdi import interfaces as _interfaces
29
30
32 """
33 Query class for the HTML DTD
34
35 :IVariables:
36 `_cdata` : ``dict``
37 Dict of CDATA elements (for speed lookup)
38
39 `_optional` : ``dict``
40 Dict of optional elements (for speed lookup)
41
42 `_empty` : ``dict``
43 Dict of empty elements (for speed lookup)
44 """
45 __implements__ = [_interfaces.DTDInterface]
46
47
48
49
50 _EMPTY = ('app', 'area', 'base', 'basefont', 'bgsound', 'br', 'col',
51 'command', 'embed', 'frame', 'hr', 'img', 'input', 'isindex',
52 'keygen', 'link', 'meta', 'nextid', 'param', 'sound', 'source',
53 'spacer', 'track', 'wbr')
54
55
56
57
58
59
60
61
62 _CDATA = ('listing', 'plaintext', 'script', 'style', 'textarea', 'xmp')
63
64
65 _intable = ('caption', 'col', 'colgroup', 'tbody', 'thead', 'tfoot')
66
67
68
69
70
71 _OPTIONAL = tuple({
72 'html': ('html',),
73 'head': ('html', 'body', 'head',),
74 'body': ('html', 'body', 'head',),
75 'li': ('li',),
76 'dt': ('dt', 'dd',),
77 'dd': ('dt', 'dd',),
78 'p': ('address', 'article', 'aside', 'blockquote', 'body',
79 'dd', 'dir', 'div', 'dl', 'dt', 'fieldset', 'footer',
80 'form', 'frame', 'frameset', 'h1', 'h2', 'h3', 'h4',
81 'h5', 'h6', 'head', 'header', 'hgroup', 'hr', 'html',
82 'isindex', 'layer', 'li', 'listing', 'map', 'marquee',
83 'menu', 'multicol', 'nav', 'noframes', 'ol', 'p',
84 'plaintext', 'pre', 'section', 'table', 'td', 'th',
85 'tr', 'ul', 'xmp') + _intable,
86 'rt': ('rt', 'rp',),
87 'rp': ('rt', 'rp',),
88 'optgroup': ('optgroup',),
89 'option': ('option', 'optgroup',),
90 'colgroup': _intable + ('td', 'th', 'tr',),
91 'caption': _intable + ('td', 'th', 'tr',),
92 'thead': _intable,
93 'tbody': _intable,
94 'tfoot': _intable,
95 'tr': _intable + ('tr',),
96 'td': _intable + ('td', 'th', 'tr',),
97 'th': _intable + ('td', 'th', 'tr',),
98 }.items())
99 del _intable
100
102 """ Initialization """
103 dict_ = dict
104 optional = dict_([
105 (name, dict_([
106 (item, None) for item in forbidden
107 ]).__contains__)
108 for name, forbidden in self._OPTIONAL
109 ]).get
110 if self.__class__ == HTMLDTD:
111 import operator as _operator
112
113 self.cdata = dict_([
114 (item, None) for item in self._CDATA
115 ]).__contains__
116 self.empty = empty = dict_([
117 (item, None) for item in self._EMPTY
118 ]).__contains__
119 def nestable(outer, inner):
120 """ :See: `tdi.interfaces.DTDInterface.nestable` """
121 opt = optional(outer)
122 if opt is not None:
123 return not(opt(inner))
124 elif empty(outer):
125 return False
126 return True
127 self.nestable = nestable
128 else:
129 self._empty = dict_([
130 (item, None) for item in self._EMPTY
131 ]).__contains__
132 self._cdata = dict_([
133 (item, None) for item in self._CDATA
134 ]).__contains__
135 self._optional = optional
136
138 """ :See: `tdi.interfaces.DTDInterface.cdata` """
139 return self._cdata(name)
140
142 """ :See: `tdi.interfaces.DTDInterface.nestable` """
143 opt = self._optional(outer)
144 if opt is not None:
145 return not(opt(inner))
146 elif self._empty(outer):
147 return False
148
149 return True
150
152 """ :See: `tdi.interfaces.DTDInterface.empty` """
153 return self._empty(name)
154
155
157 """
158 XML DTD query class
159
160 This class effectively defines every wellformed XML valid.
161 """
162 __implements__ = [_interfaces.DTDInterface]
163
165 """ :See: `DTDInterface` """
166
167 return False
168
170 """ :See: `DTDInterface` """
171
172 return True
173
175 """ :See: `DTDInterface` """
176
177 return False
178