aboutsummaryrefslogtreecommitdiff
path: root/lib/document-matchers.js
blob: 820d6c28a2d6d5c2d3123c1abaee24fc0b538996 (plain)
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
exports.paragraph = paragraph;
exports.run = run;


function paragraph(styleName) {
    return new Matcher("paragraph", styleName);
}

function run(styleName) {
    return new Matcher("run", styleName);
}

function Matcher(elementType, styleName) {
    this._elementType = elementType;
    this._styleName = styleName;
}

Matcher.prototype.matches = function(element) {
    return element.type === this._elementType &&
        (!this._styleName || element.styleName === this._styleName) &&
        (!this._listIndex || isList(element, this._listIndex, this._listIsOrdered));
}

Matcher.prototype.orderedList = function(levelIndex) {
    this._listIndex = levelIndex;
    this._listIsOrdered = true;
    return this;
}

Matcher.prototype.unorderedList = function(levelIndex) {
    this._listIndex = levelIndex;
    this._listIsOrdered = false;
    return this;
}

function isList(element, levelIndex, isOrdered) {
    return element.numbering &&
        element.numbering.level == levelIndex &&
        element.numbering.isOrdered == isOrdered;
}