|
1 | 1 | import '@exodus/bytes/encoding.js' |
2 | 2 | import { percentEncodeAfterEncoding } from '@exodus/bytes/whatwg.js' |
3 | | -import { test } from 'node:test' |
| 3 | +import { describe, test } from 'node:test' |
| 4 | +import { labels } from './encoding/fixtures/encodings.cjs' |
| 5 | + |
| 6 | +const userinfo = ' "#/:;<=>?@[\\]^`{|}' // https://url.spec.whatwg.org/#userinfo-percent-encode-set |
| 7 | +const jsuri = ' "%<>[\\]^`{|}' // https://tc39.es/ecma262/#sec-encodeuri-uri |
| 8 | +const jsuricomponent = ' "#$%&+,/:;<=>?@[\\]^`{|}' // https://tc39.es/ecma262/#sec-encodeuricomponent-uricomponent |
| 9 | + |
| 10 | +const slowEngine = |
| 11 | + process.env.EXODUS_TEST_PLATFORM === 'quickjs' || |
| 12 | + process.env.EXODUS_TEST_PLATFORM === 'xs' || |
| 13 | + process.env.EXODUS_TEST_PLATFORM === 'engine262' |
4 | 14 |
|
5 | 15 | // https://url.spec.whatwg.org/#example-percent-encode-operations |
6 | | -test('percent-encode after encoding', (t) => { |
7 | | - const userinfo = ' "#/:;<=>?@[\\]^`{|}' // https://url.spec.whatwg.org/#userinfo-percent-encode-set |
| 16 | +describe('percent-encode after encoding', () => { |
8 | 17 | const f = percentEncodeAfterEncoding |
9 | | - t.assert.strictEqual(f('Shift_JIS', ' ', userinfo), '%20') |
10 | | - t.assert.strictEqual(f('Shift_JIS', '≡', userinfo), '%81%DF') |
11 | | - t.assert.strictEqual(f('Shift_JIS', '‽', userinfo), '%26%238253%3B') |
12 | | - // t.assert.strictEqual(f('ISO-2022-JP', '¥', userinfo), "%1B(J\\%1B(B") // https://github.com/whatwg/url/issues/895 |
13 | | - t.assert.strictEqual( |
14 | | - f('Shift_JIS', '1+1 ≡ 2%20‽', userinfo, true), |
15 | | - '1+1+%81%DF+2%20%26%238253%3B' |
16 | | - ) |
17 | | - t.assert.strictEqual(f('UTF-8', '≡', userinfo), '%E2%89%A1') |
18 | | - t.assert.strictEqual(f('UTF-8', '‽', userinfo), '%E2%80%BD') |
19 | | - t.assert.strictEqual(f('UTF-8', 'Say what‽', userinfo), 'Say%20what%E2%80%BD') |
| 18 | + |
| 19 | + test('examples from spec', (t) => { |
| 20 | + t.assert.strictEqual(f('Shift_JIS', ' ', userinfo), '%20') |
| 21 | + t.assert.strictEqual(f('Shift_JIS', '≡', userinfo), '%81%DF') |
| 22 | + t.assert.strictEqual(f('Shift_JIS', '‽', userinfo), '%26%238253%3B') |
| 23 | + // t.assert.strictEqual(f('ISO-2022-JP', '¥', userinfo), "%1B(J\\%1B(B") // https://github.com/whatwg/url/issues/895 |
| 24 | + t.assert.strictEqual( |
| 25 | + f('Shift_JIS', '1+1 ≡ 2%20‽', userinfo, true), |
| 26 | + '1+1+%81%DF+2%20%26%238253%3B' |
| 27 | + ) |
| 28 | + t.assert.strictEqual(f('UTF-8', '≡', userinfo), '%E2%89%A1') |
| 29 | + t.assert.strictEqual(f('UTF-8', '‽', userinfo), '%E2%80%BD') |
| 30 | + t.assert.strictEqual(f('UTF-8', 'Say what‽', userinfo), 'Say%20what%E2%80%BD') |
| 31 | + }) |
| 32 | + |
| 33 | + describe('throws on unkown, utf-16 and replacement', () => { |
| 34 | + // https://encoding.spec.whatwg.org/#get-an-encoder |
| 35 | + for (const encoding of ['what', 'UTF-16', 'utf16-le', 'utf16-be', 'replacement', 'unicode']) { |
| 36 | + test(encoding, (t) => { |
| 37 | + t.assert.throws(() => f(encoding, '', userinfo), /encoding/) |
| 38 | + t.assert.throws(() => f(encoding, '', jsuri), /encoding/) |
| 39 | + }) |
| 40 | + } |
| 41 | + }) |
| 42 | + |
| 43 | + describe('throws on non-scalarvalue', () => { |
| 44 | + for (const encoding of labels) { |
| 45 | + test(encoding, (t) => { |
| 46 | + for (let cp = 0xd8_00; cp < 0xe0_00; cp++) { |
| 47 | + const s = String.fromCodePoint(cp) |
| 48 | + t.assert.throws(() => f(encoding, s, userinfo)) |
| 49 | + t.assert.throws(() => f(encoding, s, jsuri)) |
| 50 | + } |
| 51 | + }) |
| 52 | + } |
| 53 | + }) |
| 54 | + |
| 55 | + test('encodeURI / encodeURIComponent', (t) => { |
| 56 | + // https://tc39.es/ecma262/#sec-encodeuri-uri step 2, coherence check |
| 57 | + t.assert.deepStrictEqual([...(jsuri + ';/?:@&=+$,#')].sort(), [...jsuricomponent].sort()) |
| 58 | + |
| 59 | + const MAX = slowEngine ? 0x1_ff_ff : 0x10_ff_ff // Max Unicode codepoint |
| 60 | + for (let cp = 0; cp <= MAX; cp++) { |
| 61 | + if (cp >= 0xd8_00 && cp < 0xe0_00) continue |
| 62 | + const s = String.fromCodePoint(cp) |
| 63 | + t.assert.strictEqual(f('utf8', s, jsuricomponent), encodeURIComponent(s)) |
| 64 | + t.assert.strictEqual(f('utf8', s, jsuri), encodeURI(s)) |
| 65 | + } |
| 66 | + }) |
20 | 67 | }) |
0 commit comments