forked from svgdotjs/svgdom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdom.js
167 lines (151 loc) · 3.74 KB
/
dom.js
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
const {invent, extend} = require('./utils/objectCreationUtils')
const EventTarget = require('./class/EventTarget')
const SVGPoint = require('./class/SVGPoint')
const SVGMatrix = require('./class/SVGMatrix')
const {SVGElement, Node, TextNode} = require('./class/Node')
const sizeOf = require('image-size')
const path = require('path')
const fontkit = require('fontkit')
var HTMLImageElement = invent({
name: 'HTMLImageElement',
create: function(){
Node.call(this, 'img')
this.width = 0
this.height = 0
this.naturalWidth = 0
this.naturalHeight = 0
},
inherit: Node,
props: {
src: {
get: function() {
return this.attrs.get('src')
},
set: function(val) {
this.attrs.set('src', val)
sizeOf(val, function (err, size) {
if(err){
this.dispatchEvent(new Event('error', this))
return
}
this.width = this.naturalWidth = size.width
this.height = this.naturalHeight = size.height
this.dispatchEvent(new Event('load', this))
}.bind(this));
}
},
height: {
get: function() {
return this.attrs.get('height')
},
set: function(val) {
this.attrs.set('height', val)
}
},
height: {
get: function() {
return this.attrs.get('height')
},
set: function(val) {
this.attrs.set('height', val)
}
},
}
})
var Event = invent({
name: 'Event',
create: function(type){
this.type = type
this.cancelable = false
this.defaultPrevented = false
},
extend: {
preventDefault: function(){
this.defaultPrevented = true
}
}
})
var CustomEvent = invent({
name: 'CustomEvent',
create: function(name, props = {}) {
Event.call(this, name)
this.detail = props.detail || null
this.cancelable = props.cancelable || false
},
inherit: Event
})
var Document = invent({
name: 'Document',
create: function(root) {
Node.call(this, '#document')
this.nodeType = 9
var root = this.createElement(root)
this.appendChild(root)
root.ownerDocument = this
this.documentElement = root
this._preloaded = {}
},
inherit: Node,
extend: {
createElementNS: function(ns, name){
return new SVGElement(name, {
attrs: {xmlns: ns},
ownerDocument: this
})
},
createElement: function(name) {
return new SVGElement(name, {ownerDocument: this})
},
createTextNode: function(text) {
return new TextNode('#text', {data:text, ownerDocument: this})
},
createAttribute: function(name) {
return new AttributeNode(name, {ownerDocument: this})
}
}
})
var Window = invent({
create: function() {
EventTarget.call(this)
this.document = new Document('svg')
},
inherit: EventTarget,
extend: {
setFontDir: function(dir) {
this.document.fontDir = dir
return this
},
setFontFamilyMappings: function(map) {
this.document.fontFamilyMappings = map
return this
},
preloadFonts: function() {
var map = this.document.fontFamilyMappings, filename, font
for(var i in map) {
filename = path.join(this.document.fontDir, map[i])
try{
this.document._preloaded[i] = fontkit.openSync(filename)
}catch(e){
console.warn('Could not load font file for ' + i + '.' + e)
}
}
return this
}
}
})
extend(Window, {
Node: Node,
TextNode: TextNode,
SVGElement: SVGElement,
CustomEvent: CustomEvent,
Event: Event,
SVGMatrix: SVGMatrix,
SVGPoint: SVGPoint,
Image: HTMLImageElement,
HTMLImageElement: HTMLImageElement,
setTimeout: setTimeout,
clearTimeout: clearTimeout,
pageXOffset: 0,
pageYOffset: 0
})
module.exports = new Window