Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

More Douglas Crockford remedials.

  • Loading branch information...
commit d5eb657ecc4a16d37097df8c971a1f41396877fb 1 parent e31528a
@detro detro authored
Showing with 56 additions and 7 deletions.
  1. +51 −3 src/bootstrap.js
  2. +5 −4 src/modules/webpage.js
View
54 src/bootstrap.js
@@ -66,8 +66,8 @@ phantom.defaultErrorHandler = function(message, stack) {
stack.forEach(function(item) {
var message = item.file + ":" + item.line;
- if (item.function)
- message += " in " + item.function;
+ if (item["function"])
+ message += " in " + item["function"];
console.log(" " + message);
});
};
@@ -292,7 +292,7 @@ phantom.onError = phantom.defaultErrorHandler;
// Legacy way to use WebPage
window.WebPage = require('webpage').create;
-// Remedy to fauly "typeof": "typeOf" by Douglas Crockford
+// 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) {
@@ -310,3 +310,51 @@ window.detectType = function (value) {
}
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
9 src/modules/webpage.js
@@ -264,13 +264,14 @@ function decorateNewPage(opts, page) {
argType = detectType(arg);
switch (argType) {
- case "object": case "array": //< for type "object" and "array"
+ case "object": //< for type "object"
+ case "array": //< for type "array"
str += "JSON.parse(" + JSON.stringify(JSON.stringify(arg)) + "),"
break;
- case "string": //< for type "string"
- str += '"' + arg +'",';
+ case "string": //< for type "string"
+ str += arg.quote() + ',';
break;
- default: // for types: "null", "number", "function", "regexp", "undefined"
+ default: // for types: "null", "number", "function", "regexp", "undefined"
str += arg + ',';
break;
}
Please sign in to comment.
Something went wrong with that request. Please try again.