Skip to content

Commit cbc627d

Browse files
authored
Merge pull request #1270 from Patternslib/petschki-mockup-parser-fixes
modernize code in `mockup-parser.js`
2 parents 8b5afe9 + f064bcc commit cbc627d

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

src/core/mockup-parser.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import $ from "jquery";
21

32
var parser = {
43
getOptions($el, patternName, options) {
@@ -12,7 +11,7 @@ var parser = {
1211
*/
1312
options = options || {};
1413
// get options from parent element first, stop if element tag name is 'body'
15-
if ($el.length !== 0 && !$.nodeName($el[0], "body")) {
14+
if ($el.length !== 0 && $el[0].nodeName !== "BODY") {
1615
options = this.getOptions($el.parent(), patternName, options);
1716
}
1817
// collect all options from element
@@ -23,7 +22,7 @@ var parser = {
2322
// parse options if string
2423
if (typeof elOptions === "string") {
2524
const tmpOptions = {};
26-
$.each(elOptions.split(";"), function (i, item) {
25+
elOptions.split(";").forEach(item => {
2726
item = item.split(":");
2827
item.reverse();
2928
let key = item.pop();
@@ -37,7 +36,10 @@ var parser = {
3736
}
3837
}
3938
}
40-
return $.extend(true, {}, options, elOptions);
39+
return {
40+
...options,
41+
...elOptions,
42+
}
4143
},
4244
};
4345

src/core/mockup-parser.test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import $ from "jquery";
2+
import mockupParser from "./mockup-parser";
3+
4+
describe("The mockup-parser", function () {
5+
it("parses the data attribute of a single node", function () {
6+
const $el = $(`
7+
<span data-pat-testpattern="option1: value1; option2: value2">
8+
mockup parser test
9+
</span>`);
10+
const options = mockupParser.getOptions($el, "testpattern");
11+
expect(options.option1).toBe("value1");
12+
expect(options.option2).toBe("value2");
13+
});
14+
it("parses the data attribute of nested nodes", function () {
15+
const $el = $(`
16+
<div data-pat-testpattern="parentOption1: value1; parentOption2: value2">
17+
<span data-pat-testpattern="option1: subvalue1; option2: subvalue2">
18+
nested mockup parser test
19+
</span>
20+
</div>`);
21+
const options = mockupParser.getOptions($el, "testpattern");
22+
expect(options.parentOption1).toBe("value1");
23+
expect(options.parentOption2).toBe("value2");
24+
expect(options.option1).toBe(undefined);
25+
expect(options.option2).toBe(undefined);
26+
});
27+
it("parses the data attribute of a single node and preserves injected options", function () {
28+
const $el = $(`
29+
<span data-pat-testpattern="option1: value1; option2: value2">
30+
mockup parser test
31+
</span>
32+
`);
33+
const options = mockupParser.getOptions($el, "testpattern", {
34+
injectedOption: "injectedValue",
35+
});
36+
expect(options.option1).toBe("value1");
37+
expect(options.option2).toBe("value2");
38+
expect(options.injectedOption).toBe("injectedValue");
39+
});
40+
});

0 commit comments

Comments
 (0)