-
Notifications
You must be signed in to change notification settings - Fork 1
/
outputFormatterStyleStack.js
60 lines (45 loc) · 1.4 KB
/
outputFormatterStyleStack.js
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
56
57
58
59
60
const InvalidArgumentException = require('@ostro/support/exceptions/invalidArgumentException')
const OutputFormatterStyle = require('./outputFormatterStyle')
class OutputFormatterStyleStack {
constructor($emptyStyle = null) {
this.$emptyStyle = $emptyStyle || new OutputFormatterStyle();
this.$styles = [];
}
reset() {
this.$styles = [];
}
push($style) {
this.$styles.push($style);
}
pop($style = null) {
if (empty(this.$styles)) {
return this.$emptyStyle;
}
if (null === $style) {
return this.$styles.pop();
}
this.$styles = this.$styles.reverse()
for (let $i = 0; $i < this.$styles.length; $i++) {
let $stackedStyle = this.$styles[$i]
if ($style.apply('') === $stackedStyle.apply('')) {
this.$styles = this.$styles.slice(0, $i);
return $stackedStyle;
}
}
throw new InvalidArgumentException('Incorrectly nested style tag found.');
}
getCurrent() {
if (empty(this.$styles)) {
return this.$emptyStyle;
}
return this.$styles[this.$styles.length - 1];
}
setEmptyStyle($emptyStyle) {
this.$emptyStyle = $emptyStyle;
return this;
}
getEmptyStyle() {
return this.$emptyStyle;
}
}
module.exports = OutputFormatterStyleStack