2
0
mirror of https://github.com/tenrok/vue-meta.git synced 2026-06-23 01:30:33 +03:00

refactor: extract escape method

This commit is contained in:
pimlie
2019-03-08 12:58:22 +01:00
committed by Alexander Lichter
parent 31e975d312
commit 95ba9c0291
+47
View File
@@ -0,0 +1,47 @@
import { metaInfoOptionKeys, disableOptionKeys } from './constants'
import isArray from './isArray'
import { isString, isObject } from './typeof'
// sanitizes potentially dangerous characters
export default function escape(info, { tagIDKeyName }, escapeSequences = []) {
const escaped = {}
for (const key in info) {
const value = info[key]
// no need to escape configuration options
if (metaInfoOptionKeys.includes(key)) {
escaped[key] = value
continue
}
let disableKey = disableOptionKeys[0]
if (info[disableKey] && info[disableKey].includes(key)) {
// this info[key] doesnt need to escaped if the option is listed in __dangerouslyDisableSanitizers
escaped[key] = value
continue
}
if (info[tagIDKeyName]) {
disableKey = disableOptionKeys[1]
// items which vmid is listed in __dangerouslyDisableSanitizersByTagID do not need to be escaped
if (info[disableKey] && info[disableKey][key] && info[disableKey][key].includes(info[tagIDKeyName])) {
escaped[key] = value
continue
}
}
if (isString(value)) {
escaped[key] = escapeSequences.reduce((val, [v, r]) => val.replace(v, r), value)
} else if (isArray(value)) {
escaped[key] = value.map(v => escape(v, { tagIDKeyName }, escapeSequences))
} else if (isObject(value)) {
escaped[key] = escape(value, { tagIDKeyName }, escapeSequences)
} else {
escaped[key] = value
}
}
return escaped
}