summaryrefslogtreecommitdiff
path: root/tests/html/simplify.tests.js
blob: ccc561026323c02b7cb4ecf410ccd8233097317c (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
var assert = require("assert");

var _ = require("underscore");

var test = require("../testing").test;
var html = require("../../lib/html");
var htmlPaths = require("../../lib/html-paths");

var nonFreshElement = html.nonFreshElement;
var text = html.text;
var pathToNodes = html.pathToNodes;

describe("simplify", function() {
    test("empty text nodes are removed", function() {
        assert.deepEqual(
            simplifyNode(text("")),
            []
        );
    });
    
    test("elements with no children are removed", function() {
        assert.deepEqual(
            simplifyNode(nonFreshElement("p", {}, [])),
            []
        );
    });
    
    test("elements only containing empty nodes are removed", function() {
        assert.deepEqual(
            simplifyNode(nonFreshElement("p", {}, [text("")])),
            []
        );
    });
    
    test("empty children of element are removed", function() {
        assert.deepEqual(
            simplifyNode(nonFreshElement("p", {}, [text("Hello"), text("")])),
            [nonFreshElement("p", {}, [text("Hello")])]
        );
    });
    
    test("successive fresh elements are not collapsed", function() {
        var path = htmlPaths.elements([
            htmlPaths.element("p", {}, {fresh: true})
        ]);
        var original = concat(
            pathToNodes(path, [text("Hello")]),
            pathToNodes(path, [text(" there")])
        );
        
        assert.deepEqual(
            html.simplify(original),
            original);
    });
    
    test("successive plain non-fresh elements are collapsed if they have the same tag name", function() {
        var path = htmlPaths.elements([
            htmlPaths.element("p", {}, {fresh: false})
        ]);
        assert.deepEqual(
            html.simplify(concat(
                pathToNodes(path, [text("Hello")]),
                pathToNodes(path, [text(" there")])
            )),
            pathToNodes(path, [text("Hello"), text(" there")])
        );
    });
    
    test("non-fresh can collapse into preceding fresh element", function() {
        var freshPath = htmlPaths.elements([
            htmlPaths.element("p", {}, {fresh: true})]);
        var nonFreshPath = htmlPaths.elements([
            htmlPaths.element("p", {}, {fresh: false})]);
        assert.deepEqual(
            html.simplify(concat(
                pathToNodes(freshPath, [text("Hello")]),
                pathToNodes(nonFreshPath, [text(" there")])
            )),
            pathToNodes(freshPath, [text("Hello"), text(" there")])
        );
    });
    
    test("children of collapsed element can collapse with children of another collapsed element", function() {
        assert.deepEqual(
            html.simplify([
                nonFreshElement("blockquote", {}, [nonFreshElement("p", {}, [text("Hello")])]),
                nonFreshElement("blockquote", {}, [nonFreshElement("p", {}, [text("there")])])
            ]),
            [nonFreshElement("blockquote", {}, [nonFreshElement("p", {}, [text("Hello"), text("there")])])]
        );
    });
    
    test("empty elements are removed before collapsing", function() {
        var freshPath = htmlPaths.elements([
            htmlPaths.element("p", {}, {fresh: true})]);
        var nonFreshPath = htmlPaths.elements([
            htmlPaths.element("p", {}, {fresh: false})]);
        assert.deepEqual(
            html.simplify(concat(
                pathToNodes(nonFreshPath, [text("Hello")]),
                pathToNodes(freshPath, []),
                pathToNodes(nonFreshPath, [text(" there")])
            )),
            pathToNodes(nonFreshPath, [text("Hello"), text(" there")])
        );
    });
});

function simplifyNode(node) {
    return html.simplify([node]);
}

function concat() {
    return _.flatten(arguments, true);
}