Fix lint warnings in URL polyfill

This commit is contained in:
Yash Srivastav 2016-09-12 19:09:25 +05:30
parent 03588ccbf7
commit 4e428c7675
No known key found for this signature in database
GPG Key ID: 67A09FFAC003E189

View File

@ -1719,8 +1719,6 @@ function loadJpegStream(id, imageUrl, objs) {
/* Any copyright is dedicated to the Public Domain. /* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */ * http://creativecommons.org/publicdomain/zero/1.0/ */
(function checkURLConstructor(scope) { (function checkURLConstructor(scope) {
/* jshint ignore:start */
// feature detect for URL constructor // feature detect for URL constructor
var hasWorkingUrl = false; var hasWorkingUrl = false;
try { try {
@ -1733,8 +1731,9 @@ function loadJpegStream(id, imageUrl, objs) {
} }
} catch(e) { } } catch(e) { }
if (hasWorkingUrl) if (hasWorkingUrl) {
return; return;
}
var relative = Object.create(null); var relative = Object.create(null);
relative['ftp'] = 21; relative['ftp'] = 21;
@ -1761,11 +1760,11 @@ function loadJpegStream(id, imageUrl, objs) {
} }
function IDNAToASCII(h) { function IDNAToASCII(h) {
if ('' == h) { if ('' === h) {
invalid.call(this) invalid.call(this);
} }
// XXX // XXX
return h.toLowerCase() return h.toLowerCase();
} }
function percentEscape(c) { function percentEscape(c) {
@ -1773,7 +1772,7 @@ function loadJpegStream(id, imageUrl, objs) {
if (unicode > 0x20 && if (unicode > 0x20 &&
unicode < 0x7F && unicode < 0x7F &&
// " # < > ? ` // " # < > ? `
[0x22, 0x23, 0x3C, 0x3E, 0x3F, 0x60].indexOf(unicode) == -1 [0x22, 0x23, 0x3C, 0x3E, 0x3F, 0x60].indexOf(unicode) === -1
) { ) {
return c; return c;
} }
@ -1788,20 +1787,19 @@ function loadJpegStream(id, imageUrl, objs) {
if (unicode > 0x20 && if (unicode > 0x20 &&
unicode < 0x7F && unicode < 0x7F &&
// " # < > ` (do not escape '?') // " # < > ` (do not escape '?')
[0x22, 0x23, 0x3C, 0x3E, 0x60].indexOf(unicode) == -1 [0x22, 0x23, 0x3C, 0x3E, 0x60].indexOf(unicode) === -1
) { ) {
return c; return c;
} }
return encodeURIComponent(c); return encodeURIComponent(c);
} }
var EOF = undefined, var EOF, ALPHA = /[a-zA-Z]/,
ALPHA = /[a-zA-Z]/,
ALPHANUMERIC = /[a-zA-Z0-9\+\-\.]/; ALPHANUMERIC = /[a-zA-Z0-9\+\-\.]/;
function parse(input, stateOverride, base) { function parse(input, stateOverride, base) {
function err(message) { function err(message) {
errors.push(message) errors.push(message);
} }
var state = stateOverride || 'scheme start', var state = stateOverride || 'scheme start',
@ -1811,7 +1809,8 @@ function loadJpegStream(id, imageUrl, objs) {
seenBracket = false, seenBracket = false,
errors = []; errors = [];
loop: while ((input[cursor - 1] != EOF || cursor == 0) && !this._isInvalid) { loop: while ((input[cursor - 1] !== EOF || cursor === 0) &&
!this._isInvalid) {
var c = input[cursor]; var c = input[cursor];
switch (state) { switch (state) {
case 'scheme start': case 'scheme start':
@ -1831,7 +1830,7 @@ function loadJpegStream(id, imageUrl, objs) {
case 'scheme': case 'scheme':
if (c && ALPHANUMERIC.test(c)) { if (c && ALPHANUMERIC.test(c)) {
buffer += c.toLowerCase(); // ASCII-safe buffer += c.toLowerCase(); // ASCII-safe
} else if (':' == c) { } else if (':' === c) {
this._scheme = buffer; this._scheme = buffer;
buffer = ''; buffer = '';
if (stateOverride) { if (stateOverride) {
@ -1840,9 +1839,10 @@ function loadJpegStream(id, imageUrl, objs) {
if (isRelativeScheme(this._scheme)) { if (isRelativeScheme(this._scheme)) {
this._isRelative = true; this._isRelative = true;
} }
if ('file' == this._scheme) { if ('file' === this._scheme) {
state = 'relative'; state = 'relative';
} else if (this._isRelative && base && base._scheme == this._scheme) { } else if (this._isRelative && base &&
base._scheme === this._scheme) {
state = 'relative or authority'; state = 'relative or authority';
} else if (this._isRelative) { } else if (this._isRelative) {
state = 'authority first slash'; state = 'authority first slash';
@ -1854,24 +1854,24 @@ function loadJpegStream(id, imageUrl, objs) {
cursor = 0; cursor = 0;
state = 'no scheme'; state = 'no scheme';
continue; continue;
} else if (EOF == c) { } else if (EOF === c) {
break loop; break loop;
} else { } else {
err('Code point not allowed in scheme: ' + c) err('Code point not allowed in scheme: ' + c);
break loop; break loop;
} }
break; break;
case 'scheme data': case 'scheme data':
if ('?' == c) { if ('?' === c) {
this._query = '?'; this._query = '?';
state = 'query'; state = 'query';
} else if ('#' == c) { } else if ('#' === c) {
this._fragment = '#'; this._fragment = '#';
state = 'fragment'; state = 'fragment';
} else { } else {
// XXX error handling // XXX error handling
if (EOF != c && '\t' != c && '\n' != c && '\r' != c) { if (EOF !== c && '\t' !== c && '\n' !== c && '\r' !== c) {
this._schemeData += percentEscape(c); this._schemeData += percentEscape(c);
} }
} }
@ -1888,20 +1888,21 @@ function loadJpegStream(id, imageUrl, objs) {
break; break;
case 'relative or authority': case 'relative or authority':
if ('/' == c && '/' == input[cursor+1]) { if ('/' === c && '/' === input[cursor+1]) {
state = 'authority ignore slashes'; state = 'authority ignore slashes';
} else { } else {
err('Expected /, got: ' + c); err('Expected /, got: ' + c);
state = 'relative'; state = 'relative';
continue continue;
} }
break; break;
case 'relative': case 'relative':
this._isRelative = true; this._isRelative = true;
if ('file' != this._scheme) if ('file' !== this._scheme) {
this._scheme = base._scheme; this._scheme = base._scheme;
if (EOF == c) { }
if (EOF === c) {
this._host = base._host; this._host = base._host;
this._port = base._port; this._port = base._port;
this._path = base._path.slice(); this._path = base._path.slice();
@ -1909,11 +1910,12 @@ function loadJpegStream(id, imageUrl, objs) {
this._username = base._username; this._username = base._username;
this._password = base._password; this._password = base._password;
break loop; break loop;
} else if ('/' == c || '\\' == c) { } else if ('/' === c || '\\' === c) {
if ('\\' == c) if ('\\' === c) {
err('\\ is an invalid code point.'); err('\\ is an invalid code point.');
}
state = 'relative slash'; state = 'relative slash';
} else if ('?' == c) { } else if ('?' === c) {
this._host = base._host; this._host = base._host;
this._port = base._port; this._port = base._port;
this._path = base._path.slice(); this._path = base._path.slice();
@ -1921,7 +1923,7 @@ function loadJpegStream(id, imageUrl, objs) {
this._username = base._username; this._username = base._username;
this._password = base._password; this._password = base._password;
state = 'query'; state = 'query';
} else if ('#' == c) { } else if ('#' === c) {
this._host = base._host; this._host = base._host;
this._port = base._port; this._port = base._port;
this._path = base._path.slice(); this._path = base._path.slice();
@ -1931,12 +1933,12 @@ function loadJpegStream(id, imageUrl, objs) {
this._password = base._password; this._password = base._password;
state = 'fragment'; state = 'fragment';
} else { } else {
var nextC = input[cursor+1] var nextC = input[cursor+1];
var nextNextC = input[cursor+2] var nextNextC = input[cursor+2];
if ( if ('file' !== this._scheme || !ALPHA.test(c) ||
'file' != this._scheme || !ALPHA.test(c) || (nextC !== ':' && nextC !== '|') ||
(nextC != ':' && nextC != '|') || (EOF !== nextNextC && '/' !== nextNextC && '\\' !== nextNextC &&
(EOF != nextNextC && '/' != nextNextC && '\\' != nextNextC && '?' != nextNextC && '#' != nextNextC)) { '?' !== nextNextC && '#' !== nextNextC)) {
this._host = base._host; this._host = base._host;
this._port = base._port; this._port = base._port;
this._username = base._username; this._username = base._username;
@ -1950,17 +1952,17 @@ function loadJpegStream(id, imageUrl, objs) {
break; break;
case 'relative slash': case 'relative slash':
if ('/' == c || '\\' == c) { if ('/' === c || '\\' === c) {
if ('\\' == c) { if ('\\' === c) {
err('\\ is an invalid code point.'); err('\\ is an invalid code point.');
} }
if ('file' == this._scheme) { if ('file' === this._scheme) {
state = 'file host'; state = 'file host';
} else { } else {
state = 'authority ignore slashes'; state = 'authority ignore slashes';
} }
} else { } else {
if ('file' != this._scheme) { if ('file' !== this._scheme) {
this._host = base._host; this._host = base._host;
this._port = base._port; this._port = base._port;
this._username = base._username; this._username = base._username;
@ -1972,10 +1974,10 @@ function loadJpegStream(id, imageUrl, objs) {
break; break;
case 'authority first slash': case 'authority first slash':
if ('/' == c) { if ('/' === c) {
state = 'authority second slash'; state = 'authority second slash';
} else { } else {
err("Expected '/', got: " + c); err('Expected \'/\', got: ' + c);
state = 'authority ignore slashes'; state = 'authority ignore slashes';
continue; continue;
} }
@ -1983,14 +1985,14 @@ function loadJpegStream(id, imageUrl, objs) {
case 'authority second slash': case 'authority second slash':
state = 'authority ignore slashes'; state = 'authority ignore slashes';
if ('/' != c) { if ('/' !== c) {
err("Expected '/', got: " + c); err('Expected \'/\', got: ' + c);
continue; continue;
} }
break; break;
case 'authority ignore slashes': case 'authority ignore slashes':
if ('/' != c && '\\' != c) { if ('/' !== c && '\\' !== c) {
state = 'authority'; state = 'authority';
continue; continue;
} else { } else {
@ -1999,7 +2001,7 @@ function loadJpegStream(id, imageUrl, objs) {
break; break;
case 'authority': case 'authority':
if ('@' == c) { if ('@' === c) {
if (seenAt) { if (seenAt) {
err('@ already seen.'); err('@ already seen.');
buffer += '%40'; buffer += '%40';
@ -2007,20 +2009,25 @@ function loadJpegStream(id, imageUrl, objs) {
seenAt = true; seenAt = true;
for (var i = 0; i < buffer.length; i++) { for (var i = 0; i < buffer.length; i++) {
var cp = buffer[i]; var cp = buffer[i];
if ('\t' == cp || '\n' == cp || '\r' == cp) { if ('\t' === cp || '\n' === cp || '\r' === cp) {
err('Invalid whitespace in authority.'); err('Invalid whitespace in authority.');
continue; continue;
} }
// XXX check URL code points // XXX check URL code points
if (':' == cp && null === this._password) { if (':' === cp && null === this._password) {
this._password = ''; this._password = '';
continue; continue;
} }
var tempC = percentEscape(cp); var tempC = percentEscape(cp);
(null !== this._password) ? this._password += tempC : this._username += tempC; if (null !== this._password) {
this._password += tempC;
} else {
this._username += tempC;
}
} }
buffer = ''; buffer = '';
} else if (EOF == c || '/' == c || '\\' == c || '?' == c || '#' == c) { } else if (EOF === c || '/' === c || '\\' === c ||
'?' === c || '#' === c) {
cursor -= buffer.length; cursor -= buffer.length;
buffer = ''; buffer = '';
state = 'host'; state = 'host';
@ -2031,10 +2038,11 @@ function loadJpegStream(id, imageUrl, objs) {
break; break;
case 'file host': case 'file host':
if (EOF == c || '/' == c || '\\' == c || '?' == c || '#' == c) { if (EOF === c || '/' === c || '\\' === c || '?' === c || '#' === c) {
if (buffer.length == 2 && ALPHA.test(buffer[0]) && (buffer[1] == ':' || buffer[1] == '|')) { if (buffer.length === 2 && ALPHA.test(buffer[0]) &&
(buffer[1] === ':' || buffer[1] === '|')) {
state = 'relative path'; state = 'relative path';
} else if (buffer.length == 0) { } else if (buffer.length === 0) {
state = 'relative path start'; state = 'relative path start';
} else { } else {
this._host = IDNAToASCII.call(this, buffer); this._host = IDNAToASCII.call(this, buffer);
@ -2042,7 +2050,7 @@ function loadJpegStream(id, imageUrl, objs) {
state = 'relative path start'; state = 'relative path start';
} }
continue; continue;
} else if ('\t' == c || '\n' == c || '\r' == c) { } else if ('\t' === c || '\n' === c || '\r' === c) {
err('Invalid whitespace in file host.'); err('Invalid whitespace in file host.');
} else { } else {
buffer += c; buffer += c;
@ -2051,15 +2059,16 @@ function loadJpegStream(id, imageUrl, objs) {
case 'host': case 'host':
case 'hostname': case 'hostname':
if (':' == c && !seenBracket) { if (':' === c && !seenBracket) {
// XXX host parsing // XXX host parsing
this._host = IDNAToASCII.call(this, buffer); this._host = IDNAToASCII.call(this, buffer);
buffer = ''; buffer = '';
state = 'port'; state = 'port';
if ('hostname' == stateOverride) { if ('hostname' === stateOverride) {
break loop; break loop;
} }
} else if (EOF == c || '/' == c || '\\' == c || '?' == c || '#' == c) { } else if (EOF === c || '/' === c ||
'\\' === c || '?' === c || '#' === c) {
this._host = IDNAToASCII.call(this, buffer); this._host = IDNAToASCII.call(this, buffer);
buffer = ''; buffer = '';
state = 'relative path start'; state = 'relative path start';
@ -2067,10 +2076,10 @@ function loadJpegStream(id, imageUrl, objs) {
break loop; break loop;
} }
continue; continue;
} else if ('\t' != c && '\n' != c && '\r' != c) { } else if ('\t' !== c && '\n' !== c && '\r' !== c) {
if ('[' == c) { if ('[' === c) {
seenBracket = true; seenBracket = true;
} else if (']' == c) { } else if (']' === c) {
seenBracket = false; seenBracket = false;
} }
buffer += c; buffer += c;
@ -2082,10 +2091,11 @@ function loadJpegStream(id, imageUrl, objs) {
case 'port': case 'port':
if (/[0-9]/.test(c)) { if (/[0-9]/.test(c)) {
buffer += c; buffer += c;
} else if (EOF == c || '/' == c || '\\' == c || '?' == c || '#' == c || stateOverride) { } else if (EOF === c || '/' === c || '\\' === c ||
if ('' != buffer) { '?' === c || '#' === c || stateOverride) {
if ('' !== buffer) {
var temp = parseInt(buffer, 10); var temp = parseInt(buffer, 10);
if (temp != relative[this._scheme]) { if (temp !== relative[this._scheme]) {
this._port = temp + ''; this._port = temp + '';
} }
buffer = ''; buffer = '';
@ -2095,7 +2105,7 @@ function loadJpegStream(id, imageUrl, objs) {
} }
state = 'relative path start'; state = 'relative path start';
continue; continue;
} else if ('\t' == c || '\n' == c || '\r' == c) { } else if ('\t' === c || '\n' === c || '\r' === c) {
err('Invalid code point in port: ' + c); err('Invalid code point in port: ' + c);
} else { } else {
invalid.call(this); invalid.call(this);
@ -2103,60 +2113,64 @@ function loadJpegStream(id, imageUrl, objs) {
break; break;
case 'relative path start': case 'relative path start':
if ('\\' == c) if ('\\' === c) {
err("'\\' not allowed in path."); err('\'\\\' not allowed in path.');
}
state = 'relative path'; state = 'relative path';
if ('/' != c && '\\' != c) { if ('/' !== c && '\\' !== c) {
continue; continue;
} }
break; break;
case 'relative path': case 'relative path':
if (EOF == c || '/' == c || '\\' == c || (!stateOverride && ('?' == c || '#' == c))) { if (EOF === c || '/' === c || '\\' === c ||
if ('\\' == c) { (!stateOverride && ('?' === c || '#' === c))) {
if ('\\' === c) {
err('\\ not allowed in relative path.'); err('\\ not allowed in relative path.');
} }
var tmp; var tmp;
if (tmp = relativePathDotMapping[buffer.toLowerCase()]) { if (tmp = relativePathDotMapping[buffer.toLowerCase()]) {
buffer = tmp; buffer = tmp;
} }
if ('..' == buffer) { if ('..' === buffer) {
this._path.pop(); this._path.pop();
if ('/' != c && '\\' != c) { if ('/' !== c && '\\' !== c) {
this._path.push(''); this._path.push('');
} }
} else if ('.' == buffer && '/' != c && '\\' != c) { } else if ('.' === buffer && '/' !== c && '\\' !== c) {
this._path.push(''); this._path.push('');
} else if ('.' != buffer) { } else if ('.' !== buffer) {
if ('file' == this._scheme && this._path.length == 0 && buffer.length == 2 && ALPHA.test(buffer[0]) && buffer[1] == '|') { if ('file' === this._scheme && this._path.length === 0 &&
buffer.length === 2 && ALPHA.test(buffer[0]) &&
buffer[1] === '|') {
buffer = buffer[0] + ':'; buffer = buffer[0] + ':';
} }
this._path.push(buffer); this._path.push(buffer);
} }
buffer = ''; buffer = '';
if ('?' == c) { if ('?' === c) {
this._query = '?'; this._query = '?';
state = 'query'; state = 'query';
} else if ('#' == c) { } else if ('#' === c) {
this._fragment = '#'; this._fragment = '#';
state = 'fragment'; state = 'fragment';
} }
} else if ('\t' != c && '\n' != c && '\r' != c) { } else if ('\t' !== c && '\n' !== c && '\r' !== c) {
buffer += percentEscape(c); buffer += percentEscape(c);
} }
break; break;
case 'query': case 'query':
if (!stateOverride && '#' == c) { if (!stateOverride && '#' === c) {
this._fragment = '#'; this._fragment = '#';
state = 'fragment'; state = 'fragment';
} else if (EOF != c && '\t' != c && '\n' != c && '\r' != c) { } else if (EOF !== c && '\t' !== c && '\n' !== c && '\r' !== c) {
this._query += percentEscapeQuery(c); this._query += percentEscapeQuery(c);
} }
break; break;
case 'fragment': case 'fragment':
if (EOF != c && '\t' != c && '\n' != c && '\r' != c) { if (EOF !== c && '\t' !== c && '\n' !== c && '\r' !== c) {
this._fragment += c; this._fragment += c;
} }
break; break;
@ -2182,9 +2196,10 @@ function loadJpegStream(id, imageUrl, objs) {
// Does not process domain names or IP addresses. // Does not process domain names or IP addresses.
// Does not handle encoding for the query parameter. // Does not handle encoding for the query parameter.
function jURL(url, base /* , encoding */) { function JURL(url, base /* , encoding */) {
if (base !== undefined && !(base instanceof jURL)) if (base !== undefined && !(base instanceof JURL)) {
base = new jURL(String(base)); base = new JURL(String(base));
}
this._url = url; this._url = url;
clear.call(this); clear.call(this);
@ -2195,18 +2210,18 @@ function loadJpegStream(id, imageUrl, objs) {
parse.call(this, input, null, base); parse.call(this, input, null, base);
} }
jURL.prototype = { JURL.prototype = {
toString: function() { toString: function() {
return this.href; return this.href;
}, },
get href() { get href() {
if (this._isInvalid) if (this._isInvalid) {
return this._url; return this._url;
}
var authority = ''; var authority = '';
if ('' != this._username || null != this._password) { if ('' !== this._username || null !== this._password) {
authority = this._username + authority = this._username +
(null != this._password ? ':' + this._password : '') + '@'; (null !== this._password ? ':' + this._password : '') + '@';
} }
return this.protocol + return this.protocol +
@ -2222,8 +2237,9 @@ function loadJpegStream(id, imageUrl, objs) {
return this._scheme + ':'; return this._scheme + ':';
}, },
set protocol(protocol) { set protocol(protocol) {
if (this._isInvalid) if (this._isInvalid) {
return; return;
}
parse.call(this, protocol + ':', 'scheme start'); parse.call(this, protocol + ':', 'scheme start');
}, },
@ -2232,8 +2248,9 @@ function loadJpegStream(id, imageUrl, objs) {
this._host + ':' + this._port : this._host; this._host + ':' + this._port : this._host;
}, },
set host(host) { set host(host) {
if (this._isInvalid || !this._isRelative) if (this._isInvalid || !this._isRelative) {
return; return;
}
parse.call(this, host, 'host'); parse.call(this, host, 'host');
}, },
@ -2241,8 +2258,9 @@ function loadJpegStream(id, imageUrl, objs) {
return this._host; return this._host;
}, },
set hostname(hostname) { set hostname(hostname) {
if (this._isInvalid || !this._isRelative) if (this._isInvalid || !this._isRelative) {
return; return;
}
parse.call(this, hostname, 'hostname'); parse.call(this, hostname, 'hostname');
}, },
@ -2250,8 +2268,9 @@ function loadJpegStream(id, imageUrl, objs) {
return this._port; return this._port;
}, },
set port(port) { set port(port) {
if (this._isInvalid || !this._isRelative) if (this._isInvalid || !this._isRelative) {
return; return;
}
parse.call(this, port, 'port'); parse.call(this, port, 'port');
}, },
@ -2260,35 +2279,40 @@ function loadJpegStream(id, imageUrl, objs) {
'/' + this._path.join('/') : this._schemeData; '/' + this._path.join('/') : this._schemeData;
}, },
set pathname(pathname) { set pathname(pathname) {
if (this._isInvalid || !this._isRelative) if (this._isInvalid || !this._isRelative) {
return; return;
}
this._path = []; this._path = [];
parse.call(this, pathname, 'relative path start'); parse.call(this, pathname, 'relative path start');
}, },
get search() { get search() {
return this._isInvalid || !this._query || '?' == this._query ? return this._isInvalid || !this._query || '?' === this._query ?
'' : this._query; '' : this._query;
}, },
set search(search) { set search(search) {
if (this._isInvalid || !this._isRelative) if (this._isInvalid || !this._isRelative) {
return; return;
}
this._query = '?'; this._query = '?';
if ('?' == search[0]) if ('?' === search[0]) {
search = search.slice(1); search = search.slice(1);
}
parse.call(this, search, 'query'); parse.call(this, search, 'query');
}, },
get hash() { get hash() {
return this._isInvalid || !this._fragment || '#' == this._fragment ? return this._isInvalid || !this._fragment || '#' === this._fragment ?
'' : this._fragment; '' : this._fragment;
}, },
set hash(hash) { set hash(hash) {
if (this._isInvalid) if (this._isInvalid) {
return; return;
}
this._fragment = '#'; this._fragment = '#';
if ('#' == hash[0]) if ('#' === hash[0]) {
hash = hash.slice(1); hash = hash.slice(1);
}
parse.call(this, hash, 'fragment'); parse.call(this, hash, 'fragment');
}, },
@ -2320,18 +2344,17 @@ function loadJpegStream(id, imageUrl, objs) {
// Copy over the static methods // Copy over the static methods
var OriginalURL = scope.URL; var OriginalURL = scope.URL;
if (OriginalURL) { if (OriginalURL) {
jURL.createObjectURL = function(blob) { JURL.createObjectURL = function(blob) {
// IE extension allows a second optional options argument. // IE extension allows a second optional options argument.
// http://msdn.microsoft.com/en-us/library/ie/hh772302(v=vs.85).aspx // http://msdn.microsoft.com/en-us/library/ie/hh772302(v=vs.85).aspx
return OriginalURL.createObjectURL.apply(OriginalURL, arguments); return OriginalURL.createObjectURL.apply(OriginalURL, arguments);
}; };
jURL.revokeObjectURL = function(url) { JURL.revokeObjectURL = function(url) {
OriginalURL.revokeObjectURL(url); OriginalURL.revokeObjectURL(url);
}; };
} }
scope.URL = jURL; scope.URL = JURL;
/* jshint ignore:end */
})(globalScope); })(globalScope);
//#endif //#endif