Skip to content

Instantly share code, notes, and snippets.

@Reinmar Reinmar/encoding.html Secret
Created Jun 7, 2015

Embed
What would you like to do?
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Encoding tests</title>
</head>
<body>
<textarea id="pleasegoaway" cols="100" rows="20"></textarea>
<script>
'use strict';
var MAX_BMP = 0xFFFF, // Basic Multilingual Plane - max range of fromCharCode().
MAX_UNICODE = 0x10FFFF,
MAX = String.fromCodePoint ? MAX_UNICODE : MAX_BMP,
// String.fromCodePoint was introduced by ES6. The difference between code point and char code is that
// char code's range is, in case of ES, only 16 bit while code points are related to Unicode (which requires 21 bits)
// Unicode characters beyond ES characters's range are represented by two ES characters.
//
// > String.fromCodePoint( 0xFFFF + 1 ).length
// 2
// > String.fromCharCode( 0xFFFF + 1 ).length
// 1
// > String.fromCharCode( 0xFFFF + 1 ) == String.fromCharCode( 0 )
// true
//
fromCode = String.fromCodePoint || String.fromCharCode,
logs = document.getElementById( 'pleasegoaway' );
log( 'Testing with String.' + ( String.fromCodePoint ? 'fromCodePoint' : 'fromCharCode' ) + '()' );
// Start async loop which will test text encoding and then run testAttributes.
testText();
function testText() {
var charCode = 0,
char,
el = document.createElement( 'div' );
test();
function test() {
for ( ; charCode < MAX; charCode++ ) {
char = fromCode( charCode );
el.innerHTML = char;
if ( el.innerHTML != char ) {
log( 'ERROR HTML - code: ' + charCode + ', char: ' + char + ', actual: ' + el.innerHTML + ' ' + toCharCodes( el.innerHTML ) );
}
if ( charCode % 0xFFFF == 0xFFFE ) {
// This info isn't particularly useful so log only on the console.
console.log( '... ' + charCode );
charCode++;
// This loop may take few seconds, so split it to avoid alerts.
setTimeout( test );
return;
}
}
log( 'Done text. Finished at ' + charCode + '.' );
testAttributes();
}
}
function testAttributes() {
var charCode = 0,
char,
actualAttr,
el = document.createElement( 'div' ),
el2 = document.createElement( 'div' );
el.appendChild( el2 );
test();
function test() {
for ( ; charCode < MAX; charCode++ ) {
char = fromCode( charCode );
el2.setAttribute( 'data-foo', char );
actualAttr = el.innerHTML.replace( /^<div data-foo=["']/i, '' ).replace( /["']><\/div>$/i, '' );
if ( char != actualAttr ) {
log( 'ERROR ATTR - code: ' + charCode + ', char: ' + char + ', actual: ' + actualAttr + ' ' + toCharCodes( actualAttr ) );
}
if ( charCode % 0xFFFF == 0xFFFE ) {
// This info isn't particularly useful so log only on the console.
console.log( '... ' + charCode );
charCode++;
// This loop may take few seconds, so split it to avoid alerts.
setTimeout( test );
return;
}
}
log( 'Done attributes. Finished at ' + charCode + '.' );
}
}
function toCharCodes( str ) {
var codes = [];
for ( var i = 0; i < str.length; i++ ) {
codes.push( str.charCodeAt( i ) );
}
return '<' + codes.join( ',' ) + '>';
}
// MS developers are lazy and you cannot copy multiple messages from their consoles...
// so let's use a textarea.
function log( msg ) {
logs.value += msg + '\n';
console.log( msg );
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.