-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
154 lines (119 loc) · 3.23 KB
/
index.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
'use strict';
const EventEmitter = require('events').EventEmitter;
const setCookiePattern = /^(\w+)=(.*?)(;(.*?))?$/;
const settingsPattern = /(\w+)=(.*?)(;|$)/g;
class Mockdock extends EventEmitter {
constructor(data) {
super();
this.eventListeners = {};
this._cookie = '';
this.selector = data && data.selector;
this.attributes = data && data.attributes || {};
this.children = (data && data.children) ? data.children : [];
this.setCookies = {};
this.create = [];
this.style = {};
this.readyState = 'complete';
}
get cookie() {
return this._cookie;
}
set cookie(value) {
const parsedCookie = parseSetCookie.call(this, value);
if (!parsedCookie.cookie) throw new Error(`Not a valid cookie: ${value}`);
this._cookie += parsedCookie.cookie;
}
addEventListener(type, fn) {
if (!this.eventListeners[type]) {
this.eventListeners[type] = [];
}
this.eventListeners[type].push(fn);
}
triggerEventListener(type) {
if (!this.eventListeners[type]) return;
this.eventListeners[type].forEach((fn) => {
fn();
});
}
createElement(type) {
const document = new Mockdock();
if (type) {
document.localName = type.toLowerCase();
}
if (type === 'video') {
document.canPlayType = () => {
return '';
};
}
this.children.push(document);
document.on('setAttribute', (value) => {
this.emit('setAttribute', value);
});
document.on('removeAttribute', (value) => {
this.emit('removeAttribute', value);
});
return document;
}
getElementsByTagName(tag) {
return this.children.filter((c) => c.localName === tag.toLowerCase());
}
removeAttribute(attrName) {
if (attrName === 'id') {
delete this.id;
}
delete this.attributes[attrName];
this.emit('removeAttribute', attrName, this);
}
setAttribute(cmd, value) {
this.attributes[cmd] = value;
this.emit('setAttribute', value, this);
}
hasAttribute(attrName) {
return this.attributes[attrName];
}
domContentLoaded() {
this.emit('DOMContentLoaded');
}
getElementById(id) {
return this.children.find((elm) => elm.id === id);
}
querySelector(selectors) {
return this.children.find((child) => {
return child.selector === selectors;
});
}
querySelectorAll() {
return this.children;
}
getAttribute(name) {
return this.attributes && this.attributes[name];
}
appendChild(element) {
this.children.push(element);
this.emit('appendChild', element);
return element;
}
}
function parseSetCookie(cookie) {
const parsed = {};
if (!/;$/.test(cookie)) cookie = `${cookie};`;
cookie.trim().replace(setCookiePattern, (match, name, value, settings) => {
parsed.cookie = `${name}=${value};`;
parsed.name = name;
parsed.value = decodeURIComponent(value);
Object.assign(parsed, parseSettings(settings));
});
if (parsed.name) {
this.setCookies[parsed.name] = parsed;
}
return parsed;
}
function parseSettings(settings) {
const parsed = {};
settings.replace(settingsPattern, (match, name, value) => {
if (name === 'expires') value = new Date(value);
parsed[name] = value;
});
return parsed;
}
module.exports = Mockdock;