Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BuilderOption: keepEntityCharRefs #131

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ export interface XMLBuilderOptions {
* - str - the input string
*/
invalidCharReplacement: string | ((char: string, offset: number, str: string) => string) | undefined
/**
* Whether predefined entities and character references in text nodes are converted or not.
*/
keepEntityCharRefs: boolean
/**
* Defines custom parser functions.
*/
Expand Down Expand Up @@ -143,6 +147,7 @@ export const DefaultBuilderOptions: XMLBuilderOptions = {
xlink: "http://www.w3.org/1999/xlink"
},
invalidCharReplacement: undefined,
keepEntityCharRefs: false,
parser: undefined
}

Expand Down Expand Up @@ -1342,6 +1347,10 @@ export type XMLBuilderCBOptions = CBWriterOptions & {
* a replacement character.
*/
invalidCharReplacement: string | ((substring: string, ...args: any[]) => string) | undefined
/**
* Whether predefined entities and character references in text nodes are converted or not.
*/
keepEntityCharRefs: boolean
}

/**
Expand Down Expand Up @@ -1507,7 +1516,8 @@ export const DefaultXMLBuilderCBOptions: Partial<XMLBuilderCBOptions> = {
mathml: "http://www.w3.org/1998/Math/MathML",
svg: "http://www.w3.org/2000/svg",
xlink: "http://www.w3.org/1999/xlink"
}
},
keepEntityCharRefs: false
}

type RecursivePartial<T> = {
Expand Down
2 changes: 1 addition & 1 deletion src/readers/BaseReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export abstract class BaseReader<U extends string | ExpandObject> {
* @param text - text value to serialize
*/
_decodeText(text: string): string {
if (text == null) return text
if (text == null || this._builderOptions.keepEntityCharRefs) return text

return text.replace(/&(quot|amp|apos|lt|gt);/g, (_match, tag) =>
BaseReader._entityTable[tag]
Expand Down
3 changes: 3 additions & 0 deletions src/writers/BaseWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,7 @@ export abstract class BaseWriter<T extends BaseWriterOptions, U extends XMLSeria
* @param level - current depth of the XML tree
*/
private _serializeText(node: CharacterData, requireWellFormed: boolean): void {
if (this._builderOptions.keepEntityCharRefs) return

/**
* 1. If the require well-formed flag is set (its value is true), and
Expand All @@ -932,6 +933,8 @@ export abstract class BaseWriter<T extends BaseWriterOptions, U extends XMLSeria
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')

if (this._builderOptions.keepEntityCharRefs) markup = node.data

this.text(markup)
}

Expand Down