Skip to content

Instantly share code, notes, and snippets.

@Mouvedia
Last active January 30, 2016 21:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mouvedia/4181f8becc08a5c63299 to your computer and use it in GitHub Desktop.
Save Mouvedia/4181f8becc08a5c63299 to your computer and use it in GitHub Desktop.
String
//https://github.com/Mouvedia/isString
String.parseAsClass = function(className, HTML5) {
if (!String.isString(className))
throw new TypeError('The passed argument must be a string.');
if (className === '')
throw new SyntaxError('The string provided must not be empty.');
if (!HTML5) {
className = className.replace(/^[ \t\n\f\r\u200b]+|[ \t\n\f\r\u200b]+$/g, '');
if (/[ \t\n\f\r\0\u200b]/.test(className)) // INVALID_CHARACTER_ERR 5
throw new Error(className.inspect() + ' contains an invalid character at position ' + className.indexOf(className.match(/[ \t\n\f\r\0\u200b]/)[0]));
} else if (/[ \t\n\f\r\0]/.test(className)) // INVALID_CHARACTER_ERR 5
throw new Error(className.inspect() + ' contains an invalid character at position ' + className.indexOf(className.match(/[ \t\n\f\r\0]/)[0]));
};
String.parseAsId = function(id, type) {
var startChar, NCName;
if (!String.isString(id))
throw new TypeError('The passed argument must be a string.');
if (id === '')
throw new SyntaxError('The string provided must not be empty.');
switch (type) {
case 'HTML5':
if (/[ \t\n\f\r\0]/.test(id)) // INVALID_CHARACTER_ERR 5
throw new Error(id.inspect() + ' contains an invalid character at position ' + id.indexOf(id.match(/[ \t\n\f\r\0]/)[0]));
break;
case 'XML1':
startChar = 'a-z_A-Z\xC0-\xD6\xD8-\xF6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD';
NCName = new RegExp('^[' + startChar + '][-' + startChar + '.\\d\xB7\u0300-\u036F\u203F-\u2040]*$');
if (!/^[\u0020-\uD7FF\n\t\r\uE000-\uFFFD]+$/.test(id)) // INVALID_CHARACTER_ERR 5
throw new Error(id.inspect() + ' contains an invalid character at position ' + id.indexOf(id.match(/[^\u0020-\uD7FF\n\t\r\uE000-\uFFFD]/)[0]));
if (!NCName.test(id))
throw new SyntaxError(id.inspect() + ' must be a valid XML non-colonized name.');
if (/^xml/i.test(id) && console && console.warn)
console.warn('Names beginning with "' + id.substring(0, 3) + '" are reserved for XML standardization.');
break;
case 'HTML4':
default:
id = id.replace(/^[ \t\n\f\r\u200b]+|[ \t\n\f\r\u200b]+$/g, '');
if (!/^[a-zA-Z][\w:.-]*$/.test(id))
throw new SyntaxError(id.inspect() + ' must be a valid ID token.');
}
};
//http://stackoverflow.com/questions/1077084/what-characters-are-allowed-in-dom-ids
//http://regexadvice.com/blogs/mash/archive/2009/02/21/Looking-again-at-the-Lookahead-bug.aspx
//http://www.w3.org/TR/charmod/#C078
// MIME type
// http://lists.w3.org/Archives/Public/xml-dist-app/2003Jul/0064.html
String.prototype.has = function(str, insensitive) {
var proto = String.prototype
, method = !insensitive ? 'valueOf' : proto.toLocaleLowerCase ? 'toLocaleLowerCase' : 'toLowerCase'
, ES6 = proto.includes || proto.contains || false;
return (ES6 || proto.indexOf).call(this[method](), String(str)[method]()) > (ES6 ? 0 : -1);
};
String.prototype.insert = function(index, str) {
index = index < 0 ? this.length + ~~index : ~~index;
return this.substring(0, index) + str + this.substring(index, this.length);
};
String.prototype.inspect = function() {
if (JSON)
return JSON.stringify(this);
else if (this.quote)
return this.quote();
else {
var c, i, l = this.length, o = '"';
for (i = 0; i < l; ++i) {
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' + (~~(c / 16)).toString(16) + (c % 16).toString(16);
}
}
}
return o + '"';
}
};
// todo
// startsWith(str) vs lastIndexOf(str, 0) vs (new RegExp('^' + str)).test()
/*
http://javascript.crockford.com/remedial.html
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/quote
http://permalink.gmane.org/gmane.comp.lang.javascript.ecmascript4.general/1994
https://gist.github.com/mathiasbynens/1243213
https://github.com/triblondon/polyfill/blob/master/js.js#L19
*/
//IE Mac
//http://www.robertnyman.com/test/conditional-compilation/conditional-compilation.htmlcons
//Symbol.toStringTag
//https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.prototype.tostring
//https://esdiscuss.org/topic/tostringtag-spoofing-for-null-and-undefined#content-59
//DOMException
//https://github.com/AMorgaut/w3c-domcore-errors
//http://heycam.github.io/webidl/#idl-DOMException-error-names
//http://www.w3.org/TR/2015/WD-dom-20150428/#exception-domexception
//http://stackoverflow.com/a/9856490/248058
/*
HTML5 U+0009, U+000A, U+000C, U+000D, U+0020
HTML4 U+200B
http://www.w3.org/TR/CSS21/syndata.html#characters
http://www.w3.org/TR/html-markup/syntax.html#syntax-text
http://www.w3.org/TR/html401/types.html#type-cdata
http://www.w3.org/TR/xhtml1/#C_15
http://www.w3.org/TR/REC-xml/#NT-Name
http://www.w3.org/TR/REC-xml-names/#NT-NCName
http://en.wikipedia.org/wiki/Valid_characters_in_XML
http://en.wikipedia.org/wiki/Character_encodings_in_HTML#Illegal_characters
*/
/*
https://web.archive.org/web/20061208040954/http://www.hedgerwow.com/360/dhtml/js-array2.html
new ActiveXObject("htmlfile");
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment