| 
									
										
										
										
											2018-04-16 23:43:45 +08:00
										 |  |  | module.exports = class FakeDocument { | 
					
						
							|  |  |  | 	constructor() { | 
					
						
							|  |  |  | 		this.head = this.createElement("head"); | 
					
						
							| 
									
										
										
										
											2020-08-05 05:42:29 +08:00
										 |  |  | 		this.baseURI = "https://test.cases/path/index.html"; | 
					
						
							| 
									
										
										
										
											2019-09-26 21:52:19 +08:00
										 |  |  | 		this._elementsByTagName = new Map([["head", [this.head]]]); | 
					
						
							| 
									
										
										
										
											2018-04-16 23:43:45 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	createElement(type) { | 
					
						
							| 
									
										
										
										
											2019-09-26 21:52:19 +08:00
										 |  |  | 		return new FakeElement(this, type); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	_onElementAttached(element) { | 
					
						
							|  |  |  | 		const type = element._type; | 
					
						
							|  |  |  | 		let list = this._elementsByTagName.get(type); | 
					
						
							|  |  |  | 		if (list === undefined) { | 
					
						
							|  |  |  | 			list = []; | 
					
						
							|  |  |  | 			this._elementsByTagName.set(type, list); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		list.push(element); | 
					
						
							| 
									
										
										
										
											2018-04-16 23:43:45 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-03 21:04:54 +08:00
										 |  |  | 	_onElementRemoved(element) { | 
					
						
							|  |  |  | 		const type = element._type; | 
					
						
							|  |  |  | 		let list = this._elementsByTagName.get(type); | 
					
						
							|  |  |  | 		const idx = list.indexOf(element); | 
					
						
							|  |  |  | 		list.splice(idx, 1); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-16 23:43:45 +08:00
										 |  |  | 	getElementsByTagName(name) { | 
					
						
							| 
									
										
										
										
											2019-09-26 21:52:19 +08:00
										 |  |  | 		return this._elementsByTagName.get(name) || []; | 
					
						
							| 
									
										
										
										
											2018-04-16 23:43:45 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class FakeElement { | 
					
						
							| 
									
										
										
										
											2019-09-26 21:52:19 +08:00
										 |  |  | 	constructor(document, type) { | 
					
						
							|  |  |  | 		this._document = document; | 
					
						
							| 
									
										
										
										
											2018-04-16 23:43:45 +08:00
										 |  |  | 		this._type = type; | 
					
						
							|  |  |  | 		this._children = []; | 
					
						
							|  |  |  | 		this._attributes = Object.create(null); | 
					
						
							| 
									
										
										
										
											2018-06-28 17:03:08 +08:00
										 |  |  | 		this._src = undefined; | 
					
						
							|  |  |  | 		this._href = undefined; | 
					
						
							| 
									
										
										
										
											2020-06-03 21:04:54 +08:00
										 |  |  | 		this.parentNode = undefined; | 
					
						
							| 
									
										
										
										
											2018-04-16 23:43:45 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	appendChild(node) { | 
					
						
							| 
									
										
										
										
											2019-09-26 21:52:19 +08:00
										 |  |  | 		this._document._onElementAttached(node); | 
					
						
							| 
									
										
										
										
											2018-04-16 23:43:45 +08:00
										 |  |  | 		this._children.push(node); | 
					
						
							| 
									
										
										
										
											2020-06-03 21:04:54 +08:00
										 |  |  | 		node.parentNode = this; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	removeChild(node) { | 
					
						
							|  |  |  | 		const idx = this._children.indexOf(node); | 
					
						
							|  |  |  | 		if (idx >= 0) { | 
					
						
							|  |  |  | 			this._children.splice(idx, 1); | 
					
						
							|  |  |  | 			this._document._onElementRemoved(node); | 
					
						
							|  |  |  | 			node.parentNode = undefined; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-04-16 23:43:45 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	setAttribute(name, value) { | 
					
						
							|  |  |  | 		this._attributes[name] = value; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	getAttribute(name) { | 
					
						
							|  |  |  | 		return this._attributes[name]; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-06-28 17:03:08 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	_toRealUrl(value) { | 
					
						
							|  |  |  | 		if (/^\//.test(value)) { | 
					
						
							|  |  |  | 			return `https://test.cases${value}`; | 
					
						
							|  |  |  | 		} else if (/^\.\.\//.test(value)) { | 
					
						
							|  |  |  | 			return `https://test.cases${value.substr(2)}`; | 
					
						
							|  |  |  | 		} else if (/^\.\//.test(value)) { | 
					
						
							|  |  |  | 			return `https://test.cases/path${value.substr(1)}`; | 
					
						
							|  |  |  | 		} else if (/^\w+:\/\//.test(value)) { | 
					
						
							|  |  |  | 			return value; | 
					
						
							|  |  |  | 		} else if (/^\/\//.test(value)) { | 
					
						
							|  |  |  | 			return `https:${value}`; | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			return `https://test.cases/path/${value}`; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	set src(value) { | 
					
						
							|  |  |  | 		if (this._type === "script") { | 
					
						
							|  |  |  | 			this._src = this._toRealUrl(value); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	get src() { | 
					
						
							|  |  |  | 		return this._src; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	set href(value) { | 
					
						
							|  |  |  | 		if (this._type === "link") { | 
					
						
							|  |  |  | 			this._href = this._toRealUrl(value); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	get href() { | 
					
						
							|  |  |  | 		return this._href; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-04-16 23:43:45 +08:00
										 |  |  | } |