Skip to content

Commit

Permalink
Encoding cleanup, dropped dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
JrMasterModelBuilder committed Sep 22, 2023
1 parent 17d2809 commit aff707b
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 50 deletions.
14 changes: 1 addition & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
"typescript": "^5.2.2"
},
"dependencies": {
"@xmldom/xmldom": "^0.8.10",
"entities": "^4.5.0"
"@xmldom/xmldom": "^0.8.10"
}
}
21 changes: 0 additions & 21 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {decodeXML, encodeXML} from 'entities';
import {DOMParser} from '@xmldom/xmldom';

const numberLimit = 0x1fffffffffffff;
Expand All @@ -16,26 +15,6 @@ export interface IElement {
toString: () => string;
}

/**
* Encode string for XML.
*
* @param value String value.
* @returns Escaped string.
*/
export function xmlEntitiesEncode(value: string) {
return encodeXML(value);
}

/**
* Decode string for XML.
*
* @param value Encoded value.
* @returns Decoded string.
*/
export function xmlEntitiesDecode(value: string) {
return decodeXML(value);
}

/**
* Decode an XML string.
*
Expand Down
11 changes: 6 additions & 5 deletions src/value/dict.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {IToXmlOptioned} from '../options';
import {IElement, xmlElementChildElements, xmlEntitiesEncode} from '../util';
import {IElement, xmlElementChildElements} from '../util';
import {Value} from '../value';

import {ValueArray} from './array';
Expand Down Expand Up @@ -213,10 +213,11 @@ export class ValueDict extends Value {
const p2 = optioned.indentString.repeat(depth + 1);
const r = [`${p}<dict>`];
for (const [key, val] of v) {
r.push(
`${p2}<key>${xmlEntitiesEncode(key)}</key>`,
val.toXml(optioned, depth + 1)
);
const e = key
.replaceAll('&', '&amp;')
.replaceAll('<', '&lt;')
.replaceAll('>', '&gt;');
r.push(`${p2}<key>${e}</key>`, val.toXml(optioned, depth + 1));
}
r.push(`${p}</dict>`);
return r.join(optioned.newlineString);
Expand Down
14 changes: 7 additions & 7 deletions src/value/string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ void describe('value/string', () => {
);
});

void it('<&>', () => {
const el = new ValueString('<&>');
strictEqual(el.toXml(), '<string>&lt;&amp;&gt;</string>');
void it(`<'"&"'>`, () => {
const el = new ValueString(`<'"&"'>`);
strictEqual(el.toXml(), `<string>&lt;'"&amp;"'&gt;</string>`);
strictEqual(
el.toXml(null, 1),
'\t<string>&lt;&amp;&gt;</string>'
`\t<string>&lt;'"&amp;"'&gt;</string>`
);
});
});
Expand All @@ -45,10 +45,10 @@ void describe('value/string', () => {
strictEqual(el.value, 'hello world');
});

void it('<&>', () => {
void it(`<'"&"'>`, () => {
const el = new ValueString('');
el.fromXml('<string>&lt;&amp;&gt;</string>');
strictEqual(el.value, '<&>');
el.fromXml(`<string>&lt;'"&amp;"'&gt;</string>`);
strictEqual(el.value, `<'"&"'>`);
});

void it('empty', () => {
Expand Down
8 changes: 6 additions & 2 deletions src/value/string.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {IToXmlOptioned} from '../options';
import {IElement, xmlEntitiesEncode} from '../util';
import {IElement} from '../util';
import {Value} from '../value';

/**
Expand Down Expand Up @@ -51,6 +51,10 @@ export class ValueString extends Value {
*/
protected _toXml(optioned: Readonly<IToXmlOptioned>, depth: number) {
const p = optioned.indentString.repeat(depth);
return `${p}<string>${xmlEntitiesEncode(this.value)}</string>`;
const e = this.value
.replaceAll('&', '&amp;')
.replaceAll('<', '&lt;')
.replaceAll('>', '&gt;');
return `${p}<string>${e}</string>`;
}
}

0 comments on commit aff707b

Please sign in to comment.