Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 31 additions & 25 deletions src/core/mockup-parser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import utils from "./utils";

var parser = {
getOptions($el, patternName, options) {
const parser = {
getOptions(el, pattern_name, options) {
/* This is the Mockup parser. An alternative parser for Patternslib
* patterns.
*
Expand All @@ -9,37 +10,42 @@ var parser = {
*
* It parses a DOM element for pattern configuration options.
*/

el = utils.jqToNode(el);
options = options || {};
if (!el) {
return options;
}

// get options from parent element first, stop if element tag name is 'body'
if ($el.length !== 0 && $el[0].nodeName !== "BODY") {
options = this.getOptions($el.parent(), patternName, options);
if (el.nodeName !== "BODY") {
options = this.getOptions(el.parentElement, pattern_name, options);
}
// collect all options from element
let elOptions = {};
if ($el.length !== 0) {
elOptions = $el.data("pat-" + patternName);
if (elOptions) {
// parse options if string
if (typeof elOptions === "string") {
const tmpOptions = {};
elOptions.split(";").forEach(item => {
item = item.split(":");
item.reverse();
let key = item.pop();
key = key.replace(/^\s+|\s+$/g, ""); // trim
item.reverse();
let value = item.join(":");
value = value.replace(/^\s+|\s+$/g, ""); // trim
tmpOptions[key] = value;
});
elOptions = tmpOptions;
}
let el_options = {};
// Use `getAttribute` over `dataset` because dataset uses camelCasing of data attributes.
el_options = el.getAttribute(`data-pat-${pattern_name}`);
if (el_options) {
// parse options if string
if (typeof el_options === "string") {
const tmp_options = {};
el_options.split(";").forEach((item) => {
item = item.split(":");
item.reverse();
let key = item.pop();
key = key.replace(/^\s+|\s+$/g, ""); // trim
item.reverse();
let value = item.join(":");
value = value.replace(/^\s+|\s+$/g, ""); // trim
tmp_options[key] = value;
});
el_options = tmp_options;
}
}
return {
...options,
...elOptions,
}
...el_options,
};
},
};

Expand Down
8 changes: 4 additions & 4 deletions src/core/mockup-parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ describe("The mockup-parser", function () {
it("parses the data attribute of nested nodes", function () {
const $el = $(`
<div data-pat-testpattern="parentOption1: value1; parentOption2: value2">
<span data-pat-testpattern="option1: subvalue1; option2: subvalue2">
<span class="pat-testpattern" data-pat-testpattern="option1: subvalue1; option2: subvalue2">
nested mockup parser test
</span>
</div>`);
const options = mockupParser.getOptions($el, "testpattern");
const options = mockupParser.getOptions($(".pat-testpattern", $el), "testpattern");
expect(options.parentOption1).toBe("value1");
expect(options.parentOption2).toBe("value2");
expect(options.option1).toBe(undefined);
expect(options.option2).toBe(undefined);
expect(options.option1).toBe("subvalue1");
expect(options.option2).toBe("subvalue2");
});
it("parses the data attribute of a single node and preserves injected options", function () {
const $el = $(`
Expand Down
2 changes: 1 addition & 1 deletion src/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ const isIE = () => {

const jqToNode = (el) => {
// Return a DOM node if a jQuery node was passed.
if (el.jquery) {
if (el?.jquery) {
el = el[0];
}
return el;
Expand Down
7 changes: 7 additions & 0 deletions src/core/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,13 @@ describe("core.utils tests", () => {

done();
});

it("it returns the empty value, if no element was passed.", (done) => {
expect(utils.jqToNode(null)).toBe(null);
expect(utils.jqToNode(undefined)).toBe(undefined);

done();
});
});

describe("ensureArray tests", () => {
Expand Down