Skip to content

Commit

Permalink
Fix replacement encoding mode
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiasbynens committed Sep 5, 2021
1 parent 1d11ed1 commit b3445a9
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ This function takes a plain text string (the `input` parameter) and encodes it a
const encodedData = iso885914.encode(text);
```

The optional `options` object and its `mode` property can be used to set the error mode. The two available error modes are `'fatal'` (the default) or `'replacement'`. (Note: This differs from [the spec](https://encoding.spec.whatwg.org/#error-mode), which recognizes `'fatal`' and `'html'` modes for encoders. The reason behind this difference is that the spec algorithm is aimed at producing HTML, whereas this library encodes into an environment-agnostic `Uint16Array` of bytes.)
The optional `options` object and its `mode` property can be used to set the error mode. The two available error modes are `'fatal'` (the default) or `'replacement'`. (Note: This differs from [the spec](https://encoding.spec.whatwg.org/#error-mode), which recognizes fatal and HTML” modes for encoders. The reason behind this difference is that the spec algorithm is aimed at producing HTML, whereas this library encodes into an environment-agnostic `Uint16Array` of bytes.)

```js
const encodedData = iso885914.encode(text, {
Expand Down
5 changes: 2 additions & 3 deletions iso-8859-14.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,8 @@ export const encode = (input, options) => {
if (options && options.mode) {
mode = options.mode.toLowerCase();
}
// “An error mode […] is either `fatal` (default) or `HTML` for an
// encoder.”
if (mode !== 'fatal' && mode !== 'html') {
// Support `fatal` (default) and `replacement` error modes.
if (mode !== 'fatal' && mode !== 'replacement') {
mode = 'fatal';
}
const length = input.length;
Expand Down
4 changes: 2 additions & 2 deletions scripts/transform-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ fs.writeFileSync(
// tests/tests.src.js → tests/tests.js
const TEST_TEMPLATE = fs.readFileSync('./tests/tests.src.mjs', 'utf8');
const createTest = template(TEST_TEMPLATE, {
interpolate: /<%=([\s\S]+?)%>/g,
interpolate: /<\%=([\s\S]+?)%\>/g,
});
const testCode = createTest(require('./export-data.js'));
fs.writeFileSync('./tests/tests.mjs', testCode);

// src/iso-8859-14.src.mjs -> iso-8859-14.mjs
const LIB_TEMPLATE = fs.readFileSync('./src/iso-8859-14.src.mjs', 'utf8');
const createLib = template(LIB_TEMPLATE, {
interpolate: /<%=([\s\S]+?)%>/g,
interpolate: /<\%=([\s\S]+?)%\>/g,
});
const libCode = createLib(require('./export-data.js'));
fs.writeFileSync('./iso-8859-14.mjs', libCode);
5 changes: 2 additions & 3 deletions src/iso-8859-14.src.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,8 @@ export const encode = (input, options) => {
if (options && options.mode) {
mode = options.mode.toLowerCase();
}
// “An error mode […] is either `fatal` (default) or `HTML` for an
// encoder.”
if (mode !== 'fatal' && mode !== 'html') {
// Support `fatal` (default) and `replacement` error modes.
if (mode !== 'fatal' && mode !== 'replacement') {
mode = 'fatal';
}
const length = input.length;
Expand Down
5 changes: 5 additions & 0 deletions tests/tests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ assert.throws(
Error,
'Mode names are case-insensitive'
);
assert.deepStrictEqual(
iso885914.encode('\uFFFF', { mode: 'replacement' }),
new Uint16Array([0xFFFD]),
'Encoding a code point that is invalid for this encoding results in U+FFFD in `replacement` mode'
);

console.log('Testing `iso885914.decode`…');
assert.deepStrictEqual(
Expand Down
5 changes: 5 additions & 0 deletions tests/tests.src.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ assert.throws(
Error,
'Mode names are case-insensitive'
);
assert.deepStrictEqual(
iso885914.encode('\uFFFF', { mode: 'replacement' }),
new Uint16Array([0xFFFD]),
'Encoding a code point that is invalid for this encoding results in U+FFFD in `replacement` mode'
);

console.log('Testing `iso885914.decode`…');
assert.deepStrictEqual(
Expand Down

0 comments on commit b3445a9

Please sign in to comment.