annotate browser_side/nativedom.py @ 411:a0256b81d367

setup.py: update website (it's not http://www.salut-a-toi.org), and removed pyjamas framework which is not standard in pypi
author Goffi <goffi@goffi.org>
date Tue, 18 Mar 2014 19:18:16 +0100
parents ce5b33f499c5
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
142
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
3
339
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 337
diff changeset
4 # Libervia: a Salut à Toi frontend
340
ce5b33f499c5 dates update
Goffi <goffi@goffi.org>
parents: 339
diff changeset
5 # Copyright (C) 2011, 2012, 2013, 2014 Jérôme Poisson <goffi@goffi.org>
142
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
6
339
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 337
diff changeset
7 # This program is free software: you can redistribute it and/or modify
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 337
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 337
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 337
diff changeset
10 # (at your option) any later version.
142
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
11
339
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 337
diff changeset
12 # This program is distributed in the hope that it will be useful,
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 337
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 337
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 337
diff changeset
15 # GNU Affero General Public License for more details.
142
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
16
339
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 337
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 337
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
142
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
19
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
21 This class provide basic DOM parsing based on native javascript parser
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
22 __init__ code comes from Tim Down at http://stackoverflow.com/a/8412989
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
23 """
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
24
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from __pyjamas__ import JS
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
26
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
27
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
28 class Node():
245
43881c3dda9d browser side: added Node.setAttribute and Node.toxml to nativedom
Goffi <goffi@goffi.org>
parents: 165
diff changeset
29
142
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
30 def __init__(self, js_node):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
31 self._node = js_node
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
32
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
33 def _jsNodesList2List(self, js_nodes_list):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
34 ret=[]
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
35 for i in range(len(js_nodes_list)):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
36 #ret.append(Element(js_nodes_list.item(i)))
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
37 ret.append(self.__class__(js_nodes_list.item(i))) # XXX: Ugly, but used to word around a Pyjamas's bug
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
38 return ret
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
39
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
40 @property
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
41 def nodeName(self):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
42 return self._node.nodeName
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
43
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
44 @property
337
ea1be522ba88 browser side: XMLUI fixes:
Goffi <goffi@goffi.org>
parents: 245
diff changeset
45 def wholeText(self):
ea1be522ba88 browser side: XMLUI fixes:
Goffi <goffi@goffi.org>
parents: 245
diff changeset
46 return self._node.wholeText
ea1be522ba88 browser side: XMLUI fixes:
Goffi <goffi@goffi.org>
parents: 245
diff changeset
47
ea1be522ba88 browser side: XMLUI fixes:
Goffi <goffi@goffi.org>
parents: 245
diff changeset
48 @property
142
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
49 def childNodes(self):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
50 return self._jsNodesList2List(self._node.childNodes)
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
51
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
52 def getAttribute(self, attr):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
53 return self._node.getAttribute(attr)
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
54
245
43881c3dda9d browser side: added Node.setAttribute and Node.toxml to nativedom
Goffi <goffi@goffi.org>
parents: 165
diff changeset
55 def setAttribute(self, attr, value):
43881c3dda9d browser side: added Node.setAttribute and Node.toxml to nativedom
Goffi <goffi@goffi.org>
parents: 165
diff changeset
56 return self._node.setAttribute(attr, value)
43881c3dda9d browser side: added Node.setAttribute and Node.toxml to nativedom
Goffi <goffi@goffi.org>
parents: 165
diff changeset
57
142
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
58 def hasAttribute(self, attr):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
59 return self._node.hasAttribute(attr)
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
60
245
43881c3dda9d browser side: added Node.setAttribute and Node.toxml to nativedom
Goffi <goffi@goffi.org>
parents: 165
diff changeset
61 def toxml(self):
43881c3dda9d browser side: added Node.setAttribute and Node.toxml to nativedom
Goffi <goffi@goffi.org>
parents: 165
diff changeset
62 return JS("""this._node.outerHTML || new XMLSerializer().serializeToString(this._node);""")
43881c3dda9d browser side: added Node.setAttribute and Node.toxml to nativedom
Goffi <goffi@goffi.org>
parents: 165
diff changeset
63
142
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
64
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
65 class Element(Node):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
66
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
67 def __init__(self, js_node):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
68 Node.__init__(self, js_node)
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
69
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
70 def getElementsByTagName(self, tagName):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
71 return self._jsNodesList2List(self._node.getElementsByTagName(tagName))
245
43881c3dda9d browser side: added Node.setAttribute and Node.toxml to nativedom
Goffi <goffi@goffi.org>
parents: 165
diff changeset
72
43881c3dda9d browser side: added Node.setAttribute and Node.toxml to nativedom
Goffi <goffi@goffi.org>
parents: 165
diff changeset
73
142
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
74 class Document(Node):
245
43881c3dda9d browser side: added Node.setAttribute and Node.toxml to nativedom
Goffi <goffi@goffi.org>
parents: 165
diff changeset
75
142
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
76 def __init__(self, js_document):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
77 Node.__init__(self, js_document)
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
78
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
79 @property
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
80 def documentElement(self):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
81 return Element(self._node.documentElement)
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
82
245
43881c3dda9d browser side: added Node.setAttribute and Node.toxml to nativedom
Goffi <goffi@goffi.org>
parents: 165
diff changeset
83
142
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
84 class NativeDOM:
245
43881c3dda9d browser side: added Node.setAttribute and Node.toxml to nativedom
Goffi <goffi@goffi.org>
parents: 165
diff changeset
85
142
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
86 def __init__(self):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
87 JS("""
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
88
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
89 if (typeof window.DOMParser != "undefined") {
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
90 this.parseXml = function(xmlStr) {
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
91 return ( new window.DOMParser() ).parseFromString(xmlStr, "text/xml");
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
92 };
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
93 } else if (typeof window.ActiveXObject != "undefined" &&
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
94 new window.ActiveXObject("Microsoft.XMLDOM")) {
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
95 this.parseXml = function(xmlStr) {
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
96 var xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM");
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
97 xmlDoc.async = "false";
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
98 xmlDoc.loadXML(xmlStr);
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
99 return xmlDoc;
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
100 };
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
101 } else {
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
102 throw new Error("No XML parser found");
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
103 }
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
104 """)
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
105
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
106 def parseString(self, xml):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
107 return Document(self.parseXml(xml))
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
108