aboutsummaryrefslogtreecommitdiffstats
path: root/src/qmldom/qqmldomcompare.cpp
blob: 65b357cc751cc3d4b0250b00b29cf9f1cbd5f5bd (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qqmldomcompare_p.h"
#include "QtCore/qglobal.h"

QT_BEGIN_NAMESPACE

namespace QQmlJS {
namespace Dom {

bool domCompare(const DomItem &i1, const DomItem &i2, function_ref<bool(Path, const DomItem &, const DomItem &)> change,
                function_ref<bool(const DomItem &, const PathEls::PathComponent &, const DomItem &)> filter,
                Path basePath)
{
    DomKind k1 = i1.domKind();
    DomKind k2 = i2.domKind();
    if (k1 != k2) {
        if (!change(basePath, i1, i2))
            return false;
    } else {
        switch (k1) {
        case DomKind::Empty:
            return true;
        case DomKind::Object: {
            QStringList f1 = i1.fields();
            QStringList f2 = i2.fields();
            f1.sort();
            f2.sort();
            qsizetype it1 = 0;
            qsizetype it2 = 0;
            while (it1 != f1.size() || it2 != f2.size()) {
                QString k1, k2;
                DomItem el1, el2;
                bool hasK1 = it1 != f1.size();
                bool filt1 = hasK1;
                if (hasK1) {
                    k1 = f1.at(it1);
                    el1 = i1.field(k1);
                    filt1 = i1.isCanonicalChild(el1) && filter(i1, PathEls::Field(k1), el1);
                }
                bool hasK2 = it2 != f2.size();
                bool filt2 = hasK2;
                if (hasK2) {
                    k2 = f2.at(it2);
                    el2 = i2.field(k2);
                    filt2 = i2.isCanonicalChild(el2) && filter(i2, PathEls::Field(k2), el2);
                }
                // continue when filtering out
                if (hasK1 && !filt1) {
                    ++it1;
                    if (hasK2 && !filt2)
                        ++it2;
                    continue;
                } else if (hasK2 && !filt2) {
                    ++it2;
                    continue;
                }
                if (filt1 && filt2 && k1 == k2) {
                    if (!domCompare(el1, el2, change, filter, basePath.field(k1)))
                        return false;
                    ++it1;
                    ++it2;
                } else if (!hasK1 || (hasK2 && k1 > k2)) {
                    if (!change(basePath.field(k1), DomItem::empty, el2))
                        return false;
                    ++it2;
                } else {
                    if (!change(basePath.field(k1), el1, DomItem::empty))
                        return false;
                    ++it1;
                }
            }
        } break;
        case DomKind::Map: {
            QStringList f1 = i1.sortedKeys();
            QStringList f2 = i2.sortedKeys();
            qsizetype it1 = 0;
            qsizetype it2 = 0;
            while (it1 != f1.size() || it2 != f2.size()) {
                QString k1, k2;
                DomItem el1, el2;
                bool hasK1 = it1 != f1.size();
                bool filt1 = hasK1;
                if (hasK1) {
                    k1 = f1.at(it1);
                    el1 = i1.key(k1);
                    filt1 = i1.isCanonicalChild(el1) && filter(i1, PathEls::Key(k1), el1);
                }
                bool hasK2 = it2 != f2.size();
                bool filt2 = hasK2;
                if (hasK2) {
                    k2 = f2.at(it2);
                    el2 = i2.key(k2);
                    filt2 = i2.isCanonicalChild(el2) && filter(i2, PathEls::Key(k2), el2);
                }
                // continue when filtering out
                if (hasK1 && !filt1) {
                    ++it1;
                    if (hasK2 && !filt2)
                        ++it2;
                    continue;
                } else if (hasK2 && !filt2) {
                    ++it2;
                    continue;
                }
                if (filt1 && filt2 && k1 == k2) {
                    if (!domCompare(el1, el2, change, filter, basePath.key(k1)))
                        return false;
                    ++it1;
                    ++it2;
                } else if (!hasK1 || (hasK2 && k1 > k2)) {
                    if (!change(basePath.key(k1), DomItem::empty, el2))
                        return false;
                    ++it2;
                } else {
                    if (!change(basePath.key(k1), el1, DomItem::empty))
                        return false;
                    ++it1;
                }
            }
        } break;
        case DomKind::List: {
            // we could support smarter matching keeping filtering in account like map
            // currently it is just a simple index by index comparison
            index_type len1 = i1.indexes();
            index_type len2 = i2.indexes();
            if (len1 != len2)
                return change(basePath, i1, i2);
            for (index_type i = 0; i < len1; ++i) {
                DomItem v1 = i1.index(i);
                DomItem v2 = i2.index(i);
                if (filter(i1, PathEls::Index(i), v1) && filter(i2, PathEls::Index(i), v2)) {
                    DomItem el1 = i1.index(i);
                    DomItem el2 = i2.index(i);
                    if (i1.isCanonicalChild(el1) && i2.isCanonicalChild(el2)
                        && !domCompare(el1, el2, change, filter, basePath.index(i)))
                        return false;
                }
            }
        } break;
        case DomKind::Value: {
            QCborValue v1 = i1.value();
            QCborValue v2 = i2.value();
            if (v1 != v2)
                return change(basePath, i1, i2);
        } break;
        case DomKind::ScriptElement: {
            // TODO: implement me
            return false;

        } break;
        }
    }
    return true;
}

QStringList
domCompareStrList(const DomItem &i1, const DomItem &i2,
                  function_ref<bool(const DomItem &, const PathEls::PathComponent &, const DomItem &) const> filter,
                  DomCompareStrList stopAtFirstDiff)
{
    QStringList res;
    bool hasDiff = false;
    domCompare(
            i1, i2,
            [&res, &hasDiff, stopAtFirstDiff](const Path &p, const DomItem &j1, const DomItem &j2) {
                hasDiff = true;
                if (!j1) {
                    res.append(QStringLiteral("- %1\n").arg(p.toString()));
                } else if (!j2) {
                    res.append(QStringLiteral("+ %1\n").arg(p.toString()));
                } else {
                    DomKind k1 = j1.domKind();
                    DomKind k2 = j2.domKind();
                    if (k1 != k2) {
                        res.append(
                                QStringLiteral("- %1 %2\n").arg(p.toString(), domKindToString(k1)));
                        res.append(
                                QStringLiteral("+ %1 %2\n").arg(p.toString(), domKindToString(k2)));
                    } else {
                        switch (k1) {
                        case DomKind::Empty:
                        case DomKind::Object:
                        case DomKind::Map:
                            Q_ASSERT(false);
                            break;
                        case DomKind::List: {
                            index_type len1 = j1.indexes();
                            index_type len2 = j2.indexes();
                            res.append(QStringLiteral("- %1 #%2\n").arg(p.toString()).arg(len1));
                            res.append(QStringLiteral("+ %1 #%2\n").arg(p.toString()).arg(len2));
                        } break;
                        case DomKind::Value: {
                            QCborValue v1 = j1.value();
                            QCborValue v2 = j2.value();
                            auto t1 = v1.type();
                            auto t2 = v2.type();
                            if (t1 != t2) {
                                res.append(QStringLiteral("- %1 type(%2)\n")
                                                   .arg(p.toString())
                                                   .arg(int(t1)));
                                res.append(QStringLiteral("+ %1 type(%2)\n")
                                                   .arg(p.toString())
                                                   .arg(int(t2)));
                            } else {
                                res.append(QStringLiteral("- %1 value(%2)\n")
                                                   .arg(p.toString())
                                                   .arg(j1.toString()));
                                res.append(QStringLiteral("+ %1 value(%2)\n")
                                                   .arg(p.toString())
                                                   .arg(j2.toString()));
                            }
                        } break;
                        case DomKind::ScriptElement: {
                            // implement me
                            break;
                        }
                        }
                    }
                }
                return (stopAtFirstDiff == DomCompareStrList::AllDiffs);
            },
            filter);
    if (hasDiff && res.isEmpty()) // should never happen
        res.append(QStringLiteral(u"Had changes!"));
    return res;
}

} // end namespace Dom
} // end namespace QQmlJS

QT_END_NAMESPACE