Skip to content

Commit 94f2ef3

Browse files
committed
maint(core mockup-parser): fix parsing JSON values in data attributes
1 parent 16a46e4 commit 94f2ef3

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

src/core/mockup-parser.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ const parser = {
2727
el_options = el.getAttribute(`data-pat-${pattern_name}`);
2828
if (el_options) {
2929
// parse options if string
30-
if (typeof el_options === "string") {
30+
try {
31+
el_options = JSON.parse(el_options);
32+
} catch {
33+
// not JSON, try to parse it as semi-colon/colon list
3134
const tmp_options = {};
3235
el_options.split(";").forEach((item) => {
3336
item = item.split(":");

src/core/mockup-parser.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,16 @@ describe("The mockup-parser", function () {
4444
expect(options.option2).toBe("value2");
4545
expect(options.injectedOption).toBe("injectedValue");
4646
});
47+
it("parses JSON data attribute of a single node", function () {
48+
const el = document.createElement("div");
49+
el.setAttribute(
50+
"data-pat-testpattern",
51+
'{"option1": "value1", "option2": "value2"}'
52+
);
53+
54+
const options = mockupParser.getOptions(el, "testpattern");
55+
56+
expect(options.option1).toBe("value1");
57+
expect(options.option2).toBe("value2");
58+
});
4759
});

0 commit comments

Comments
 (0)