escape selector attribute value inside double quotes
// When passing user input into querySelector(All) the embedded string must not alter
// the semantics of the query. This escape function is safe to use when we know the
// provided value is going to be wrapped in double quotes as part of an attribute selector
// Do not use it anywhere else
// we escape double quotes and backslashes
// 当将用户输入传递给 querySelector(All) 时,嵌入的字符串不能改变查询的语义。该转义
// 函数在我们知道提供的值将作为属性选择器的一部分用双引号包裹时可以安全使用。
// 不要在其他地方使用它. 我们转义双引号和反斜杠
const escapeSelectorAttributeValueInsideDoubleQuotesRegex = /[\n\"\\]/g;
export default function escapeSelectorAttributeValueInsideDoubleQuotes(
value: string,
): string {
return value.replace(
escapeSelectorAttributeValueInsideDoubleQuotesRegex,
ch => '\\' + ch.charCodeAt(0).toString(16) + ' ',
);
}