Skip to content

Commit

Permalink
fix escape HTML in ftd.text (#1296)
Browse files Browse the repository at this point in the history
* fix escape HTML in ftd.text

* added more characters in escape map

* fixed tests

* fixed test
  • Loading branch information
harshdoesdev authored Sep 11, 2023
1 parent 4a404d5 commit cc6c4d0
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 54 deletions.
3 changes: 1 addition & 2 deletions fastn-js/js/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -1836,8 +1836,7 @@ class Node2 {
} else if (kind === fastn_dom.PropertyKind.StringValue) {
this.#rawInnerValue = staticValue;
if (!ssr) {
let escapedHtmlValue = fastn_utils.escapeHtmlInMarkdown(staticValue);
staticValue = fastn_utils.markdown_inline(escapedHtmlValue);
staticValue = fastn_utils.markdown_inline(staticValue);
staticValue = fastn_utils.process_post_markdown(this.#node, staticValue);
}
this.#node.innerHTML = staticValue;
Expand Down
21 changes: 11 additions & 10 deletions fastn-js/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,21 +421,22 @@ let fastn_utils = {
},

escapeHtmlInMarkdown(str) {
if(typeof str !== 'string') {
return str;
}

let result = "";
let ch_map = {
'<': "&lt;"
'<': "&lt;",
'>': "&gt;",
'&': "&amp;",
'"': "&quot;",
"'": "&#39;",
'/': "&#47;",
};
// To avoid replacing html characters inside <code> body
let backtick_found = false;
for (var i = 0; i < str.length; i++) {
let current = str[i];
if (current === '`') backtick_found = !backtick_found;
if (ch_map[current] !== undefined && !backtick_found) {
result += ch_map[current];
}
else {
result += current;
}
result += ch_map[current] ?? current;
}
return result;
},
Expand Down
2 changes: 1 addition & 1 deletion fastn-js/js/virtual.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class Node {
toHtmlAsString() {
const openingTag = `<${this.#tagName}${this.getDataIdString()}${this.getAttributesString()}${this.getClassString()}${this.getStyleString()}>`;
const closingTag = `</${this.#tagName}>`;
const innerHTML = this.innerHTML;
const innerHTML = fastn_utils.escapeHtmlInMarkdown(this.innerHTML);
const childNodes = this.#children.map(child => child.toHtmlAsString()).join('');

return `${openingTag}${innerHTML}${childNodes}${closingTag}`;
Expand Down
2 changes: 1 addition & 1 deletion ftd/t/js/01-basic.html

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

10 changes: 5 additions & 5 deletions ftd/t/js/03-common-properties.html

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

8 changes: 4 additions & 4 deletions ftd/t/js/09-text-properties.html

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

2 changes: 1 addition & 1 deletion ftd/t/js/18-rive.html

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

2 changes: 1 addition & 1 deletion ftd/t/js/31-advance-list.html

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

44 changes: 22 additions & 22 deletions ftd/t/js/46-code-languages.html

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

Loading

0 comments on commit cc6c4d0

Please sign in to comment.