mirror of
https://github.com/pebble-dev/clay.git
synced 2026-08-28 04:46:57 -04:00
Merge pull request #56 from pebble/#55/unicode-support
Allow unicode characters in config
This commit is contained in:
+6
-1
@@ -20,9 +20,14 @@
|
||||
|
||||
var returnTo = getQueryParam('return_to');
|
||||
var data = decodeURIComponent(location.hash.substring(1));
|
||||
if (data) {
|
||||
|
||||
// Check if we dealing with a base64 encoded URI
|
||||
if (data && data.charAt(0) !== '<') {
|
||||
data = window.atob(data).replace('$$RETURN_TO$$', returnTo);
|
||||
window.location.href = 'data:text/html;base64,' + encodeURIComponent(window.btoa(data));
|
||||
} else if (data) {
|
||||
data = data.replace('$$RETURN_TO$$', returnTo);
|
||||
window.location.href = 'data:text/html;charset=utf-8,' + encodeURIComponent(data);
|
||||
}
|
||||
</script>
|
||||
<body>
|
||||
|
||||
@@ -184,58 +184,12 @@ Clay.prototype.getSettings = function(response, convert) {
|
||||
|
||||
/**
|
||||
* @param {string} input
|
||||
* @param {string} [prefix]
|
||||
* @param {string} [prefix='data:text/html;charset=utf-8,']
|
||||
* @returns {string}
|
||||
*/
|
||||
Clay.encodeDataUri = function(input, prefix) {
|
||||
prefix = typeof prefix !== 'undefined' ? prefix : 'data:text/html;base64,';
|
||||
|
||||
if (window.btoa) {
|
||||
return prefix + encodeURIComponent(window.btoa(input));
|
||||
}
|
||||
|
||||
// iOS doesn't have a window so we need to polyfil window.btoa
|
||||
// extracted from https://github.com/inexorabletash/polyfill
|
||||
var B64_ALPHABET =
|
||||
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
|
||||
input = String(input);
|
||||
var position = 0;
|
||||
var out = [];
|
||||
var o1;
|
||||
var o2;
|
||||
var o3;
|
||||
var e1;
|
||||
var e2;
|
||||
var e3;
|
||||
var e4;
|
||||
|
||||
if (/[^\x00-\xFF]/.test(input)) { throw Error('InvalidCharacterError'); }
|
||||
|
||||
while (position < input.length) {
|
||||
o1 = input.charCodeAt(position++);
|
||||
o2 = input.charCodeAt(position++);
|
||||
o3 = input.charCodeAt(position++);
|
||||
|
||||
// 111111 112222 222233 333333
|
||||
e1 = o1 >> 2;
|
||||
e2 = ((o1 & 0x3) << 4) | (o2 >> 4);
|
||||
e3 = ((o2 & 0xf) << 2) | (o3 >> 6);
|
||||
e4 = o3 & 0x3f;
|
||||
|
||||
if (position === input.length + 2) {
|
||||
e3 = 64;
|
||||
e4 = 64;
|
||||
} else if (position === input.length + 1) {
|
||||
e4 = 64;
|
||||
}
|
||||
|
||||
out.push(B64_ALPHABET.charAt(e1),
|
||||
B64_ALPHABET.charAt(e2),
|
||||
B64_ALPHABET.charAt(e3),
|
||||
B64_ALPHABET.charAt(e4));
|
||||
}
|
||||
|
||||
return prefix + encodeURIComponent(out.join(''));
|
||||
prefix = typeof prefix !== 'undefined' ? prefix : 'data:text/html;charset=utf-8,';
|
||||
return prefix + encodeURIComponent(input);
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
+16
-41
@@ -153,7 +153,7 @@ describe('Clay', function() {
|
||||
* @returns {string}
|
||||
*/
|
||||
function decodeUrl(url) {
|
||||
return atob(decodeURIComponent(url.replace(/^.*?[#,]/, '')));
|
||||
return decodeURIComponent(url.replace(/^.*?[#,]/, ''));
|
||||
}
|
||||
|
||||
describe('string substitutions', function() {
|
||||
@@ -360,50 +360,25 @@ describe('Clay', function() {
|
||||
});
|
||||
|
||||
describe('Clay.encodeDataUri()', function() {
|
||||
|
||||
/**
|
||||
* @return {void}
|
||||
*/
|
||||
function testEncodeDataUri() {
|
||||
it('adds the correct prefix', function() {
|
||||
assert.equal(Clay.encodeDataUri('test', 'prefix:'), 'prefix:dGVzdA%3D%3D');
|
||||
assert.equal(
|
||||
Clay.encodeDataUri('test'),
|
||||
'data:text/html;base64,dGVzdA%3D%3D'
|
||||
);
|
||||
});
|
||||
|
||||
it('encodes the data correctly', function() {
|
||||
assert.equal(Clay.encodeDataUri('test', ''), 'dGVzdA%3D%3D');
|
||||
assert.equal(Clay.encodeDataUri('test{2}', ''), 'dGVzdHsyfQ%3D%3D');
|
||||
assert.equal(Clay.encodeDataUri('test{10}', ''), 'dGVzdHsxMH0%3D');
|
||||
});
|
||||
|
||||
it('throws if the input is invalid', function() {
|
||||
assert.throws(function() {
|
||||
Clay.encodeDataUri('♥');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
describe('native', function() {
|
||||
testEncodeDataUri();
|
||||
it('adds the correct prefix', function() {
|
||||
assert.equal(Clay.encodeDataUri('<test>', 'prefix:'), 'prefix:%3Ctest%3E');
|
||||
assert.equal(
|
||||
Clay.encodeDataUri('<test>'),
|
||||
'data:text/html;charset=utf-8,%3Ctest%3E'
|
||||
);
|
||||
});
|
||||
|
||||
describe('polyfill', function() {
|
||||
var btoaOriginal = window.btoa;
|
||||
|
||||
before(function() {
|
||||
window.btoa = undefined;
|
||||
});
|
||||
|
||||
testEncodeDataUri();
|
||||
|
||||
after(function() {
|
||||
window.btoa = btoaOriginal;
|
||||
});
|
||||
it('encodes the data correctly', function() {
|
||||
assert.equal(Clay.encodeDataUri('test', ''), 'test');
|
||||
assert.equal(Clay.encodeDataUri('test{2}', ''), 'test%7B2%7D');
|
||||
assert.equal(Clay.encodeDataUri('test{10}', ''), 'test%7B10%7D');
|
||||
});
|
||||
|
||||
it('does not throw if unicode character is provided', function() {
|
||||
assert.doesNotThrow(function() {
|
||||
Clay.encodeDataUri('♥');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('.prepareForAppMessage', function() {
|
||||
|
||||
Reference in New Issue
Block a user