Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmldomcompare.cpp
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant
4
6#include "QtCore/qglobal.h"
7
9
10namespace QQmlJS {
11namespace Dom {
12
13bool domCompare(const DomItem &i1, const DomItem &i2, function_ref<bool(Path, const DomItem &, const DomItem &)> change,
14 function_ref<bool(const DomItem &, const PathEls::PathComponent &, const DomItem &)> filter,
15 Path basePath)
16{
17 DomKind k1 = i1.domKind();
18 DomKind k2 = i2.domKind();
19 if (k1 != k2) {
20 if (!change(basePath, i1, i2))
21 return false;
22 } else {
23 switch (k1) {
24 case DomKind::Empty:
25 return true;
26 case DomKind::Object: {
27 QStringList f1 = i1.fields();
28 QStringList f2 = i2.fields();
29 f1.sort();
30 f2.sort();
31 qsizetype it1 = 0;
32 qsizetype it2 = 0;
33 while (it1 != f1.size() || it2 != f2.size()) {
34 QString k1, k2;
35 DomItem el1, el2;
36 bool hasK1 = it1 != f1.size();
37 bool filt1 = hasK1;
38 // fields() only yields Fields::* names
39 auto fieldOf = [](const QString &name) {
40 auto fn = fieldNameFromString(name);
41 Q_ASSERT(fn);
42 return *fn;
43 };
44 if (hasK1) {
45 k1 = f1.at(it1);
46 el1 = i1.field(fieldOf(k1));
47 filt1 = i1.isCanonicalChild(el1) && filter(i1, fieldOf(k1), el1);
48 }
49 bool hasK2 = it2 != f2.size();
50 bool filt2 = hasK2;
51 if (hasK2) {
52 k2 = f2.at(it2);
53 el2 = i2.field(fieldOf(k2));
54 filt2 = i2.isCanonicalChild(el2) && filter(i2, fieldOf(k2), el2);
55 }
56 // continue when filtering out
57 if (hasK1 && !filt1) {
58 ++it1;
59 if (hasK2 && !filt2)
60 ++it2;
61 continue;
62 } else if (hasK2 && !filt2) {
63 ++it2;
64 continue;
65 }
66 if (filt1 && filt2 && k1 == k2) {
67 if (!domCompare(el1, el2, change, filter, basePath.withField(fieldOf(k1))))
68 return false;
69 ++it1;
70 ++it2;
71 } else if (!hasK1 || (hasK2 && k1 > k2)) {
72 if (!change(basePath.withField(fieldOf(k1)), DomItem::empty, el2))
73 return false;
74 ++it2;
75 } else {
76 if (!change(basePath.withField(fieldOf(k1)), el1, DomItem::empty))
77 return false;
78 ++it1;
79 }
80 }
81 } break;
82 case DomKind::Map: {
83 QStringList f1 = i1.sortedKeys();
84 QStringList f2 = i2.sortedKeys();
85 qsizetype it1 = 0;
86 qsizetype it2 = 0;
87 while (it1 != f1.size() || it2 != f2.size()) {
88 QString k1, k2;
89 DomItem el1, el2;
90 bool hasK1 = it1 != f1.size();
91 bool filt1 = hasK1;
92 if (hasK1) {
93 k1 = f1.at(it1);
94 el1 = i1.key(k1);
95 filt1 = i1.isCanonicalChild(el1) && filter(i1, PathEls::Key(k1), el1);
96 }
97 bool hasK2 = it2 != f2.size();
98 bool filt2 = hasK2;
99 if (hasK2) {
100 k2 = f2.at(it2);
101 el2 = i2.key(k2);
102 filt2 = i2.isCanonicalChild(el2) && filter(i2, PathEls::Key(k2), el2);
103 }
104 // continue when filtering out
105 if (hasK1 && !filt1) {
106 ++it1;
107 if (hasK2 && !filt2)
108 ++it2;
109 continue;
110 } else if (hasK2 && !filt2) {
111 ++it2;
112 continue;
113 }
114 if (filt1 && filt2 && k1 == k2) {
115 if (!domCompare(el1, el2, change, filter, basePath.withKey(k1)))
116 return false;
117 ++it1;
118 ++it2;
119 } else if (!hasK1 || (hasK2 && k1 > k2)) {
120 if (!change(basePath.withKey(k1), DomItem::empty, el2))
121 return false;
122 ++it2;
123 } else {
124 if (!change(basePath.withKey(k1), el1, DomItem::empty))
125 return false;
126 ++it1;
127 }
128 }
129 } break;
130 case DomKind::List: {
131 // we could support smarter matching keeping filtering in account like map
132 // currently it is just a simple index by index comparison
133 index_type len1 = i1.indexes();
134 index_type len2 = i2.indexes();
135 if (len1 != len2)
136 return change(basePath, i1, i2);
137 for (index_type i = 0; i < len1; ++i) {
138 DomItem v1 = i1.index(i);
139 DomItem v2 = i2.index(i);
140 if (filter(i1, PathEls::Index(i), v1) && filter(i2, PathEls::Index(i), v2)) {
141 DomItem el1 = i1.index(i);
142 DomItem el2 = i2.index(i);
143 if (i1.isCanonicalChild(el1) && i2.isCanonicalChild(el2)
144 && !domCompare(el1, el2, change, filter, basePath.withIndex(i)))
145 return false;
146 }
147 }
148 } break;
149 case DomKind::Value: {
150 QCborValue v1 = i1.value();
151 QCborValue v2 = i2.value();
152 if (v1 != v2)
153 return change(basePath, i1, i2);
154 } break;
155 case DomKind::ScriptElement: {
156 // TODO: implement me
157 return false;
158
159 } break;
160 }
161 }
162 return true;
163}
164
166domCompareStrList(const DomItem &i1, const DomItem &i2,
167 function_ref<bool(const DomItem &, const PathEls::PathComponent &, const DomItem &) const> filter,
168 DomCompareStrList stopAtFirstDiff)
169{
170 QStringList res;
171 bool hasDiff = false;
172 domCompare(
173 i1, i2,
174 [&res, &hasDiff, stopAtFirstDiff](const Path &p, const DomItem &j1, const DomItem &j2) {
175 hasDiff = true;
176 if (!j1) {
177 res.append(QStringLiteral("- %1\n").arg(p.toString()));
178 } else if (!j2) {
179 res.append(QStringLiteral("+ %1\n").arg(p.toString()));
180 } else {
181 DomKind k1 = j1.domKind();
182 DomKind k2 = j2.domKind();
183 if (k1 != k2) {
184 res.append(
185 QStringLiteral("- %1 %2\n").arg(p.toString(), domKindToString(k1)));
186 res.append(
187 QStringLiteral("+ %1 %2\n").arg(p.toString(), domKindToString(k2)));
188 } else {
189 switch (k1) {
190 case DomKind::Empty:
191 case DomKind::Object:
192 case DomKind::Map:
193 Q_ASSERT(false);
194 break;
195 case DomKind::List: {
196 index_type len1 = j1.indexes();
197 index_type len2 = j2.indexes();
198 res.append(QStringLiteral("- %1 #%2\n").arg(p.toString()).arg(len1));
199 res.append(QStringLiteral("+ %1 #%2\n").arg(p.toString()).arg(len2));
200 } break;
201 case DomKind::Value: {
202 QCborValue v1 = j1.value();
203 QCborValue v2 = j2.value();
204 auto t1 = v1.type();
205 auto t2 = v2.type();
206 if (t1 != t2) {
207 res.append(QStringLiteral("- %1 type(%2)\n")
208 .arg(p.toString())
209 .arg(int(t1)));
210 res.append(QStringLiteral("+ %1 type(%2)\n")
211 .arg(p.toString())
212 .arg(int(t2)));
213 } else {
214 res.append(QStringLiteral("- %1 value(%2)\n")
215 .arg(p.toString())
216 .arg(j1.toString()));
217 res.append(QStringLiteral("+ %1 value(%2)\n")
218 .arg(p.toString())
219 .arg(j2.toString()));
220 }
221 } break;
222 case DomKind::ScriptElement: {
223 // implement me
224 break;
225 }
226 }
227 }
228 }
229 return (stopAtFirstDiff == DomCompareStrList::AllDiffs);
230 },
231 filter);
232 if (hasDiff && res.isEmpty()) // should never happen
233 res.append(QStringLiteral(u"Had changes!"));
234 return res;
235}
236
237} // end namespace Dom
238} // end namespace QQmlJS
239
240QT_END_NAMESPACE
A value type that references any element of the Dom.
DomItem field(FieldName name) const
bool isCanonicalChild(const DomItem &child) const
static DomItem empty
Represents an immutable JsonPath like path in the Qml code model (from a DomItem to another DomItem).
Path withField(FieldName name) const
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)
QStringList domCompareStrList(const DomItem &i1, const DomItem &i2, function_ref< bool(const DomItem &, const PathEls::PathComponent &, const DomItem &) const > filter, DomCompareStrList stopAtFirstDiff)
Combined button and popup list for selecting options.