From 2f212c6e4d5223111ff0ae46f743b408f9c28882 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sat, 10 Aug 2013 06:46:42 -0500 Subject: [PATCH] improve document/element/context handling. --- lib/term.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/term.js b/lib/term.js index ee522a5e..3a211942 100644 --- a/lib/term.js +++ b/lib/term.js @@ -138,7 +138,7 @@ function Terminal(options) { EventEmitter.call(this); - if (arguments.length > 1) { + if (typeof options === 'number') { options = { cols: arguments[0], rows: arguments[1], @@ -148,9 +148,10 @@ function Terminal(options) { this._options = options || {}; - this.context = options.context || window; - this.document = options.document || document; - this.parent = options.body || options.parent || document.body; + // this.context = options.context || window; + // this.document = options.document || document; + this.parent = options.body || options.parent + || (document ? document.body : null); this.cols = options.cols || Terminal.geometry[0]; this.rows = options.rows || Terminal.geometry[1]; @@ -450,7 +451,14 @@ Terminal.prototype.open = function(parent) { , i = 0 , div; - this.parent = parent || this.parent || document.body; + this.parent = parent || this.parent; + + if (!this.parent) { + throw new Error('Terminal requires a parent element.'); + } + + this.context = this.parent.ownerDocument.defaultView; + this.document = this.parent.ownerDocument; this.element = this.document.createElement('div'); this.element.className = 'terminal';