-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathmockup-parser.js
More file actions
55 lines (51 loc) · 1.87 KB
/
mockup-parser.js
File metadata and controls
55 lines (51 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import utils from "./utils";
const parser = {
getOptions(el, pattern_name, options) {
/* This is the Mockup parser. An alternative parser for Patternslib
* patterns.
*
* NOTE: Use of the Mockup parser is discouraged and is added here for
* legacy support for the Plone Mockup project.
*
* 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.nodeName !== "BODY") {
options = this.getOptions(el.parentElement, pattern_name, options);
}
// collect all options from element
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
try {
el_options = JSON.parse(el_options);
} catch {
// not JSON, try to parse it as semi-colon/colon list
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,
...el_options,
};
},
};
export default parser;