annotate browser_side/nativedom.py @ 312:b4781a350483

browser_side: display a "New message" button and add a "comment" icon for main entries in MicroblogPanel when the unibox is disabled
author souliane <souliane@mailoo.org>
date Mon, 30 Dec 2013 00:33:19 +0100
parents 43881c3dda9d
children ea1be522ba88
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
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
4 """
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
5 Libervia: a Salut à Toi frontend
165
9763dec220ed dates update
Goffi <goffi@goffi.org>
parents: 142
diff changeset
6 Copyright (C) 2011, 2012, 2013 Jérôme Poisson <goffi@goffi.org>
142
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
7
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
9 it under the terms of the GNU Affero General Public License as published by
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
11 (at your option) any later version.
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
12
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
16 GNU Affero General Public License for more details.
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
17
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
18 You should have received a copy of the GNU Affero General Public License
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
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
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
22 """
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
23 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
24 __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
25 """
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 from __pyjamas__ import JS
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
28
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
29
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
30 class Node():
245
43881c3dda9d browser side: added Node.setAttribute and Node.toxml to nativedom
Goffi <goffi@goffi.org>
parents: 165
diff changeset
31
142
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
32 def __init__(self, js_node):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
33 self._node = js_node
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
34
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
35 def _jsNodesList2List(self, js_nodes_list):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
36 ret=[]
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
37 for i in range(len(js_nodes_list)):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
38 #ret.append(Element(js_nodes_list.item(i)))
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
39 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
40 return ret
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
41
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
42 @property
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
43 def nodeName(self):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
44 return self._node.nodeName
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
45
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
46 @property
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
47 def childNodes(self):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
48 return self._jsNodesList2List(self._node.childNodes)
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
49
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
50 def getAttribute(self, attr):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
51 return self._node.getAttribute(attr)
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
52
245
43881c3dda9d browser side: added Node.setAttribute and Node.toxml to nativedom
Goffi <goffi@goffi.org>
parents: 165
diff changeset
53 def setAttribute(self, attr, value):
43881c3dda9d browser side: added Node.setAttribute and Node.toxml to nativedom
Goffi <goffi@goffi.org>
parents: 165
diff changeset
54 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
55
142
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
56 def hasAttribute(self, attr):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
57 return self._node.hasAttribute(attr)
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
58
245
43881c3dda9d browser side: added Node.setAttribute and Node.toxml to nativedom
Goffi <goffi@goffi.org>
parents: 165
diff changeset
59 def toxml(self):
43881c3dda9d browser side: added Node.setAttribute and Node.toxml to nativedom
Goffi <goffi@goffi.org>
parents: 165
diff changeset
60 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
61
142
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
62
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
63 class Element(Node):
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 def __init__(self, js_node):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
66 Node.__init__(self, js_node)
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
67
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
68 def getElementsByTagName(self, tagName):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
69 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
70
43881c3dda9d browser side: added Node.setAttribute and Node.toxml to nativedom
Goffi <goffi@goffi.org>
parents: 165
diff changeset
71
142
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
72 class Document(Node):
245
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 def __init__(self, js_document):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
75 Node.__init__(self, js_document)
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
76
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
77 @property
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
78 def documentElement(self):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
79 return Element(self._node.documentElement)
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
80
245
43881c3dda9d browser side: added Node.setAttribute and Node.toxml to nativedom
Goffi <goffi@goffi.org>
parents: 165
diff changeset
81
142
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
82 class NativeDOM:
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 def __init__(self):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
85 JS("""
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
86
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
87 if (typeof window.DOMParser != "undefined") {
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
88 this.parseXml = function(xmlStr) {
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
89 return ( new window.DOMParser() ).parseFromString(xmlStr, "text/xml");
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
90 };
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
91 } else if (typeof window.ActiveXObject != "undefined" &&
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
92 new window.ActiveXObject("Microsoft.XMLDOM")) {
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
93 this.parseXml = function(xmlStr) {
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
94 var xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM");
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
95 xmlDoc.async = "false";
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
96 xmlDoc.loadXML(xmlStr);
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
97 return xmlDoc;
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
98 };
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
99 } else {
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
100 throw new Error("No XML parser found");
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
101 }
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
102 """)
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 def parseString(self, xml):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
105 return Document(self.parseXml(xml))
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
106