Skip to content

Commit 2f1022d

Browse files
committed
1.20.0 addClass(), chainable setters, domPath(), setAttribute() fix
1 parent 153211e commit 2f1022d

20 files changed

+210
-72
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,13 @@ In terms of extendability, returning an object with named properties should be t
206206

207207
## Changelog
208208

209+
### 1.20.0
210+
211+
* Class FEATURE: addClass()
212+
* Class FEATURE: Chainable setters
213+
* Class BUG: domPath() must be external since clone doesn't rebuild self-references.
214+
* Class BUG: setAttribute() failed when no previous attributes
215+
209216
### 1.19.1
210217

211218
* Deepmerge is a runtime dependancy.
@@ -214,7 +221,7 @@ In terms of extendability, returning an object with named properties should be t
214221

215222
* Class syntax (semantic, path, style, build, clone, content, render)
216223
* Remove useless space in html style attribute after property colon.
217-
* BUG src/obj.es and src/svg.es wasn't transpiled in gradle.build.
224+
* BUG: src/obj.es and src/svg.es wasn't transpiled in gradle.build.
218225

219226
### 1.18.0
220227

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ projectName = render-js
33
appName = com.enonic.lib.render-js
44
displayName = Render JS
55
xpVersion = 6.12.2
6-
version = 1.19.1
6+
version = 1.20.0

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"eslint-plugin-react": "^7.5.1",
2626
"mocha": "^4.0.1",
2727
"postcss-js": "^1.0.1",
28+
"q-i": "^2.0.1",
2829
"sinon": "^4.1.3",
2930
"webpack": "^3.10.0"
3031
},
@@ -47,5 +48,5 @@
4748
"mocha": "node_modules/.bin/mocha -c --require babel-core/register",
4849
"test": "npm run mocha -- test/*.es* test/*/*.es*"
4950
},
50-
"version": "1.19.1"
51+
"version": "1.20.0"
5152
}

src/class.es

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import {element as importedElement} from './class/element.es';
88

99
export {build} from './class/build.es';
1010
export {clone} from './class/clone.es';
11+
export {addClass} from './class/addClass.es';
1112
export {addContent} from './class/addContent.es';
13+
export {domPath} from './class/domPath.es';
1214
export const element = importedElement;
1315

1416
export {getAttribute} from './class/getAttribute.es';

src/class/addClass.es

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* eslint-disable import/prefer-default-export */
2+
/* eslint-disable no-param-reassign */
3+
4+
5+
import {PROP_ATTR} from './element.es';
6+
import {isArray} from '../util/isArray.es';
7+
import {sortAndRemoveDups} from '../util/sortAndRemoveDups.es';
8+
9+
10+
export function addClass(element, classes) {
11+
if (!element[PROP_ATTR]) { element[PROP_ATTR] = {}; }
12+
if (!element[PROP_ATTR].class) {
13+
element[PROP_ATTR].class = [];
14+
} else if (!isArray(element[PROP_ATTR].class)) {
15+
element[PROP_ATTR].class = [element[PROP_ATTR].class];
16+
}
17+
let arr = [];
18+
if (isArray(classes)) {
19+
classes.forEach(c => {
20+
arr = arr.concat(c.split(' '));
21+
});
22+
} else {
23+
arr = arr.concat(classes.split(' '));
24+
}
25+
element[PROP_ATTR].class = sortAndRemoveDups(element[PROP_ATTR].class.concat(arr));
26+
return element; // Chainable
27+
}

src/class/addContent.es

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {isArray} from '../util/isArray.es';
44
import {PROP_CONTENT} from './element.es';
55

66

