This repository has been archived by the owner on Nov 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 484
feat(window): mocking window object #311
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's
GLOBAL
?There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it looks like
GLOBAL
androot
will be removednodejs/node#1838