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

fix: enable client parser to retain carriage return characters #902

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
16 changes: 16 additions & 0 deletions src/client/domparser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,29 @@
};
}

/**
* Escapes special characters before parsing.
*
* @param html - The HTML string.
* @returns - HTML string with escaped special characters.
*/
function escapeSpecialCharacters(html: string): string {
remarkablemark marked this conversation as resolved.
Show resolved Hide resolved
return html.replace(/[\r]/g, (match) => {
if (match === '\r') return '\\r';
return match;

Check warning on line 121 in src/client/domparser.ts

View check run for this annotation

Codecov / codecov/patch

src/client/domparser.ts#L121

Added line #L121 was not covered by tests
});
}

/**
* Parses HTML string to DOM nodes.
*
* @param html - HTML markup.
* @returns - DOM nodes.
*/
export default function domparser(html: string): NodeList {
// Escape special characters before parsing
html = escapeSpecialCharacters(html);

const match = html.match(FIRST_TAG_REGEX);
const firstTagName = match && match[1] ? match[1].toLowerCase() : '';

Expand Down
12 changes: 11 additions & 1 deletion src/client/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ function formatTagName(tagName: string): string {
return tagName;
}

/**
* Reverts escaped special characters back to actual characters.
*
* @param text - The text with escaped characters.
* @returns - Text with escaped characters reverted.
*/
function revertEscapedCharacters(text: string): string {
remarkablemark marked this conversation as resolved.
Show resolved Hide resolved
return text.replace(/\\r/g, '\r');
}

/**
* Transforms DOM nodes to `domhandler` nodes.
*
Expand Down Expand Up @@ -95,7 +105,7 @@ export function formatDOM(
}

case 3:
current = new Text(node.nodeValue!);
current = new Text(revertEscapedCharacters(node.nodeValue!));
break;

case 8:
Expand Down