Skip to content

Commit 1905567

Browse files
fix(node-options): encode urls in default node options
1 parent 60e794a commit 1905567

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

__test__/mock/json-element-mock-result.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const styleObjHtml = "<h1 style=\"text-align:justify;\">heading1</h1><h2 style=\
2424
const referenceObjHtml = "<p><a class=\"embedded-entry redactor-component block-entry\" href=\"/test\" target=\"_self\">Embed entry as a link</a></p><p><a class=\"embedded-entry redactor-component block-entry\" href=\"/entry-3\" target=\"_blank\">Open entry as a link in new tab</a></p><p><a class=\"embedded-entry redactor-component block-entry\" href=\"/entry-2\" target=\"_self\">Bold entry</a></p><p><a class=\"embedded-entry redactor-component block-entry\" href=\"/entry-4\" target=\"_blank\"><strong>Bold entry open in new tab</strong></a></p>"
2525
const referenceObjHtmlBlock = "<p><a class=\"embedded-entry redactor-component block-entry\" href=\"/Test\" target=\"_self\">Embed entry as a link</a></p><p><a class=\"embedded-entry redactor-component block-entry\" href=\"undefined\" target=\"_blank\">Embed entry as a link open in new tab</a></p><ul><li><a class=\"embedded-entry redactor-component block-entry\" href=\"undefined\" target=\"_self\">Entry as a link</a></li><li><a class=\"embedded-entry redactor-component block-entry\" href=\"undefined\" target=\"_blank\">Open entry as a link in new tab</a></li><li><a class=\"embedded-entry redactor-component block-entry\" href=\"undefined\" target=\"_self\"><strong><u>Entry as a link bold</u></strong></a></li><li><a class=\"embedded-entry redactor-component block-entry\" href=\"khjgf\" target=\"_blank\"><strong><u>Open bold entry as a link in new tab </u></strong></a></li><li><a href=\"https://\" target=\"_self\"><strong><u>Link URL</u></strong></a></li><li><a href=\"https://\" target=\"_blank\"><strong><u>Open link in new tab</u></strong></a></li></ul>"
2626
const imagetags = "<figure style=\"text-align:right;max-width:137px;float:right;width:137px;max-height:257px;height:257px;\"><a href=\"https://batman.com\" target=\"_blank\"><img asset_uid=\"asset-UID\" class=\"embedded-asset\" src=\"https://images.contentstack.io/v3/assets/api-key/asset-UID/random-uid/batman.png\" alt=\"batman\" target=\"_blank\" style=\"text-align:right;max-width:137px;float:right;width:137px;max-height:257px;height:257px;\" /></a><figcaption>The Batman</figcaption></figure>"
27-
const escapeHtml = "<p>&lt;p&gt;Welcome to Contentstack! &lt;script&gt;console.log(/\"Hello from Contentstack!/\");&lt;/script&gt; Explore our platform to create, manage, and publish content seamlessly.&lt;/p&gt;</p>"
27+
const escapeHtml = "<p>&lt;p&gt;Welcome to Contentstack! &lt;script&gt;console.log(/&quot;Hello from Contentstack!/&quot;);&lt;/script&gt; Explore our platform to create, manage, and publish content seamlessly.&lt;/p&gt;</p>"
2828

2929
export {
3030
h1Html,

src/options/default-node-options.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ export const defaultNodeOption: RenderOption = {
1919
return `<a${node.attrs.style ? ` style="${node.attrs.style}"` : ``}${node.attrs['class-name'] ? ` class="${node.attrs['class-name']}"` : ``}${node.attrs.id ? ` id="${node.attrs.id}"` : ``} href="${sanitizedHref}">${sanitizeHTML(next(node.children))}</a>`
2020
},
2121
[NodeType.IMAGE]:(node: Node, next: Next) => {
22-
const sanitizedSrc = sanitizeHTML(node.attrs.src || node.attrs.url);
22+
const sanitizedSrc = encodeURI(sanitizeHTML(node.attrs.src || node.attrs.url));
2323
return `<img${node.attrs.style ? ` style="${node.attrs.style}"` : ``}${node.attrs['class-name'] ? ` class="${node.attrs['class-name']}"` : ``}${node.attrs.id ? ` id="${node.attrs.id}"` : ``} src="${sanitizedSrc}" />${sanitizeHTML(next(node.children))}`
2424
},
2525
[NodeType.EMBED]:(node: Node, next: Next) => {
26-
const sanitizedSrc = sanitizeHTML(node.attrs.src || node.attrs.url);
26+
const sanitizedSrc = encodeURI(sanitizeHTML(node.attrs.src || node.attrs.url));
2727
return `<iframe${node.attrs.style ? ` style="${node.attrs.style}"` : ``}${node.attrs['class-name'] ? ` class="${node.attrs['class-name']}"` : ``}${node.attrs.id ? ` id="${node.attrs.id}"` : ``} src="${sanitizedSrc}">${sanitizeHTML(next(node.children))}</iframe>`
2828
},
2929
[NodeType.HEADING_1]:(node: Node, next: Next) => {
@@ -123,7 +123,7 @@ export const defaultNodeOption: RenderOption = {
123123

124124
['reference']:(node: Node, next: Next) => {
125125
if (node.attrs.type === 'asset') {
126-
const src = node.attrs['asset-link'];
126+
const src = encodeURI(node.attrs['asset-link']);
127127
const alt = node.attrs?.['redactor-attributes']?.['alt'];
128128
const link = node.attrs.link;
129129
const target = node.attrs.target || "";

src/options/default-options.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@ export const defaultOptions: RenderOption = {
1616
return `<span>${title}</span>`;
1717
},
1818
[StyleType.LINK]: (item: EmbeddedItem | EntryNode, metadata: Metadata) => {
19-
const url = sanitizeHTML(item.url || 'undefined');
19+
const url = encodeURI(sanitizeHTML(item.url || 'undefined'));
2020
const text = sanitizeHTML(metadata.text || item.title || item.uid || (item.system ? item.system.uid : ''));
2121
return `<a href="${url}">${text}</a>`;
2222
},
2323
[StyleType.DISPLAY]: (item: EmbeddedItem | EntryNode, metadata: Metadata) => {
24-
const url = sanitizeHTML(item.url || 'undefined');
24+
const url = encodeURI(sanitizeHTML(item.url || 'undefined'));
2525
const alt = sanitizeHTML(metadata.attributes.alt || item.title || item.filename || item.uid
2626
|| (item.system ? item.system.uid : ''));
2727
return `<img src="${url}" alt="${alt}" />`;
2828
},
2929
[StyleType.DOWNLOAD]: (item: EmbeddedItem | EntryNode, metadata: Metadata) => {
30-
const href = sanitizeHTML(item.url || 'undefined');
30+
const href = encodeURI(sanitizeHTML(item.url || 'undefined'));
3131
const text = sanitizeHTML(metadata.text || item.title || item.uid || (item.system ? item.system.content_type_uid : ''));
3232
return `<a href="${href}">${text}</a>`;
3333
},

0 commit comments

Comments
 (0)