/
dom.coffee
56 lines (54 loc) · 1.54 KB
/
dom.coffee
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
class ElementShim
constructor: (@tagName, @text, @attributes) ->
@nodeName = @tagName
@attributes ?= {}
@children = []
nodeType: 1
name: -> @tagName
getAttribute: (attribute) ->
if @attributes.hasOwnProperty(attribute) && @attributes[attribute] != undefined
@attributes[attribute].toString()
else
''
compareDocumentPosition: (that) ->
if this.attributes.id < that.attributes.id
4
else
2
getElementsByTagName: (name) ->
if name == "*"
@children
else
result = []
result = result.concat(child for child in @children when child.tagName == name)
for child in @children
result = result.concat(child.getElementsByTagName(name))
result
appendChild: (child) ->
@children.push(child)
child.parentNode = this
uuid: () -> [@attributes.id, @attributes.sentenceId].toString()
class DocumentShim
constructor: (@tags) ->
@tags ?= []
nodeType: 9
getElementsByTagName: (name) ->
if name == "*"
@tags.all
else
@tags[name] || []
documentElement: do ->
removeChild: (node) -> # this.tags[node.nodeName].splice(this.tags[node.nodeName].indexOf(node), 1)
createComment: () -> {}
createElement: (tagName, text, attributes) ->
element = new ElementShim(tagName, text, attributes)
(@tags[tagName] ?= []).push(element)
element.parentNode = @documentElement
element.ownerDocument = this
element
getElementById: () -> []
clear: () ->
@tags = []
module.exports =
ElementShim: ElementShim
DocumentShim: DocumentShim