forked from akira-cn/sublime-v8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdom.py
220 lines (183 loc) · 5.09 KB
/
dom.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
from BeautifulSoup import BeautifulSoup, Tag, NavigableString, Comment
from PyV8 import JSClass, JSArray
def normalizeNode(node = None):
if('_Node__wrap' in dir(node)):
return node._Node__wrap
if(isinstance(node, BeautifulSoup)): #Document
return Document(node)
if(isinstance(node, Tag)): #ElementNode
return ElementNode(node)
elif(isinstance(node, NavigableString)): #TextNode
return TextNode(node)
elif(isinstance(node, Comment)): #CommentNode
return CommentNode(node)
return node
class Document(JSClass):
def __init__(self, source):
if(not isinstance(source, BeautifulSoup)):
source = BeautifulSoup(source)
self._soup = source
self.nodeType = 9
def getElementById(self, id):
node = self._soup.find(id=id)
return normalizeNode(node)
def getElementsByTagName(self, tagName):
if(tagName == '*'):
tagName = True
nodelist = self._soup.findAll(tagName)
return JSArray([normalizeNode(node) for node in nodelist])
def getElementsByClassName(self, className):
nodelist = self._soup.findAll(None, {"class":className})
return JSArray([normalizeNode(node) for node in nodelist])
def createElement(self, tagName):
return normalizeNode(BeautifulSoup(tagName).contents[0])
@property
def body(self):
return normalizeNode(self._soup.find('body'))
class Node(JSClass, object): #baseclass
def __init__(self, soup):
soup.__wrap = self
self._soup = soup
@property
def parentNode(self):
return normalizeNode(self._soup.parent)
@property
def parentElement(self):
return normalizeNode(self._soup.parent)
@property
def ownerDocument(self):
return normalizeNode(self._soup.findParents()[-1])
@property
def nextSibling(self):
return normalizeNode(self._soup.nextSibling)
@property
def previousSibling(self):
return normalizeNode(self._soup.previousSibling)
class ElementNode(Node):
def __init__(self, node):
self.nodeType = 1
Node.__init__(self, node)
def getElementsByTagName(self, tagName):
if(tagName == '*'):
tagName = True
nodelist = self._soup.findAll(tagName)
return JSArray([normalizeNode(node) for node in nodelist])
def getElementsByClassName(self, className):
nodelist = self._soup.findAll(None, {"class":className})
return JSArray([normalizeNode(node) for node in nodelist])
@property
def tagName(self):
return self._soup.name.upper()
@property
def nodeName(self):
return self._soup.name.upper()
@property
def childNodes(self):
node = self._soup
return JSArray([normalizeNode(node) for node in node.contents])
@property
def firstChild(self):
node = self._soup
if(len(node.contents)):
return normalizeNode(node.contents[0])
def lastChild(self):
node = self._soup
if(len(node.contents)):
return normalizeNode(node.contents[-1])
@property
def children(self):
return JSArray(filter(lambda n: n.nodeType == 1, self.childNodes))
@property
def innerHTML(self):
node = self._soup
return ''.join([unicode(n).encode('utf-8') for n in node.contents])
@innerHTML.setter
def innerHTML(self, html):
self._soup.contents = BeautifulSoup(html).contents
@property
def outerHTML(self):
return unicode(self._soup).encode('utf-8')
@property
def textContent(self):
return self._soup.getText()
@property
def name(self):
return self._soup['name']
@name.setter
def name(self, value):
self._soup['name'] = value
@property
def id(self):
return self._soup['id']
@id.setter
def id(self, value):
self._soup['id'] = value
@property
def value(self):
return self._soup['value']
@value.setter
def value(self, value):
self._soup['value'] = value
@property
def className(self):
return self._soup['class']
@className.setter
def className(self, value):
self._soup['class'] = value
@property
def selected(self):
return self._soup['selected'] == 'selected'
@selected.setter
def selected(self, value):
if(value == True):
value = "selected"
self._soup['selected'] = value
@property
def checked(self):
return self._soup['checked'] == 'checked'
@checked.setter
def checked(self, value):
if(value == True):
value = "checked"
self._soup['checked'] = value
@property
def disabled(self):
return self._soup['disabled'] == 'disabled'
@disabled.setter
def disabled(self, value):
if(value == True):
value = "disabled"
self._soup['disabled'] = value
@property
def type(self):
return self._soup['type']
@property
def readOnly(self):
pass
def appendChild(self, node):
return self._soup.append(node)
def removeChild(self, node):
return self._soup.contents.remove(node._soup)
def getAttribute(self, attr):
return self._soup[attr]
def setAttribute(self, attr, value):
self._soup[attr] = value
def removeAttribute(self, attr):
del self._soup[attr]
class TextNode(Node):
def __init__(self, node):
self.nodeType = 3
self.nodeName = "#text"
Node.__init__(self, node)
@property
def nodeValue(self):
return self._soup.encode('utf-8')
class CommentNode(Node):
def __init__(self, node):
self.nodeType = 8
self.nodeName = "#comment"
self.nodeValue = node.encode('utf-8')
Node.__init__(self, node)
@property
def nodeValue(self):
return self._soup.encode('utf-8')