From 982eb2f91fb1be9119bf5e4900ddabfc9207f021 Mon Sep 17 00:00:00 2001 From: Graeme Lambert Date: Sat, 14 Nov 2020 17:18:27 +0000 Subject: [PATCH] Added support for JSON strings in data attributes --- lib/index.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 1b78caa..1e3c5a6 100644 --- a/lib/index.js +++ b/lib/index.js @@ -80,7 +80,11 @@ function render(tree, options) { for (const key in object) { if (typeof object[key] === 'string') { - if (quoteAllAttributes || object[key].match(ATTRIBUTE_QUOTES_REQUIRED)) { + let isJson = false; + try { JSON.parse(object[key]); isJson = true; } catch (e) { } + if (isJson) { + attr += ' ' + key + "='" + object[key] + "'"; + } else if (quoteAllAttributes || object[key].match(ATTRIBUTE_QUOTES_REQUIRED)) { let attrValue = object[key]; if (replaceQuote) {