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(svg): encode HTML when converting vnode to string. #935

Merged
merged 1 commit into from
Jul 20, 2022
Merged
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
18 changes: 18 additions & 0 deletions src/core/dom.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

import env from './env';
import {buildTransformer} from './fourPointsTransform';
import {Dictionary} from './types';

const EVENT_SAVED_PROP = '___zrEVENTSAVED';
const _calcOut: number[] = [];
Expand Down Expand Up @@ -167,3 +168,20 @@ function preparePointerTransformer(markers: HTMLDivElement[], saved: SavedInfo,
export function isCanvasEl(el: HTMLElement): el is HTMLCanvasElement {
return el.nodeName.toUpperCase() === 'CANVAS';
}

const replaceReg = /([&<>"'])/g;
const replaceMap: Dictionary<string> = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
'\'': '&#39;'
};

export function encodeHTML(source: string): string {
return source == null
? ''
: (source + '').replace(replaceReg, function (str, c) {
return replaceMap[c];
});
}
3 changes: 2 additions & 1 deletion src/svg/core.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { keys, map } from '../core/util';
import { encodeHTML } from '../core/dom';

export type CSSSelectorVNode = Record<string, string>
export type CSSAnimationVNode = Record<string, Record<string, string>>
Expand Down Expand Up @@ -71,7 +72,7 @@ export function vNodeToString(el: SVGVNode, opts?: {
function convertElToString(el: SVGVNode): string {
const {children, tag, attrs} = el;
return createElementOpen(tag, attrs)
+ (el.text || '')
+ encodeHTML(el.text)
+ (children ? `${S}${map(children, child => convertElToString(child)).join(S)}${S}` : '')
+ createElementClose(tag);
}
Expand Down