Skip to content

Commit bb05a71

Browse files
committed
Encode attribute values
1 parent 04cd91e commit bb05a71

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/utils.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,14 @@ export function segment(
184184
: [...graphemeSegmenter.segment(content)].map((seg) => seg.segment)
185185
}
186186

187+
function encodeAttributeValue(value: string) {
188+
return value
189+
.replace(/"/g, '"')
190+
.replace(/</g, '&lt;')
191+
.replace(/>/g, '&gt;')
192+
.replace(/&/g, '&amp;')
193+
}
194+
187195
export function buildXMLString(
188196
type: string,
189197
attrs: Record<string, any>,
@@ -193,7 +201,11 @@ export function buildXMLString(
193201

194202
for (const [k, _v] of Object.entries(attrs)) {
195203
if (typeof _v !== 'undefined') {
196-
attrString += ` ${k}="${_v}"`
204+
if (typeof _v === 'string') {
205+
attrString += ` ${k}="${encodeAttributeValue(_v)}"`
206+
} else {
207+
attrString += ` ${k}="${_v}"`
208+
}
197209
}
198210
}
199211

0 commit comments

Comments
 (0)