annotate src/browser/sat_browser/nativedom.py @ 754:d3fa1e8904cf

browser_side: display the number of alerts for each contact and not only a symbol (*) + favicon displays the total number of waiting messages
author souliane <souliane@mailoo.org>
date Tue, 24 Nov 2015 13:33:19 +0100
parents 9877607c719a
children f8a7a046ff9c
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
685
9877607c719a 2015 copyright dates update
Goffi <goffi@goffi.org>
parents: 589
diff changeset
5 # Copyright (C) 2011, 2012, 2013, 2014, 2015 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
488
747eaa662353 browser side (nativedom): new properties managed: TEXT_NODE, ELEMENT_NODE, ATTRIBUTE_NODE, COMMENT_NODE, nodeType
Goffi <goffi@goffi.org>
parents: 467
diff changeset
28 class Node(object):
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):
451
1a0cec9b0f1e better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 449
diff changeset
34 ret = []
142
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)))
589
a5019e62c3e9 browser side: big refactoring to base Libervia on QuickFrontend, first draft:
Goffi <goffi@goffi.org>
parents: 488
diff changeset
37 ret.append(self.__class__(js_nodes_list.item(i))) # XXX: Ugly, but used to work around a Pyjamas's bug
142
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
488
747eaa662353 browser side (nativedom): new properties managed: TEXT_NODE, ELEMENT_NODE, ATTRIBUTE_NODE, COMMENT_NODE, nodeType
Goffi <goffi@goffi.org>
parents: 467
diff changeset
40 def __getattr__(self, name):
747eaa662353 browser side (nativedom): new properties managed: TEXT_NODE, ELEMENT_NODE, ATTRIBUTE_NODE, COMMENT_NODE, nodeType
Goffi <goffi@goffi.org>
parents: 467
diff changeset
41 if name in ('TEXT_NODE', 'ELEMENT_NODE', 'ATTRIBUTE_NODE', 'COMMENT_NODE', 'nodeName', 'nodeType', 'wholeText'):
747eaa662353 browser side (nativedom): new properties managed: TEXT_NODE, ELEMENT_NODE, ATTRIBUTE_NODE, COMMENT_NODE, nodeType
Goffi <goffi@goffi.org>
parents: 467
diff changeset
42 return getattr(self._node, name)
747eaa662353 browser side (nativedom): new properties managed: TEXT_NODE, ELEMENT_NODE, ATTRIBUTE_NODE, COMMENT_NODE, nodeType
Goffi <goffi@goffi.org>
parents: 467
diff changeset
43 return object.__getattribute__(self, name)
337
ea1be522ba88 browser side: XMLUI fixes:
Goffi <goffi@goffi.org>
parents: 245
diff changeset
44
589
a5019e62c3e9 browser side: big refactoring to base Libervia on QuickFrontend, first draft:
Goffi <goffi@goffi.org>
parents: 488
diff changeset
45 @property # XXX: doesn't work in --strict mode in pyjs
142
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
46 def childNodes(self):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
47 return self._jsNodesList2List(self._node.childNodes)
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
48
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
49 def getAttribute(self, attr):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
50 return self._node.getAttribute(attr)
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
51
245
43881c3dda9d browser side: added Node.setAttribute and Node.toxml to nativedom
Goffi <goffi@goffi.org>
parents: 165
diff changeset
52 def setAttribute(self, attr, value):
43881c3dda9d browser side: added Node.setAttribute and Node.toxml to nativedom
Goffi <goffi@goffi.org>
parents: 165
diff changeset
53 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
54
142
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
55 def hasAttribute(self, attr):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
56 return self._node.hasAttribute(attr)
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
57
245
43881c3dda9d browser side: added Node.setAttribute and Node.toxml to nativedom
Goffi <goffi@goffi.org>
parents: 165
diff changeset
58 def toxml(self):
43881c3dda9d browser side: added Node.setAttribute and Node.toxml to nativedom
Goffi <goffi@goffi.org>
parents: 165
diff changeset
59 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
60
142
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
61
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
62 class Element(Node):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
63
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
64 def __init__(self, js_node):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
65 Node.__init__(self, js_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 getElementsByTagName(self, tagName):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
68 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
69
43881c3dda9d browser side: added Node.setAttribute and Node.toxml to nativedom
Goffi <goffi@goffi.org>
parents: 165
diff changeset
70
142
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
71 class Document(Node):
245
43881c3dda9d browser side: added Node.setAttribute and Node.toxml to nativedom
Goffi <goffi@goffi.org>
parents: 165
diff changeset
72
142
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
73 def __init__(self, js_document):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
74 Node.__init__(self, js_document)
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
75
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
76 @property
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
77 def documentElement(self):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
78 return Element(self._node.documentElement)
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
79
245
43881c3dda9d browser side: added Node.setAttribute and Node.toxml to nativedom
Goffi <goffi@goffi.org>
parents: 165
diff changeset
80
142
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
81 class NativeDOM:
245
43881c3dda9d browser side: added Node.setAttribute and Node.toxml to nativedom
Goffi <goffi@goffi.org>
parents: 165
diff changeset
82
142
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
83 def __init__(self):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
84 JS("""
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
85
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
86 if (typeof window.DOMParser != "undefined") {
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
87 this.parseXml = function(xmlStr) {
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
88 return ( new window.DOMParser() ).parseFromString(xmlStr, "text/xml");
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
89 };
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
90 } else if (typeof window.ActiveXObject != "undefined" &&
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
91 new window.ActiveXObject("Microsoft.XMLDOM")) {
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
92 this.parseXml = function(xmlStr) {
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
93 var xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM");
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
94 xmlDoc.async = "false";
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
95 xmlDoc.loadXML(xmlStr);
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
96 return xmlDoc;
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
97 };
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
98 } else {
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
99 throw new Error("No XML parser found");
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 """)
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 def parseString(self, xml):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
104 return Document(self.parseXml(xml))