Skip to content
This repository has been archived by the owner on Nov 22, 2024. It is now read-only.

feat(window): mocking window object #311

Merged
merged 1 commit into from
Mar 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions custom_typings/server.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ declare module "xhr2" {
nodejsSet(url: any): any;
}
}

declare module NodeJS {
interface Global {
window: any | Window;
}
}
2 changes: 2 additions & 0 deletions modules/universal/mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var window = require('./dist/server/src/node/mock/window');
global.window = window;
182 changes: 182 additions & 0 deletions modules/universal/server/src/node/mock/window.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
import {DOM} from 'angular2/src/platform/dom/dom_adapter';
import {provide} from 'angular2/core';

// the overloaded "window" must extend node's "global"
// see: https://github.com/angular/angular/blob/master/modules/angular2/src/facade/lang.ts#L38
var win = Object.create(global);

/**
* Warn the developer about direct access to Window props
* @param {String} prop The property being accessed
*/
function beDefensive(prop){
return (<any>win).__defineGetter__(prop, () => {
console.warn(`[WARNING] Property/method "${prop}" should not be called directly. Use DomAdapter instead.`);

// TODO(wassim): find a generic solution to proxify DomAdapter
// let doc = DOM.defaultDoc();
// return DOM.querySelector(doc, ...args);
return prop;
});
}

let unforgeableAttributes = [
"window",
"document",
"location",
"top"
].map(beDefensive);

let replaceableAttributes = [
"self",
"locationbar",
"menubar",
"personalbar",
"scrollbars",
"statusbar",
"toolbar",
"frames",
"parent",
"external",
"length",

// CSSOM-View
"screen",
"scrollX",
"scrollY",
"pageXOffset",
"pageYOffset",
"innerWidth",
"innerHeight",
"screenX",
"screenY",
"outerWidth",
"outerHeight",
"devicePixelRatio",
].map(beDefensive);

let methods = [
"close",
"stop",
"focus",
"blur",
"open",
"alert",
"confirm",
"prompt",
"print",
"postMessage",

// WindowBase64
"btoa",
"atob",

// WindowTimers
"setTimeout",
"clearTimeout",
"setInterval",
"clearInterval",

// HTML Editing APIs
"getSelection",

// CSSOM
"getComputedStyle",

// CSSOM-View
"matchMedia",
"scroll",
"scrollTo",
"scrollBy"
].map(beDefensive);

let readonlyAttributes = [
"history",
"frameElement",
"navigator",
"applicationCache",

// WindowSessionStorage
"sessionStorage",

// WindowLocalStorage
"localStorage",
].map(beDefensive);

let writableAttributes = [
"name",
"status",
"opener",
"onabort",
"onafterprint",
"onbeforeprint",
"onbeforeunload",
"onblur",
"oncancel",
"oncanplay",
"oncanplaythrough",
"onchange",
"onclick",
"onclose",
"oncontextmenu",
"oncuechange",
"ondblclick",
"ondrag",
"ondragend",
"ondragenter",
"ondragleave",
"ondragover",
"ondragstart",
"ondrop",
"ondurationchange",
"onemptied",
"onended",
"onerror",
"onfocus",
"onhashchange",
"oninput",
"oninvalid",
"onkeydown",
"onkeypress",
"onkeyup",
"onload",
"onloadeddata",
"onloadedmetadata",
"onloadstart",
"onmessage",
"onmousedown",
"onmousemove",
"onmouseout",
"onmouseover",
"onmouseup",
"onmousewheel",
"onoffline",
"ononline",
"onpause",
"onplay",
"onplaying",
"onpagehide",
"onpageshow",
"onpopstate",
"onprogress",
"onratechange",
"onreset",
"onresize",
"onscroll",
"onseeked",
"onseeking",
"onselect",
"onshow",
"onstalled",
"onstorage",
"onsubmit",
"onsuspend",
"ontimeupdate",
"onunload",
"onvolumechange",
"onwaiting"
].map(beDefensive);

export var window = win;
global.window = win;
GLOBAL.window = win;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's GLOBAL?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in node, GLOBAL and global are equivalent
image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I set those two justs in case :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it looks like GLOBAL and root will be removed
nodejs/node#1838