2 # -*- coding: utf-8 -*-
7 additionalCaseSensitiveAttrs
= []
8 lastCaseInsensitiveAttr
= 0
11 def constName(prefix
, i
):
12 return (prefix
+ "_%s") % i
.upper().replace("-", "_");
14 def parseFile(fname
, isAttrs
, isHtmlAttrs
):
18 global additionalCaseSensitiveAttrs
19 global lastCaseInsensitiveAttr
26 for i
in f
.xreadlines():
28 if i
.startswith("#") or i
== "": continue
29 if isHtmlAttrs
and i
.startswith("END_CI"):
30 lastCaseInsensitiveAttr
= len(cache
)
33 # don't add it twice to names list
34 cons
= constName(prefix
, i
)
35 alreadyDefined
= [k
for k
in namesList
if k
[2] == cons
]
36 if len(alreadyDefined
) > 0:
39 if i
.upper() == "TEXT" and isAttrs
: # hack to get ID_TEXT and ATTR_TEXT be different
43 key
= cache
.get(i
, len(cache
) + 1)
45 if isHtmlAttrs
and lastCaseInsensitiveAttr
> 0 and key
<= len(cache
):
46 additionalCaseSensitiveAttrs
.append(cons
)
49 namesList
.append([key
, i
, cons
])
53 parseFile(fname
, False, False)
55 def parseAttrs(fname
, isHtml
):
56 parseFile(fname
, True, isHtml
)
59 parseTags("htmltags.in");
60 lastTagId
= len(cache
)
62 # parse htmlattrs.in file
63 parseAttrs("htmlattrs.in", True)
64 lastAttrId
= len(cache
)
66 # parse SVG tags, attrs
67 parseTags("../svg/svgtags.in")
68 parseAttrs("../svg/svgattrs.in", False)
71 parseAttrs("../svg/xlinkattrs.in", False)
75 return cmp(a
[0], b
[0]) or -cmp(a
[2], b
[2])
78 ########################################
79 # END OF PARSING, START WRITING FILES
80 ########################################
82 out
= open("htmlnames.h", "w")
83 out
.write("/* This file is automatically generated from htmltags.in and htmlattrs.in by gennames.py, do not edit */\n")
84 out
.write("/* Copyright 2008 Vyacheslav Tokarev */\n")
86 out
.write("#ifndef HTMLNames_h\n")
87 out
.write("#define HTMLNames_h\n")
89 out
.write("#include \"misc/idstring.h\"\n")
91 out
.write("#define XHTML_NAMESPACE \"http://www.w3.org/1999/xhtml\"\n")
92 out
.write("#define SVG_NAMESPACE \"http://www.w3.org/2000/svg\"\n")
93 out
.write("#define XLINK_NAMESPACE \"http://www.w3.org/1999/xlink\"\n")
96 if i
[2].startswith("ATTR"):
97 out
.write("#define %s %d\n" % (i
[2], 65536 + i
[0]))
99 out
.write("#define %s %d\n" % (i
[2], i
[0]))
100 out
.write("#define ID_LAST_TAG %d\n" % (lastTagId
))
101 out
.write("#define ID_CLOSE_TAG 16384\n")
102 out
.write("#define ATTR_LAST_ATTR %d\n" % (lastAttrId
))
103 out
.write("#define ATTR_LAST_CI_ATTR %d\n" % (lastCaseInsensitiveAttr
))
105 s
= "((localNamePart(id)) > ATTR_LAST_CI_ATTR"
106 for i
in additionalCaseSensitiveAttrs
:
107 s
= s
+ " || (id) == %s" % i
109 out
.write("#define caseSensitiveAttr(id) (%s)\n" % s
)
111 out
.write("namespace DOM {\n\
113 #define NodeImpl_IdNSMask 0xffff0000\n\
114 #define NodeImpl_IdLocalMask 0x0000ffff\n\
116 const quint16 xhtmlNamespace = 0;\n\
117 const quint16 emptyNamespace = 1;\n\
118 const quint16 svgNamespace = 2;\n\
119 const quint16 xlinkNamespace = 3;\n\
120 const quint16 anyNamespace = 0xffff;\n\
121 const quint16 anyLocalName = 0xffff;\n\
122 const quint16 emptyPrefix = 0;\n\
124 inline quint16 localNamePart(quint32 id) { return id & NodeImpl_IdLocalMask; }\n\
125 inline quint16 namespacePart(quint32 id) { return (((unsigned int)id) & NodeImpl_IdNSMask) >> 16; }\n\
126 inline quint32 makeId(quint16 n, quint16 l) { return (n << 16) | l; }\n\
128 const quint32 anyQName = makeId(anyNamespace, anyLocalName);\n\
132 out
.write("namespace khtml {\n\
134 class NamespaceFactory {\n\
136 static IDTable<NamespaceFactory>* idTable() {\n\
139 static IDTable<NamespaceFactory>* initIdTable();\n\
141 static IDTable<NamespaceFactory>* s_idTable;\n\
144 class LocalNameFactory {\n\
146 static IDTable<LocalNameFactory>* idTable() {\n\
149 static IDTable<LocalNameFactory>* initIdTable();\n\
151 static IDTable<LocalNameFactory>* s_idTable;\n\
154 class PrefixFactory {\n\
156 static IDTable<PrefixFactory>* idTable() {\n\
159 static IDTable<PrefixFactory>* initIdTable();\n\
161 static IDTable<PrefixFactory>* s_idTable;\n\
166 out
.write("namespace DOM {\n\
168 typedef khtml::IDString<khtml::NamespaceFactory> NamespaceName;\n\
169 typedef khtml::IDString<khtml::LocalNameFactory> LocalName;\n\
170 typedef khtml::IDString<khtml::PrefixFactory> PrefixName;\n\
171 extern PrefixName emptyPrefixName;\n\
172 extern LocalName emptyLocalName;\n\
173 extern NamespaceName emptyNamespaceName;\n\
175 QString getPrintableName(int id);\n\n")
177 out
.write("#endif\n")
183 if prev
and prev
[0] == i
[0]: continue
184 if i
[2] == "ID_TEXT" or i
[2] == "ID_COMMENT":
185 temp
= temp
+ (" s_idTable->addHiddenMapping(%s, \"%s\");\n" % (i
[2], i
[1]))
187 temp
= temp
+ (" s_idTable->addStaticMapping(localNamePart(%s), \"%s\");\n" % (i
[2], i
[1]))
190 out
= open("htmlnames.cpp", "w")
191 out
.write("#include \"misc/htmlnames.h\"\n")
192 out
.write("#include \"dom/dom_string.h\"\n")
193 out
.write("\nusing namespace DOM;\n")
195 out
.write("namespace khtml {\n\n")
196 out
.write("IDTable<NamespaceFactory>* NamespaceFactory::s_idTable;\n\
197 IDTable<NamespaceFactory>* NamespaceFactory::initIdTable()\n\
199 s_idTable = new IDTable<NamespaceFactory>();\n\
200 s_idTable->addStaticMapping(DOM::xhtmlNamespace, XHTML_NAMESPACE);\n\
201 s_idTable->addStaticMapping(DOM::emptyNamespace, DOMString());\n\
202 s_idTable->addStaticMapping(DOM::svgNamespace, SVG_NAMESPACE);\n\
203 s_idTable->addStaticMapping(DOM::xlinkNamespace, XLINK_NAMESPACE);\n\
207 IDTable<LocalNameFactory>* LocalNameFactory::s_idTable;\n\
208 IDTable<LocalNameFactory>* LocalNameFactory::initIdTable()\n\
210 s_idTable = new IDTable<LocalNameFactory>();\n\
211 s_idTable->addStaticMapping(0, DOMString());\n\
216 IDTable<PrefixFactory>* PrefixFactory::s_idTable;\n\
217 IDTable<PrefixFactory>* PrefixFactory::initIdTable()\n\
219 s_idTable = new IDTable<PrefixFactory>();\n\
220 s_idTable->addStaticMapping(DOM::emptyPrefix, DOMString());\n\
225 out
.write("namespace DOM {\n\n")
226 out
.write("LocalName emptyLocalName;// = LocalName::fromId(0);\n")
227 out
.write("PrefixName emptyPrefixName;// = PrefixName::fromId(0);\n")
228 out
.write("NamespaceName emptyNamespaceName;// = NamespaceName::fromId(0);\n")
230 out
.write("""QString getPrintableName(int id) {
231 QString local = QString("null");
232 QString namespacename = QString("null");
234 if (localNamePart(id) != anyLocalName) {
235 DOMString localName = LocalName::fromId(localNamePart(id)).toString();
236 if (localName.implementation())
237 local = localName.string();
242 if (namespacePart(id) != anyNamespace) {
243 DOMString namespaceName = NamespaceName::fromId(namespacePart(id)).toString();
244 if (namespaceName.implementation())
245 namespacename = namespaceName.string();
249 return "{ns:" + QString::number(namespacePart(id)) + ",[" + namespacename + "] local:" + QString::number(localNamePart(id)) + ",[" + local + "]}";
254 # kate: indent-width 4; replace-tabs on; tab-width 4; space-indent on;