Skip to content

Commit f263c93

Browse files
committed
Add basic parsing logic for html markup
1 parent 466227b commit f263c93

File tree

5 files changed

+158
-6
lines changed

5 files changed

+158
-6
lines changed

.karma.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
browsers = [
2+
"Chrome"
3+
// "PhantomJS"
4+
];
5+
16
module.exports = function(config) {
27
config.set({
38
basePath: '',
4-
browsers: [
5-
"PhantomJS"
6-
],
9+
browsers: browsers,
710
frameworks: [
811
'browserify',
912
'jasmine'

src/bubble.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,16 @@ export default class Bubble {
77
end: end
88
};
99
}
10+
11+
Label() {
12+
return this.label;
13+
}
14+
15+
Start() {
16+
return this.date.start;
17+
}
18+
19+
End() {
20+
return this.date.end;
21+
}
1022
}

src/bubble.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ describe('Bubble', function () {
44
const b = new Bubble(1, 2, "label");
55

66
it('should have a start and end value', () => {
7-
expect(b.date.start).toEqual(1);
8-
expect(b.date.end).toEqual(2);
7+
expect(b.Start()).toEqual(1);
8+
expect(b.End()).toEqual(2);
99
});
1010

1111
it("should have a label", () => {
12-
expect(b.label).toEqual("label")
12+
expect(b.Label()).toEqual("label")
1313
})
1414
});

src/parser.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import Bubble from './bubble.js'
2+
3+
export default class Parser {
4+
constructor() {
5+
6+
}
7+
8+
parse(list) {
9+
if (!list.classList.contains('timesheet')) {
10+
return [];
11+
}
12+
13+
var items = list.querySelectorAll('.timesheet-item');
14+
if (items.length === 0) {
15+
return [];
16+
}
17+
18+
var data = []
19+
for (var i = 0, m = items.length; i < m; i++) {
20+
var item = items[i];
21+
22+
var dateStart = item.querySelector('.timesheet-item--date-start')
23+
, dateEnd = item.querySelector('.timesheet-item--date-end')
24+
, label = item.querySelector('.timesheet-item--label');
25+
26+
data.push(new Bubble(
27+
dateStart ? dateStart.innerHTML : null,
28+
dateEnd ? dateEnd.innerHTML : null,
29+
label ? label.innerHTML : null
30+
))
31+
}
32+
33+
return data;
34+
}
35+
}

src/parser.spec.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import Parser from '../src/parser.js';
2+
3+
var toDOM = function(text) {
4+
var div = document.createElement('div');
5+
div.innerHTML = text;
6+
7+
return div.childNodes[1];
8+
}
9+
10+
describe('Parser', function () {
11+
const p = new Parser();
12+
13+
it('should return an empty array for empty lists', () => {
14+
var input = toDOM(`
15+
<ul>
16+
</ul>
17+
`);
18+
19+
expect(p.parse(input)).toEqual([]);
20+
});
21+
22+
it('should return an empty array for lists without timesheet class', () => {
23+
var input = toDOM(`
24+
<ul>
25+
<li>example</li>
26+
</ul>
27+
`);
28+
29+
expect(p.parse(input)).toEqual([]);
30+
});
31+
32+
it('should return an empty array for lists with timesheet class but without timesheet-item elements', () => {
33+
var input = toDOM(`
34+
<ul class="timesheet">
35+
<li>example</li>
36+
</ul>
37+
`);
38+
39+
expect(p.parse(input)).toEqual([]);
40+
});
41+
42+
it('should return an array for lists with timesheet class and timesheet-item elements', () => {
43+
var input = toDOM(`
44+
<ul class="timesheet">
45+
<li class="timesheet-item">
46+
<span class="timesheet-item--date-start">2015-01-01</span>
47+
<span class="timesheet-item--date-end">2015-01-12</span>
48+
<span class="timesheet-item--label">example</span>
49+
</li>
50+
</ul>
51+
`);
52+
53+
expect(p.parse(input).length).toEqual(1);
54+
});
55+
56+
it('should return an array of bubbles for lists with timesheet class and timesheet-item elements', () => {
57+
var input = toDOM(`
58+
<ul class="timesheet">
59+
<li class="timesheet-item">
60+
<span class="timesheet-item--date-start">2015-01-01</span>
61+
<span class="timesheet-item--date-end">2015-01-12</span>
62+
<span class="timesheet-item--label">example-0</span>
63+
</li>
64+
<li class="timesheet-item">
65+
<span class="timesheet-item--date-end">2015-02-12</span>
66+
<span class="timesheet-item--label">example-1</span>
67+
</li>
68+
<li class="timesheet-item">
69+
<span class="timesheet-item--date-start">2015-03-01</span>
70+
<span class="timesheet-item--label">example-2</span>
71+
</li>
72+
<li class="timesheet-item">
73+
<span class="timesheet-item--date-start">2015-04-01</span>
74+
<span class="timesheet-item--date-end">2015-04-12</span>
75+
</li>
76+
</ul>
77+
`);
78+
79+
var output = p.parse(input);
80+
expect(output.length).toEqual(4);
81+
82+
var item = output.shift();
83+
expect(item.Start()).toEqual('2015-01-01')
84+
expect(item.End()).toEqual('2015-01-12')
85+
expect(item.Label()).toEqual('example-0')
86+
87+
var item = output.shift();
88+
expect(item.Start()).toEqual(null)
89+
expect(item.End()).toEqual('2015-02-12')
90+
expect(item.Label()).toEqual("example-1")
91+
92+
var item = output.shift();
93+
expect(item.Start()).toEqual("2015-03-01")
94+
expect(item.End()).toEqual(null)
95+
expect(item.Label()).toEqual("example-2")
96+
97+
var item = output.shift();
98+
expect(item.Start()).toEqual("2015-04-01")
99+
expect(item.End()).toEqual("2015-04-12")
100+
expect(item.Label()).toEqual(null)
101+
});
102+
});

0 commit comments

Comments
 (0)