86 lines
1.6 KiB
TypeScript
86 lines
1.6 KiB
TypeScript
import { JSDOM } from "jsdom";
|
|
|
|
export class DOMShim
|
|
{
|
|
private static _instance:DOMShim = null;
|
|
|
|
|
|
static get $():DOMShim
|
|
{
|
|
if ( this._instance )
|
|
{
|
|
return this._instance
|
|
}
|
|
|
|
this._instance = new DOMShim();
|
|
return this._instance;
|
|
}
|
|
|
|
private _applied = false;
|
|
private _jsDOM:JSDOM;
|
|
|
|
constructor()
|
|
{
|
|
this._apply();
|
|
}
|
|
|
|
setDocument( html:string )
|
|
{
|
|
this._jsDOM = new JSDOM( html );
|
|
( global as any ).window = this._jsDOM.window;
|
|
( global as any ).document = this._jsDOM.window.document;
|
|
}
|
|
|
|
get jsdom(){ return this._jsDOM; }
|
|
|
|
private _apply()
|
|
{
|
|
if ( this._applied )
|
|
{
|
|
return;
|
|
}
|
|
|
|
this._addHTMLNodeDefinition();
|
|
this._addDOMParser();
|
|
|
|
this._applied = true;
|
|
}
|
|
|
|
|
|
private _addHTMLNodeDefinition()
|
|
{
|
|
( global as any ).Node =
|
|
{
|
|
ELEMENT_NODE: 1,
|
|
ATTRIBUTE_NODE: 2,
|
|
TEXT_NODE:3,
|
|
CDATA_SECTION_NODE:4,
|
|
PROCESSING_INSTRUCTION_NODE:7,
|
|
COMMENT_NODE:8,
|
|
DOCUMENT_NODE:9,
|
|
DOCUMENT_TYPE_NODE:10,
|
|
DOCUMENT_FRAGMENT_NODE:11
|
|
}
|
|
}
|
|
|
|
private _addDOMParser()
|
|
{
|
|
DOMParserShim.implementation = ( html:string, type:string = "html" )=>
|
|
{
|
|
return new JSDOM( html, {contentType:type} ).window.document;
|
|
}
|
|
|
|
global.DOMParser = DOMParserShim;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
export class DOMParserShim
|
|
{
|
|
static implementation:( html:string, type:string )=>Document;
|
|
parseFromString( html:string, type:string ):Document
|
|
{
|
|
return DOMParserShim.implementation( html, type );
|
|
}
|
|
} |