diff --git a/src/stringy.js b/src/stringy.js index 18fb2d1..78f781b 100644 --- a/src/stringy.js +++ b/src/stringy.js @@ -1,16 +1,22 @@ // stringify is for passing objects as html atttributes. // JSON.stringify doesn't work if your object has double and single quotes as it will end the string early. export function stringifyAttribute(ob) { - if(!ob) return '' - let s = '' + if (!ob) { + return '' + } + + let s if (typeof ob === 'string') { s = ob } else { s = JSON.stringify(ob) } - s = s.replaceAll( - '"', - '"' - ) + + // Escape double quotes for HTML attributes + s = s.replaceAll('"', '"') + + // Escape single quotes for HTML attributes + s = s.replaceAll("'", ''') + return s }