Skip to content
Browse files

Reworking code related to Issue #800.

  • Loading branch information...
1 parent 7c7d1f9 commit 1fa9c048457bf4b219eaaeb3c1e22a72185513c8 @detro detro committed Oct 25, 2012
Showing with 61 additions and 72 deletions.
  1. +2 −71 src/bootstrap.js
  2. +59 −1 src/modules/webpage.js
View
73 src/bootstrap.js
@@ -106,10 +106,10 @@ phantom.onError = phantom.defaultErrorHandler;
module.exports = JSON.parse(fs.read(filename));
}
};
-
+
function loadFs() {
var file, code, module, filename = ':/modules/fs.js';
-
+
module = new Module(filename);
cache[filename] = module;
module.exports = nativeExports.fs;
@@ -291,72 +291,3 @@ phantom.onError = phantom.defaultErrorHandler;
// Legacy way to use WebPage
window.WebPage = require('webpage').create;
-
-// Remedy to fauly "typeof" by Douglas Crockford.
-// NOTE: Renamed to "detectType" and added support for RegExp
-// @see http://javascript.crockford.com/remedial.html
-window.detectType = function (value) {
- var s = typeof value;
- if (s === 'object') {
- if (value) {
- if (value instanceof Array) {
- s = 'array';
- } else if (value instanceof RegExp) {
- s = 'regexp';
- } else if (value instanceof Date) {
- s = 'date';
- }
- } else {
- s = 'null';
- }
- }
- return s;
-};
-
-// Remedy to absent "quote" method bu Douglas Crockford.
-// @see http://javascript.crockford.com/remedial.html
-if (!String.prototype.quote) {
- String.prototype.quote = function () {
- var c, i, l = this.length, o = '"';
- for (i = 0; i < l; i += 1) {
- c = this.charAt(i);
- if (c >= ' ') {
- if (c === '\\' || c === '"') {
- o += '\\';
- }
- o += c;
- } else {
- switch (c) {
- case '\b':
- o += '\\b';
- break;
- case '\f':
- o += '\\f';
- break;
- case '\n':
- o += '\\n';
- break;
- case '\r':
- o += '\\r';
- break;
- case '\t':
- o += '\\t';
- break;
- default:
- c = c.charCodeAt();
- o += '\\u00' + Math.floor(c / 16).toString(16) +
- (c % 16).toString(16);
- }
- }
- }
- return o + '"';
- };
-}
-
-// Remedy to absent "trim" method bu Douglas Crockford.
-// @see http://javascript.crockford.com/remedial.html
-if (!String.prototype.trim) {
- String.prototype.trim = function () {
- return this.replace(/^\s*(\S*(?:\s+\S+)*)\s*$/, "$1");
- };
-}
View
60 src/modules/webpage.js
@@ -140,6 +140,64 @@ function definePageCallbackSetter(page, handlers, handlerName, callbackConstruct
});
}
+// Inspired by Douglas Crockford's remedies: proper String quoting.
+// @see http://javascript.crockford.com/remedial.html
+function quoteString(str) {
+ var c, i, l = str.length, o = '"';
+ for (i = 0; i < l; i += 1) {
+ c = str.charAt(i);
+ if (c >= ' ') {
+ if (c === '\\' || c === '"') {
+ o += '\\';
+ }
+ o += c;
+ } else {
+ switch (c) {
+ case '\b':
+ o += '\\b';
+ break;
+ case '\f':
+ o += '\\f';
+ break;
+ case '\n':
+ o += '\\n';
+ break;
+ case '\r':
+ o += '\\r';
+ break;
+ case '\t':
+ o += '\\t';
+ break;
+ default:
+ c = c.charCodeAt();
+ o += '\\u00' + Math.floor(c / 16).toString(16) +
+ (c % 16).toString(16);
+ }
+ }
+ }
+ return o + '"';
+}
+
+// Inspired by Douglas Crockford's remedies: a better Type Detection.
+// @see http://javascript.crockford.com/remedial.html
+function detectType(value) {
+ var s = typeof value;
+ if (s === 'object') {
+ if (value) {
+ if (value instanceof Array) {
+ s = 'array';
+ } else if (value instanceof RegExp) {
+ s = 'regexp';
+ } else if (value instanceof Date) {
+ s = 'date';
+ }
+ } else {
+ s = 'null';
+ }
+ }
+ return s;
+}
+
function decorateNewPage(opts, page) {
var handlers = {};
@@ -286,7 +344,7 @@ function decorateNewPage(opts, page) {
str += "JSON.parse(" + JSON.stringify(JSON.stringify(arg)) + "),"
break;
case "string": //< for type "string"
- str += arg.quote() + ',';
+ str += quoteString(arg) + ',';
break;
default: // for types: "null", "number", "function", "regexp", "undefined"
str += arg + ',';

0 comments on commit 1fa9c04

Please sign in to comment.
Something went wrong with that request. Please try again.