Skip to content

Commit

Permalink
Encode attribute values
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkldshv committed Nov 12, 2022
1 parent 04cd91e commit bb05a71
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,14 @@ export function segment(
: [...graphemeSegmenter.segment(content)].map((seg) => seg.segment)
}

function encodeAttributeValue(value: string) {
return value
.replace(/"/g, '"')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/&/g, '&amp;')
}

export function buildXMLString(
type: string,
attrs: Record<string, any>,
Expand All @@ -193,7 +201,11 @@ export function buildXMLString(

for (const [k, _v] of Object.entries(attrs)) {
if (typeof _v !== 'undefined') {
attrString += ` ${k}="${_v}"`
if (typeof _v === 'string') {
attrString += ` ${k}="${encodeAttributeValue(_v)}"`
} else {
attrString += ` ${k}="${_v}"`
}
}
}

Expand Down

0 comments on commit bb05a71

Please sign in to comment.