2017-08-08 05:17:40 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
const domino = require('domino');
|
|
|
|
|
2019-08-22 22:16:25 -04:00
|
|
|
import {ɵBrowserDomAdapter as BrowserDomAdapter} from '@angular/platform-browser';
|
|
|
|
import {ɵsetRootDomAdapter as setRootDomAdapter} from '@angular/common';
|
2017-08-08 05:17:40 -04:00
|
|
|
|
|
|
|
function _notImplemented(methodName: string) {
|
|
|
|
return new Error('This method is not implemented in DominoAdapter: ' + methodName);
|
|
|
|
}
|
|
|
|
|
2018-05-24 19:04:04 -04:00
|
|
|
function setDomTypes() {
|
|
|
|
// Make all Domino types available as types in the global env.
|
|
|
|
Object.assign(global, domino.impl);
|
|
|
|
(global as any)['KeyboardEvent'] = domino.impl.Event;
|
|
|
|
}
|
|
|
|
|
2017-08-08 05:17:40 -04:00
|
|
|
/**
|
|
|
|
* Parses a document string to a Document object.
|
|
|
|
*/
|
|
|
|
export function parseDocument(html: string, url = '/') {
|
|
|
|
let window = domino.createWindow(html, url);
|
|
|
|
let doc = window.document;
|
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serializes a document to string.
|
|
|
|
*/
|
|
|
|
export function serializeDocument(doc: Document): string {
|
|
|
|
return (doc as any).serialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DOM Adapter for the server platform based on https://github.com/fgnass/domino.
|
|
|
|
*/
|
|
|
|
export class DominoAdapter extends BrowserDomAdapter {
|
2018-05-24 19:04:04 -04:00
|
|
|
static makeCurrent() {
|
|
|
|
setDomTypes();
|
|
|
|
setRootDomAdapter(new DominoAdapter());
|
|
|
|
}
|
2017-08-08 05:17:40 -04:00
|
|
|
|
|
|
|
private static defaultDoc: Document;
|
|
|
|
|
2017-09-22 13:51:03 -04:00
|
|
|
log(error: string) {
|
|
|
|
// tslint:disable-next-line:no-console
|
|
|
|
console.log(error);
|
|
|
|
}
|
2017-08-08 05:17:40 -04:00
|
|
|
|
|
|
|
logGroup(error: string) { console.error(error); }
|
|
|
|
|
|
|
|
logGroupEnd() {}
|
|
|
|
|
|
|
|
supportsDOMEvents(): boolean { return false; }
|
|
|
|
|
|
|
|
createHtmlDocument(): HTMLDocument {
|
|
|
|
return parseDocument('<html><head><title>fakeTitle</title></head><body></body></html>');
|
|
|
|
}
|
|
|
|
|
|
|
|
getDefaultDocument(): Document {
|
|
|
|
if (!DominoAdapter.defaultDoc) {
|
|
|
|
DominoAdapter.defaultDoc = domino.createDocument();
|
|
|
|
}
|
|
|
|
return DominoAdapter.defaultDoc;
|
|
|
|
}
|
|
|
|
|
|
|
|
isElementNode(node: any): boolean {
|
|
|
|
return node ? node.nodeType === DominoAdapter.defaultDoc.ELEMENT_NODE : false;
|
|
|
|
}
|
2019-08-22 21:26:01 -04:00
|
|
|
isShadowRoot(node: any): boolean { return node.shadowRoot == node; }
|
2017-08-08 05:17:40 -04:00
|
|
|
|
|
|
|
getProperty(el: Element, name: string): any {
|
|
|
|
if (name === 'href') {
|
2019-08-27 19:39:33 -04:00
|
|
|
// Domino tries to resolve href-s which we do not want. Just return the
|
2018-03-10 12:14:58 -05:00
|
|
|
// attribute value.
|
2017-08-08 05:17:40 -04:00
|
|
|
return this.getAttribute(el, 'href');
|
2017-09-09 18:12:13 -04:00
|
|
|
} else if (name === 'innerText') {
|
|
|
|
// Domino does not support innerText. Just map it to textContent.
|
|
|
|
return el.textContent;
|
2017-08-08 05:17:40 -04:00
|
|
|
}
|
|
|
|
return (<any>el)[name];
|
|
|
|
}
|
|
|
|
|
|
|
|
getGlobalEventTarget(doc: Document, target: string): EventTarget|null {
|
|
|
|
if (target === 'window') {
|
|
|
|
return doc.defaultView;
|
|
|
|
}
|
|
|
|
if (target === 'document') {
|
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
if (target === 'body') {
|
|
|
|
return doc.body;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
getBaseHref(doc: Document): string {
|
2019-08-30 00:24:33 -04:00
|
|
|
const base = doc.documentElement !.querySelector('base');
|
2017-08-08 05:17:40 -04:00
|
|
|
let href = '';
|
|
|
|
if (base) {
|
2019-08-22 21:26:01 -04:00
|
|
|
href = base.getAttribute('href') !;
|
2017-08-08 05:17:40 -04:00
|
|
|
}
|
|
|
|
// TODO(alxhub): Need relative path logic from BrowserDomAdapter here?
|
|
|
|
return href;
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatchEvent(el: Node, evt: any) {
|
|
|
|
el.dispatchEvent(evt);
|
|
|
|
|
|
|
|
// Dispatch the event to the window also.
|
|
|
|
const doc = el.ownerDocument || el;
|
|
|
|
const win = (doc as any).defaultView;
|
|
|
|
if (win) {
|
|
|
|
win.dispatchEvent(evt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getHistory(): History { throw _notImplemented('getHistory'); }
|
|
|
|
getLocation(): Location { throw _notImplemented('getLocation'); }
|
|
|
|
getUserAgent(): string { return 'Fake user agent'; }
|
|
|
|
|
|
|
|
performanceNow(): number { return Date.now(); }
|
|
|
|
|
|
|
|
supportsCookies(): boolean { return false; }
|
|
|
|
getCookie(name: string): string { throw _notImplemented('getCookie'); }
|
2018-02-23 14:33:19 -05:00
|
|
|
}
|