You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
685 B
37 lines
685 B
2 years ago
|
function encode(code) {
|
||
|
return code
|
||
|
.replace(/%/g, "%25")
|
||
|
.replace(/</g, "%3C")
|
||
|
.replace(/>/g, "%3E")
|
||
|
.replace(/&/g, "%26")
|
||
|
.replace(/#/g, "%23")
|
||
|
.replace(/{/g, "%7B")
|
||
|
.replace(/}/g, "%7D");
|
||
|
}
|
||
|
|
||
|
function addXmlns(code) {
|
||
|
if (code.indexOf("xmlns") === -1) {
|
||
|
return code.replace(/<svg/g, '<svg xmlns="http://www.w3.org/2000/svg"');
|
||
|
} else {
|
||
|
return code;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function normalize(code) {
|
||
|
return code
|
||
|
.replace(/'/g, "%22")
|
||
|
.replace(/"/g, "'")
|
||
|
.replace(/\s+/g, " ")
|
||
|
.trim();
|
||
|
}
|
||
|
|
||
|
function transform(code) {
|
||
|
return `"data:image/svg+xml;charset=utf-8,${normalize(code)}"`;
|
||
|
}
|
||
|
|
||
|
module.exports = {
|
||
|
encode,
|
||
|
addXmlns,
|
||
|
transform
|
||
|
};
|