Skip to content

Commit 8e18798

Browse files
author
Oskar Eriksson
committed
Implemented classList and className
1 parent 17a42dc commit 8e18798

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

src/worker/dom/ClassList.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class ClassList {
2+
constructor(domElement) {
3+
this._domElement = domElement;
4+
}
5+
add(value) {
6+
const classList = this._getCurrentClassList()
7+
const newClassNames = (typeof value === 'string') ? [value] : value;
8+
classList.push(...newClassNames);
9+
this._domElement.setAttribute('class', classList.join(' '));
10+
}
11+
remove(value) {
12+
const classList = this._getCurrentClassList();
13+
const classNames = (typeof value === 'string') ? [value] : value;
14+
classNames.forEach((className) => {
15+
const index = classList.indexOf(value);
16+
if (index >= 0) {
17+
classList.splice(index, 1);
18+
}
19+
})
20+
this._domElement.setAttribute('class', classList.join(' '));
21+
}
22+
item(index) {
23+
const classList = this._getCurrentClassList();
24+
return classList[index];
25+
}
26+
contains(value) {
27+
const classList = this._getCurrentClassList();
28+
return classList.includes(value);
29+
}
30+
toggle(value) {
31+
console.log('TODO: Implement classList.toggle()')
32+
}
33+
_getCurrentClassList() {
34+
if (this._domElement.attributes['class']) {
35+
return this._domElement.attributes['class'].split(' ');
36+
}
37+
return [];
38+
}
39+
}
40+
41+
export default ClassList;

src/worker/dom/DomElement.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import TreeNode from './TreeNode';
22
import Style from './Style';
3+
import ClassList from './ClassList';
34
import {OPS as _} from './../../common/constants';
45
import {ELEMENT_NODE} from './../../common/nodeType'
56

@@ -14,6 +15,7 @@ class DomElement extends TreeNode {
1415
this._bridge.send(_.createDOMElement, this._guid, [type]);
1516

1617
this.style = Style((key, val) => this._bridge.send(_.setStyle, this._guid, [key, val]));
18+
this.classList = new ClassList(this);
1719

1820
inputAttributes.forEach(prop => {
1921
Object
@@ -30,7 +32,7 @@ class DomElement extends TreeNode {
3032
}
3133

3234
removeEventListener(eventType, callback, useCapture) {
33-
console.log('// TODO Remove event listener')
35+
// console.log('// TODO Remove event listener')
3436
}
3537

3638
set textContent(val) {
@@ -41,6 +43,14 @@ class DomElement extends TreeNode {
4143
get textContent() {
4244
return this._textContent;
4345
}
46+
47+
set className(value) {
48+
this.setAttribute('class', value);
49+
}
50+
51+
get className() {
52+
return this.attributes['class'];
53+
}
4454
}
4555

4656
const inputAttributes = ["accept", "align", "alt", "autocomplete ", "autofocus ", "checked", "disabled", "form", "formaction ", "formenctype", "formmethod ", "formnovalidate ", "formtarget ", "height ", "list ", "max ", "maxlength", "min ", "multiple ", "name", "pattern ", "placeholder", "readonly", "required ", "size", "src", "step", "type", "value", "width"];
@@ -58,4 +68,4 @@ export default (tag) => {
5868
}));
5969
Object.defineProperties(element, props);
6070
return element;
61-
}
71+
}

0 commit comments

Comments
 (0)