7-
// TODO applyPath
87
export function addContent(element, content) {
98
if (isArray(element[PROP_CONTENT])) {
109
element[PROP_CONTENT].push(content);
@@ -14,4 +13,5 @@ export function addContent(element, content) {
1413
} else {
1514
element[PROP_CONTENT] = content; // eslint-disable-line no-param-reassign
1615
}
16+
return element; // Chainable
1717
} // export function addContent

src/class/domPath.es

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* eslint-disable import/prefer-default-export */
2+
3+
//import {print as inspect} from 'q-i';
4+
import {PROP_CONTENT, PROP_TAG} from './element.es';
5+
import {isArray} from '../util/isArray.es';
6+
7+
8+
export function domPath(element, path) {
9+
//inspect({domPath: {element, path}});
10+
//inspect({path});
11+
if (!element[PROP_CONTENT]) { return null; }
12+
13+
const contentArr = isArray(element[PROP_CONTENT]) ?
14+
element[PROP_CONTENT] : [element[PROP_CONTENT]];
15+
16+
const pathObj = {};
17+
for (let i = 0; i < contentArr.length; i += 1) {
18+
const item = contentArr[i];
19+
const childTag = item[PROP_TAG];
20+
if (isArray(pathObj[childTag])) {
21+
pathObj[childTag].push(item); // reference
22+
} else if (pathObj[childTag]) {
23+
pathObj[childTag] = [pathObj[childTag], item]; // reference
24+
} else {
25+
pathObj[childTag] = item; // reference
26+
}
27+
} // for
28+
//inspect({pathObj});
29+
30+
const parts = path.split('.');
31+
const currentPart = parts.shift(); //inspect({currentPart});
32+
const brackets = currentPart.split('['); //inspect({brackets});
33+
let child;
34+
if (brackets.length > 1) {
35+
//child = eval(`pathObj.${currentPart}`); // NOTE eval can be harmful, so this avoids it:
36+
const a = brackets[0]; //inspect({a});
37+
const b = brackets[1].substring(0, brackets[1].length - 1); //inspect({b});
38+
child = pathObj[a][b];
39+
} else {
40+
child = pathObj[currentPart];
41+
}
42+
//inspect({child});
43+
if (!child) { return null; }
44+
if (!parts.length) { return child; } // reference
45+
return domPath(child, parts.join('.')); // recurse
46+
} // export function domPath

src/class/element.es

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,6 @@ const PROP_BODY_BEGIN = '_bb';
2424
const PROP_BODY_END = '_be';*/
2525

2626

27-
function applyPath(obj) {
28-
const contentArr = isArray(obj[PROP_CONTENT]) ? obj[PROP_CONTENT] : [obj[PROP_CONTENT]];
29-
for (let i = 0; i < contentArr.length; i += 1) {
30-
const item = contentArr[i];
31-
//if (isString(item)) { // TODO Parse string into Element? }
32-
if (item && item[PROP_TAG]) {
33-
const childTag = item[PROP_TAG];
34-
if (isArray(obj[childTag])) {
35-
obj[childTag].push(item);
36-
} else if (obj[childTag]) {
37-
obj[childTag] = [obj[childTag], item]; // eslint-disable-line no-param-reassign
38-
} else {
39-
obj[childTag] = item; // eslint-disable-line no-param-reassign
40-
}
41-
} // if Element
42-
} // for
43-
} // function applyPath
44-
45-
4627
export function Element({
4728
tagName = DEFAULT_TAGNAME,
4829
spec = null,
@@ -70,10 +51,7 @@ export function Element({
7051
}
7152
if (!isEmptyObject(attributes)) { obj[PROP_ATTR] = attributes; }
7253
} // if spec
73-
if (content) {
74-
obj[PROP_CONTENT] = content;
75-
applyPath(obj);
76-
} // if content
54+
if (content) { obj[PROP_CONTENT] = content; } // if content
7755
/*
7856
if (headBegin ) { obj[PROP_HEAD_BEGIN] = headBegin; }
7957
if (headEnd ) { obj[PROP_HEAD_END ] = headEnd ; }

src/class/setAttribute.es

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,12 @@ import {PROP_ATTR} from './element.es';
55

66

77
export function setAttribute(element, name, value) {
8-
element[PROP_ATTR][name] = value; // eslint-disable-line no-param-reassign
8+
if (element[PROP_ATTR]) {
9+
element[PROP_ATTR][name] = value; // eslint-disable-line no-param-reassign
10+
} else {
11+
element[PROP_ATTR] = { // eslint-disable-line no-param-reassign
12+
[name]: value
13+
};
14+
}
15+
return element; // Chainable
916
}

src/class/setAttributes.es

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ import {PROP_ATTR} from './element.es';
88
export function setAttributes(element, attributes) {
99
// eslint-disable-next-line no-param-reassign
1010
element[PROP_ATTR] = merge(element[PROP_ATTR], attributes);
11+
return element; // Chainable
1112
}

0 commit comments

Comments
 (0)