forked from javascript-tutorial/server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseAttrs.js
More file actions
executable file
·34 lines (26 loc) · 886 Bytes
/
parseAttrs.js
File metadata and controls
executable file
·34 lines (26 loc) · 886 Bytes
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
'use strict';
// 'my=5 test=3 bla="my "test"' -> my=5 test=3 bla="my " (test is not matched)
const attrsReg = /([\w-]+)(?:=(?:'((?:\\'|[^'])*)'|"((?:\\"|[^"])*)"|(\S+))|(?:\s|$))/g;
module.exports = function(attrs, withBlockName) {
const attrsObject = {};
if (!attrs) {
return attrsObject;
}
let blockName;
if (withBlockName) {
blockName = attrs.match(/^\w+/);
blockName = blockName && blockName[0];
attrs = attrs.replace(/^\w+\s+/, '');
}
let match, name, value;
while ((match = attrsReg.exec(attrs)) !== null) {
name = match[1];
value = match[2] !== undefined ? match[2].replace(/\\'/g, "'") :
match[3] !== undefined ? match[3].replace(/\\"/g, '"') : match[4];
attrsObject[name.toLowerCase()] = (value === undefined) ? true : value;
}
if (blockName) {
attrsObject.blockName = blockName;
}
return attrsObject;
};