mirror of https://github.com/vitalif/phantomjs
Reworking code related to Issue #800.
This addresses [Issue #842](http://code.google.com/p/phantomjs/issues/detail?id=842).1.8
parent
7c7d1f961c
commit
1fa9c04845
|
@ -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");
|
||||
};
|
||||
}
|
||||
|
|
|
@ -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 + ',';
|
||||
|
|
Loading…
Reference in New Issue