clapton 0.0.13 → 0.0.14
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -0
- data/app/helpers/clapton/clapton_helper.rb +16 -1
- data/lib/clapton/engine.rb +14 -10
- data/lib/clapton/javascripts/dist/client.js +31 -19
- data/lib/clapton/javascripts/dist/components-for-test.js +439 -0
- data/lib/clapton/javascripts/dist/components.js +356 -382
- data/lib/clapton/javascripts/node_modules/diff-dom/LICENSE.txt +165 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/README.md +224 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/browser/diffDOM.js +2 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/browser/diffDOM.js.map +1 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/dist/dts/TraceLogger.d.ts +28 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/dist/dts/diffDOM/dom/apply.d.ts +4 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/dist/dts/diffDOM/dom/fromVirtual.d.ts +2 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/dist/dts/diffDOM/dom/index.d.ts +2 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/dist/dts/diffDOM/dom/undo.d.ts +3 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/dist/dts/diffDOM/helpers.d.ts +11 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/dist/dts/diffDOM/index.d.ts +10 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/dist/dts/diffDOM/types.d.ts +104 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/dist/dts/diffDOM/virtual/apply.d.ts +3 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/dist/dts/diffDOM/virtual/diff.d.ts +22 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/dist/dts/diffDOM/virtual/fromDOM.d.ts +2 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/dist/dts/diffDOM/virtual/fromString.d.ts +2 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/dist/dts/diffDOM/virtual/helpers.d.ts +40 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/dist/dts/diffDOM/virtual/index.d.ts +3 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/dist/dts/index.d.ts +2 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/dist/index.d.ts +136 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/dist/index.js +1996 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/dist/index.js.map +1 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/dist/index.min.js +2 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/dist/index.min.js.map +1 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/dist/module.js +1991 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/dist/module.js.map +1 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/index.html +62 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/package.json +54 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/rollup.config.mjs +67 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/src/TraceLogger.ts +143 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/src/diffDOM/dom/apply.ts +227 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/src/diffDOM/dom/fromVirtual.ts +83 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/src/diffDOM/dom/index.ts +2 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/src/diffDOM/dom/undo.ts +90 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/src/diffDOM/helpers.ts +40 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/src/diffDOM/index.ts +121 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/src/diffDOM/types.ts +154 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/src/diffDOM/virtual/apply.ts +349 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/src/diffDOM/virtual/diff.ts +855 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/src/diffDOM/virtual/fromDOM.ts +74 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/src/diffDOM/virtual/fromString.ts +239 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/src/diffDOM/virtual/helpers.ts +461 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/src/diffDOM/virtual/index.ts +3 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/src/index.ts +2 -0
- data/lib/clapton/javascripts/node_modules/diff-dom/tsconfig.json +103 -0
- data/lib/clapton/javascripts/rollup.config.mjs +17 -2
- data/lib/clapton/javascripts/src/actions/initialize-actions.ts +6 -3
- data/lib/clapton/javascripts/src/channel/clapton-channel.js +6 -3
- data/lib/clapton/javascripts/src/client.ts +15 -15
- data/lib/clapton/javascripts/src/components-for-test.ts +29 -0
- data/lib/clapton/javascripts/src/components.ts +4 -1
- data/lib/clapton/javascripts/src/dom/update-component.ts +3 -2
- data/lib/clapton/javascripts/src/inputs/initialize-inputs.ts +2 -2
- data/lib/clapton/test_helper/base.rb +1 -1
- data/lib/clapton/version.rb +1 -1
- metadata +49 -3
- data/lib/clapton/javascripts/src/dom/update-component.spec.ts +0 -32
@@ -0,0 +1,165 @@
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
2
|
+
Version 3, 29 June 2007
|
3
|
+
|
4
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
6
|
+
of this license document, but changing it is not allowed.
|
7
|
+
|
8
|
+
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
11
|
+
License, supplemented by the additional permissions listed below.
|
12
|
+
|
13
|
+
0. Additional Definitions.
|
14
|
+
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
17
|
+
General Public License.
|
18
|
+
|
19
|
+
"The Library" refers to a covered work governed by this License,
|
20
|
+
other than an Application or a Combined Work as defined below.
|
21
|
+
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
25
|
+
of using an interface provided by the Library.
|
26
|
+
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
28
|
+
Application with the Library. The particular version of the Library
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
30
|
+
Version".
|
31
|
+
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
35
|
+
based on the Application, and not on the Linked Version.
|
36
|
+
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
38
|
+
object code and/or source code for the Application, including any data
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
41
|
+
|
42
|
+
1. Exception to Section 3 of the GNU GPL.
|
43
|
+
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
46
|
+
|
47
|
+
2. Conveying Modified Versions.
|
48
|
+
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
51
|
+
that uses the facility (other than as an argument passed when the
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
53
|
+
version:
|
54
|
+
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
56
|
+
ensure that, in the event an Application does not supply the
|
57
|
+
function or data, the facility still operates, and performs
|
58
|
+
whatever part of its purpose remains meaningful, or
|
59
|
+
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
61
|
+
this License applicable to that copy.
|
62
|
+
|
63
|
+
3. Object Code Incorporating Material from Library Header Files.
|
64
|
+
|
65
|
+
The object code form of an Application may incorporate material from
|
66
|
+
a header file that is part of the Library. You may convey such object
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
68
|
+
material is not limited to numerical parameters, data structure
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
71
|
+
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
73
|
+
Library is used in it and that the Library and its use are
|
74
|
+
covered by this License.
|
75
|
+
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
77
|
+
document.
|
78
|
+
|
79
|
+
4. Combined Works.
|
80
|
+
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
82
|
+
taken together, effectively do not restrict modification of the
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
85
|
+
the following:
|
86
|
+
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
88
|
+
the Library is used in it and that the Library and its use are
|
89
|
+
covered by this License.
|
90
|
+
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
92
|
+
document.
|
93
|
+
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
95
|
+
execution, include the copyright notice for the Library among
|
96
|
+
these notices, as well as a reference directing the user to the
|
97
|
+
copies of the GNU GPL and this license document.
|
98
|
+
|
99
|
+
d) Do one of the following:
|
100
|
+
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
102
|
+
License, and the Corresponding Application Code in a form
|
103
|
+
suitable for, and under terms that permit, the user to
|
104
|
+
recombine or relink the Application with a modified version of
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
107
|
+
Corresponding Source.
|
108
|
+
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
111
|
+
a copy of the Library already present on the user's computer
|
112
|
+
system, and (b) will operate properly with a modified version
|
113
|
+
of the Library that is interface-compatible with the Linked
|
114
|
+
Version.
|
115
|
+
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
117
|
+
be required to provide such information under section 6 of the
|
118
|
+
GNU GPL, and only to the extent that such information is
|
119
|
+
necessary to install and execute a modified version of the
|
120
|
+
Combined Work produced by recombining or relinking the
|
121
|
+
Application with a modified version of the Linked Version. (If
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
126
|
+
for conveying Corresponding Source.)
|
127
|
+
|
128
|
+
5. Combined Libraries.
|
129
|
+
|
130
|
+
You may place library facilities that are a work based on the
|
131
|
+
Library side by side in a single library together with other library
|
132
|
+
facilities that are not Applications and are not covered by this
|
133
|
+
License, and convey such a combined library under terms of your
|
134
|
+
choice, if you do both of the following:
|
135
|
+
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
137
|
+
on the Library, uncombined with any other library facilities,
|
138
|
+
conveyed under the terms of this License.
|
139
|
+
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
141
|
+
is a work based on the Library, and explaining where to find the
|
142
|
+
accompanying uncombined form of the same work.
|
143
|
+
|
144
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
145
|
+
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
148
|
+
versions will be similar in spirit to the present version, but may
|
149
|
+
differ in detail to address new problems or concerns.
|
150
|
+
|
151
|
+
Each version is given a distinguishing version number. If the
|
152
|
+
Library as you received it specifies that a certain numbered version
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
154
|
+
applies to it, you have the option of following the terms and
|
155
|
+
conditions either of that published version or of any later version
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
160
|
+
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
164
|
+
permanent authorization for you to choose that version for the
|
165
|
+
Library.
|
@@ -0,0 +1,224 @@
|
|
1
|
+
# diffDOM - A JavaScript diffing algorithm for DOM elements
|
2
|
+
|
3
|
+
This library allows the abstraction of differences between DOM
|
4
|
+
elements as a "diff" object, representing the sequence of modifications
|
5
|
+
that must be applied to one element in order to turn it into the other
|
6
|
+
element. This diff is non-destructive, meaning that relocations of
|
7
|
+
DOM nodes are preferred over remove-insert operations.
|
8
|
+
|
9
|
+
## License
|
10
|
+
|
11
|
+
This project is licensed under the LGPL v. 3. For details see LICENSE.txt.
|
12
|
+
|
13
|
+
## Demo and tests
|
14
|
+
|
15
|
+
Check http://fiduswriter.github.io/diffDOM for demo and tests.
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
Include the diffDOM file in your HTML like this:
|
20
|
+
|
21
|
+
```html
|
22
|
+
<script src="browser/diffDOM.js"></script>
|
23
|
+
```
|
24
|
+
|
25
|
+
Or like this if you import from npm:
|
26
|
+
|
27
|
+
```js
|
28
|
+
import { DiffDOM } from "diff-dom"
|
29
|
+
```
|
30
|
+
|
31
|
+
Then create an instance of diffDOM within the javascript code:
|
32
|
+
|
33
|
+
```js
|
34
|
+
dd = new diffDOM.DiffDOM()
|
35
|
+
```
|
36
|
+
|
37
|
+
(leave out the `diffdom.` if you use the npm-version)
|
38
|
+
|
39
|
+
Now you can create a diff to get from dom `elementA` to dom `elementB` like this:
|
40
|
+
|
41
|
+
```js
|
42
|
+
diff = dd.diff(elementA, elementB)
|
43
|
+
```
|
44
|
+
|
45
|
+
You can now apply this diff like this:
|
46
|
+
|
47
|
+
```js
|
48
|
+
dd.apply(elementA, diff)
|
49
|
+
```
|
50
|
+
|
51
|
+
Now `elementA` will have been changed to be structurally equal to `elementB`.
|
52
|
+
|
53
|
+
### Virtual DOM and HTML strings
|
54
|
+
|
55
|
+
You can also use HTML strings or the virtual DOM objects diffDOM uses internally to create diffs.
|
56
|
+
|
57
|
+
```js
|
58
|
+
diff = dd.diff(elementA, "<div>hello</div>")
|
59
|
+
```
|
60
|
+
|
61
|
+
You can create the Virtual DOM objects diffDOM uses, create them like this:
|
62
|
+
|
63
|
+
```js
|
64
|
+
import { nodeToObj, stringToObj } from "diff-dom"
|
65
|
+
|
66
|
+
obj1 = nodeToObj(elementA)
|
67
|
+
obj2 = stringToObj("<div>hello</div>")
|
68
|
+
```
|
69
|
+
|
70
|
+
Diffing between these objects will be faster than diffing DOM nodes and can be useful in environments
|
71
|
+
without access to the DOM.
|
72
|
+
|
73
|
+
### Advanced uses
|
74
|
+
|
75
|
+
#### Undo
|
76
|
+
|
77
|
+
Continuing on from the previous example, you can also undo a diff, like this:
|
78
|
+
|
79
|
+
```js
|
80
|
+
dd.undo(elementA, diff)
|
81
|
+
```
|
82
|
+
|
83
|
+
Now elementA will be what it was like before applying the diff.
|
84
|
+
|
85
|
+
#### Remote changes
|
86
|
+
|
87
|
+
If you need to move diffs from one machine to another one, you will likely want to send the diffs through a websocket connection or as part of a form submit. In both cases you need to convert the diff to a json `string`.
|
88
|
+
|
89
|
+
To convert a diff to a json string which you can send over the network, do:
|
90
|
+
|
91
|
+
```js
|
92
|
+
diffJson = JSON.stringify(diff)
|
93
|
+
```
|
94
|
+
|
95
|
+
On the receiving end you then need to unpack it like this:
|
96
|
+
|
97
|
+
```js
|
98
|
+
diff = JSON.parse(diffJson)
|
99
|
+
```
|
100
|
+
|
101
|
+
#### Error handling when patching/applying
|
102
|
+
|
103
|
+
Sometimes one may try to patch an elment without knowing whether the patch actually will apply cleanly. This should not be a problem. If diffDOM determines that a patch cannot be executed, it will simple return `false`. Else it will return `true`:
|
104
|
+
|
105
|
+
```js
|
106
|
+
result = dd.apply(element, diff)
|
107
|
+
|
108
|
+
if (result) {
|
109
|
+
console.log("no problem!")
|
110
|
+
} else {
|
111
|
+
console.log("diff could not be applied")
|
112
|
+
}
|
113
|
+
```
|
114
|
+
|
115
|
+
#### Advanced merging of text node changes
|
116
|
+
|
117
|
+
diffDOM does not include merging for changes to text nodes. However, it includes hooks so that you can add more advanced handling. Simple overwrite the `textDiff` function of the `diffDOM` instance. The functions TEXTDIFF and TEXTPATCH need to be defined in the code:
|
118
|
+
|
119
|
+
```js
|
120
|
+
dd = new diffDOM.DiffDOM({
|
121
|
+
textDiff: function (node, currentValue, expectedValue, newValue) {
|
122
|
+
if (currentValue === expectedValue) {
|
123
|
+
// The text node contains the text we expect it to contain, so we simple change the text of it to the new value.
|
124
|
+
node.data = newValue
|
125
|
+
} else {
|
126
|
+
// The text node currently does not contain what we expected it to contain, so we need to merge.
|
127
|
+
difference = TEXTDIFF(expectedValue, currentValue)
|
128
|
+
node.data = TEXTPATCH(newValue, difference)
|
129
|
+
}
|
130
|
+
return true
|
131
|
+
},
|
132
|
+
})
|
133
|
+
```
|
134
|
+
|
135
|
+
#### Pre and post diff hooks
|
136
|
+
|
137
|
+
diffDOM provides extension points before and after virtual and actual diffs, exposing some of the internals of the diff algorithm, and allowing you to make additional decisions based on that information.
|
138
|
+
|
139
|
+
```js
|
140
|
+
dd = new diffDOM.DiffDOM({
|
141
|
+
preVirtualDiffApply: function (info) {
|
142
|
+
console.log(info)
|
143
|
+
},
|
144
|
+
postVirtualDiffApply: function (info) {
|
145
|
+
console.log(info)
|
146
|
+
},
|
147
|
+
preDiffApply: function (info) {
|
148
|
+
console.log(info)
|
149
|
+
},
|
150
|
+
postDiffApply: function (info) {
|
151
|
+
console.log(info)
|
152
|
+
},
|
153
|
+
})
|
154
|
+
```
|
155
|
+
|
156
|
+
Additionally, the _pre_ hooks allow you to shortcircuit the standard behaviour of the diff by returning `true` from this callback. This will cause the `diffApply` functions to return prematurely, skipping their standard behaviour.
|
157
|
+
|
158
|
+
```js
|
159
|
+
dd = new diffDOM.DiffDOM({
|
160
|
+
// prevent removal of attributes
|
161
|
+
preDiffApply: function (info) {
|
162
|
+
if (info.diff.action === "removeAttribute") {
|
163
|
+
console.log("preventing attribute removal")
|
164
|
+
return true
|
165
|
+
}
|
166
|
+
},
|
167
|
+
})
|
168
|
+
```
|
169
|
+
|
170
|
+
#### Outer and Inner diff hooks
|
171
|
+
|
172
|
+
diffDOM also provides a way to filter outer diff
|
173
|
+
|
174
|
+
```js
|
175
|
+
dd = new diffDOM.DiffDOM({
|
176
|
+
filterOuterDiff: function (t1, t2, diffs) {
|
177
|
+
// can change current outer diffs by returning a new array,
|
178
|
+
// or by mutating outerDiffs.
|
179
|
+
if (
|
180
|
+
!diffs.length &&
|
181
|
+
t1.nodeName == "my-component" &&
|
182
|
+
t2.nodeName == t1.nodeName
|
183
|
+
) {
|
184
|
+
// will not diff childNodes
|
185
|
+
t1.innerDone = true
|
186
|
+
}
|
187
|
+
},
|
188
|
+
})
|
189
|
+
```
|
190
|
+
|
191
|
+
#### Debugging
|
192
|
+
|
193
|
+
For debugging you might want to set a max number of diff changes between two elements before diffDOM gives up. To allow for a maximum of 500 differences between elements when diffing, initialize diffDOM like this:
|
194
|
+
|
195
|
+
```js
|
196
|
+
dd = new diffDOM.DiffDOM({
|
197
|
+
debug: true,
|
198
|
+
diffcap: 500,
|
199
|
+
})
|
200
|
+
```
|
201
|
+
|
202
|
+
#### Disable value diff detection
|
203
|
+
|
204
|
+
For forms that have been filled out by a user in ways that have changed which value is associated with an input field or which options are checked/selected without
|
205
|
+
the DOM having been updated, the values are diffed. For use cases in which no changes have been made to any of the form values, one may choose to skip diffing the values. To do this, set `valueDiffing` to `false` as a configuration option to diffDOM:
|
206
|
+
|
207
|
+
```js
|
208
|
+
dd = new diffDOM.DiffDOM({
|
209
|
+
valueDiffing: false,
|
210
|
+
})
|
211
|
+
```
|
212
|
+
|
213
|
+
#### Interprete strings as case caseSensitive
|
214
|
+
|
215
|
+
Strings of HTML can normally be interpreted case-insensitively as HTML tags don't differentiate between uppercase and lowercase. However, in the case of XML (SVGs, XHTML) there is a difference and this should be enabled. To do this, set `caseSensitive` to `true` as a configuration option to diffDOM:
|
216
|
+
|
217
|
+
|
218
|
+
```js
|
219
|
+
dd = new diffDOM.DiffDOM({
|
220
|
+
caseSensitive: true,
|
221
|
+
})
|
222
|
+
```
|
223
|
+
|
224
|
+
**NOTE!** If there is an SVG inside of the HTML in the string, diffDOM can automatically determine that it should switch to case sensitivity. It is only if the diff happens entirely within an SVG that it is required to specify this.
|
@@ -0,0 +1,2 @@
|
|
1
|
+
var diffDOM=function(e){"use strict";var t=function(){return t=Object.assign||function(e){for(var t,n=arguments,o=1,s=arguments.length;o<s;o++)for(var i in t=n[o])Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i]);return e},t.apply(this,arguments)};function n(e,t,n){if(n||2===arguments.length)for(var o,s=0,i=t.length;s<i;s++)!o&&s in t||(o||(o=Array.prototype.slice.call(t,0,s)),o[s]=t[s]);return e.concat(o||Array.prototype.slice.call(t))}"function"==typeof SuppressedError&&SuppressedError;var o=function(){function e(e){void 0===e&&(e={});var t=this;Object.entries(e).forEach((function(e){var n=e[0],o=e[1];return t[n]=o}))}return e.prototype.toString=function(){return JSON.stringify(this)},e.prototype.setValue=function(e,t){return this[e]=t,this},e}();function s(e){for(var t=arguments,n=[],o=1;o<arguments.length;o++)n[o-1]=t[o];return null!=e&&n.some((function(t){var n,o;return"function"==typeof(null===(o=null===(n=null==e?void 0:e.ownerDocument)||void 0===n?void 0:n.defaultView)||void 0===o?void 0:o[t])&&e instanceof e.ownerDocument.defaultView[t]}))}function i(e,t,n){var o;return"#text"===e.nodeName?o=n.document.createTextNode(e.data):"#comment"===e.nodeName?o=n.document.createComment(e.data):(t?(o=n.document.createElementNS("http://www.w3.org/2000/svg",e.nodeName),"foreignObject"===e.nodeName&&(t=!1)):"svg"===e.nodeName.toLowerCase()?(o=n.document.createElementNS("http://www.w3.org/2000/svg","svg"),t=!0):o=n.document.createElement(e.nodeName),e.attributes&&Object.entries(e.attributes).forEach((function(e){var t=e[0],n=e[1];return o.setAttribute(t,n)})),e.childNodes&&e.childNodes.forEach((function(e){return o.appendChild(i(e,t,n))})),n.valueDiffing&&(e.value&&s(o,"HTMLButtonElement","HTMLDataElement","HTMLInputElement","HTMLLIElement","HTMLMeterElement","HTMLOptionElement","HTMLProgressElement","HTMLParamElement")&&(o.value=e.value),e.checked&&s(o,"HTMLInputElement")&&(o.checked=e.checked),e.selected&&s(o,"HTMLOptionElement")&&(o.selected=e.selected))),o}var a=function(e,t){for(t=t.slice();t.length>0;){var n=t.splice(0,1)[0];e=e.childNodes[n]}return e};function l(e,t,o){var l,c,r,u=t[o._const.action],d=t[o._const.route];[o._const.addElement,o._const.addTextElement].includes(u)||(l=a(e,d));var h={diff:t,node:l};if(o.preDiffApply(h))return!0;switch(u){case o._const.addAttribute:if(!l||!s(l,"Element"))return!1;l.setAttribute(t[o._const.name],t[o._const.value]);break;case o._const.modifyAttribute:if(!l||!s(l,"Element"))return!1;l.setAttribute(t[o._const.name],t[o._const.newValue]),s(l,"HTMLInputElement")&&"value"===t[o._const.name]&&(l.value=t[o._const.newValue]);break;case o._const.removeAttribute:if(!l||!s(l,"Element"))return!1;l.removeAttribute(t[o._const.name]);break;case o._const.modifyTextElement:if(!l||!s(l,"Text"))return!1;o.textDiff(l,l.data,t[o._const.oldValue],t[o._const.newValue]),s(l.parentNode,"HTMLTextAreaElement")&&(l.parentNode.value=t[o._const.newValue]);break;case o._const.modifyValue:if(!l||void 0===l.value)return!1;l.value=t[o._const.newValue];break;case o._const.modifyComment:if(!l||!s(l,"Comment"))return!1;o.textDiff(l,l.data,t[o._const.oldValue],t[o._const.newValue]);break;case o._const.modifyChecked:if(!l||void 0===l.checked)return!1;l.checked=t[o._const.newValue];break;case o._const.modifySelected:if(!l||void 0===l.selected)return!1;l.selected=t[o._const.newValue];break;case o._const.replaceElement:var f="svg"===t[o._const.newValue].nodeName.toLowerCase()||"http://www.w3.org/2000/svg"===l.parentNode.namespaceURI;l.parentNode.replaceChild(i(t[o._const.newValue],f,o),l);break;case o._const.relocateGroup:n([],new Array(t[o._const.groupLength]),!0).map((function(){return l.removeChild(l.childNodes[t[o._const.from]])})).forEach((function(e,n){0===n&&(r=l.childNodes[t[o._const.to]]),l.insertBefore(e,r||null)}));break;case o._const.removeElement:l.parentNode.removeChild(l);break;case o._const.addElement:var p=(_=d.slice()).splice(_.length-1,1)[0];if(!s(l=a(e,_),"Element"))return!1;l.insertBefore(i(t[o._const.element],"http://www.w3.org/2000/svg"===l.namespaceURI,o),l.childNodes[p]||null);break;case o._const.removeTextElement:if(!l||3!==l.nodeType)return!1;var m=l.parentNode;m.removeChild(l),s(m,"HTMLTextAreaElement")&&(m.value="");break;case o._const.addTextElement:var _;p=(_=d.slice()).splice(_.length-1,1)[0];if(c=o.document.createTextNode(t[o._const.value]),!(l=a(e,_)).childNodes)return!1;l.insertBefore(c,l.childNodes[p]||null),s(l.parentNode,"HTMLTextAreaElement")&&(l.parentNode.value=t[o._const.value]);break;default:console.log("unknown action")}return o.postDiffApply({diff:h.diff,node:h.node,newNode:c}),!0}function c(e,t,n){var o=e[t];e[t]=e[n],e[n]=o}function r(e,t,n){(t=t.slice()).reverse(),t.forEach((function(t){!function(e,t,n){switch(t[n._const.action]){case n._const.addAttribute:t[n._const.action]=n._const.removeAttribute,l(e,t,n);break;case n._const.modifyAttribute:c(t,n._const.oldValue,n._const.newValue),l(e,t,n);break;case n._const.removeAttribute:t[n._const.action]=n._const.addAttribute,l(e,t,n);break;case n._const.modifyTextElement:case n._const.modifyValue:case n._const.modifyComment:case n._const.modifyChecked:case n._const.modifySelected:case n._const.replaceElement:c(t,n._const.oldValue,n._const.newValue),l(e,t,n);break;case n._const.relocateGroup:c(t,n._const.from,n._const.to),l(e,t,n);break;case n._const.removeElement:t[n._const.action]=n._const.addElement,l(e,t,n);break;case n._const.addElement:t[n._const.action]=n._const.removeElement,l(e,t,n);break;case n._const.removeTextElement:t[n._const.action]=n._const.addTextElement,l(e,t,n);break;case n._const.addTextElement:t[n._const.action]=n._const.removeTextElement,l(e,t,n);break;default:console.log("unknown action")}}(e,t,n)}))}var u=function(e){var t=[];return t.push(e.nodeName),"#text"!==e.nodeName&&"#comment"!==e.nodeName&&e.attributes&&(e.attributes.class&&t.push("".concat(e.nodeName,".").concat(e.attributes.class.replace(/ /g,"."))),e.attributes.id&&t.push("".concat(e.nodeName,"#").concat(e.attributes.id))),t},d=function(e){var t={},n={};return e.forEach((function(e){u(e).forEach((function(e){var o=e in t;o||e in n?o&&(delete t[e],n[e]=!0):t[e]=!0}))})),t},h=function(e,t){var n=d(e),o=d(t),s={};return Object.keys(n).forEach((function(e){o[e]&&(s[e]=!0)})),s},f=function(e){return delete e.outerDone,delete e.innerDone,delete e.valueDone,!e.childNodes||e.childNodes.every(f)},p=function(e){if(Object.prototype.hasOwnProperty.call(e,"data"))return{nodeName:"#text"===e.nodeName?"#text":"#comment",data:e.data};var n={nodeName:e.nodeName};return Object.prototype.hasOwnProperty.call(e,"attributes")&&(n.attributes=t({},e.attributes)),Object.prototype.hasOwnProperty.call(e,"checked")&&(n.checked=e.checked),Object.prototype.hasOwnProperty.call(e,"value")&&(n.value=e.value),Object.prototype.hasOwnProperty.call(e,"selected")&&(n.selected=e.selected),Object.prototype.hasOwnProperty.call(e,"childNodes")&&(n.childNodes=e.childNodes.map((function(e){return p(e)}))),n},m=function(e,t){if(!["nodeName","value","checked","selected","data"].every((function(n){return e[n]===t[n]})))return!1;if(Object.prototype.hasOwnProperty.call(e,"data"))return!0;if(Boolean(e.attributes)!==Boolean(t.attributes))return!1;if(Boolean(e.childNodes)!==Boolean(t.childNodes))return!1;if(e.attributes){var n=Object.keys(e.attributes),o=Object.keys(t.attributes);if(n.length!==o.length)return!1;if(!n.every((function(n){return e.attributes[n]===t.attributes[n]})))return!1}if(e.childNodes){if(e.childNodes.length!==t.childNodes.length)return!1;if(!e.childNodes.every((function(e,n){return m(e,t.childNodes[n])})))return!1}return!0},_=function(e,t,n,o,s){if(void 0===s&&(s=!1),!e||!t)return!1;if(e.nodeName!==t.nodeName)return!1;if(["#text","#comment"].includes(e.nodeName))return!!s||e.data===t.data;if(e.nodeName in n)return!0;if(e.attributes&&t.attributes){if(e.attributes.id){if(e.attributes.id!==t.attributes.id)return!1;if("".concat(e.nodeName,"#").concat(e.attributes.id)in n)return!0}if(e.attributes.class&&e.attributes.class===t.attributes.class)if("".concat(e.nodeName,".").concat(e.attributes.class.replace(/ /g,"."))in n)return!0}if(o)return!0;var i=e.childNodes?e.childNodes.slice().reverse():[],a=t.childNodes?t.childNodes.slice().reverse():[];if(i.length!==a.length)return!1;if(s)return i.every((function(e,t){return e.nodeName===a[t].nodeName}));var l=h(i,a);return i.every((function(e,t){return _(e,a[t],l,!0,!0)}))},v=function(e,t){return n([],new Array(e),!0).map((function(){return t}))},g=function(e,t){for(var o=e.childNodes?e.childNodes:[],s=t.childNodes?t.childNodes:[],i=v(o.length,!1),a=v(s.length,!1),l=[],c=function(){return arguments[1]},r=!1,d=function(){var e=function(e,t,o,s){var i=0,a=[],l=e.length,c=t.length,r=n([],new Array(l+1),!0).map((function(){return[]})),d=h(e,t),f=l===c;f&&e.some((function(e,n){var o=u(e),s=u(t[n]);return o.length!==s.length?(f=!1,!0):(o.some((function(e,t){if(e!==s[t])return f=!1,!0})),!f||void 0)}));for(var p=0;p<l;p++)for(var m=e[p],v=0;v<c;v++){var g=t[v];o[p]||s[v]||!_(m,g,d,f)?r[p+1][v+1]=0:(r[p+1][v+1]=r[p][v]?r[p][v]+1:1,r[p+1][v+1]>=i&&(i=r[p+1][v+1],a=[p+1,v+1]))}return 0!==i&&{oldValue:a[0]-i,newValue:a[1]-i,length:i}}(o,s,i,a);e?(l.push(e),n([],new Array(e.length),!0).map(c).forEach((function(t){return function(e,t,n,o){e[n.oldValue+o]=!0,t[n.newValue+o]=!0}(i,a,e,t)}))):r=!0};!r;)d();return e.subsets=l,e.subsetsAge=100,l},V=function(){function e(){this.list=[]}return e.prototype.add=function(e){var t;(t=this.list).push.apply(t,e)},e.prototype.forEach=function(e){this.list.forEach((function(t){return e(t)}))},e}();function b(e,t){var n,o,s=e;for(t=t.slice();t.length>0;)o=t.splice(0,1)[0],n=s,s=s.childNodes?s.childNodes[o]:void 0;return{node:s,parentNode:n,nodeIndex:o}}function N(e,t,n){return t.forEach((function(t){!function(e,t,n){var o,s,i,a;if(![n._const.addElement,n._const.addTextElement].includes(t[n._const.action])){var l=b(e,t[n._const.route]);s=l.node,i=l.parentNode,a=l.nodeIndex}var c,r,u=[],d={diff:t,node:s};if(n.preVirtualDiffApply(d))return!0;switch(t[n._const.action]){case n._const.addAttribute:s.attributes||(s.attributes={}),s.attributes[t[n._const.name]]=t[n._const.value],"checked"===t[n._const.name]?s.checked=!0:"selected"===t[n._const.name]?s.selected=!0:"INPUT"===s.nodeName&&"value"===t[n._const.name]&&(s.value=t[n._const.value]);break;case n._const.modifyAttribute:s.attributes[t[n._const.name]]=t[n._const.newValue];break;case n._const.removeAttribute:delete s.attributes[t[n._const.name]],0===Object.keys(s.attributes).length&&delete s.attributes,"checked"===t[n._const.name]?s.checked=!1:"selected"===t[n._const.name]?delete s.selected:"INPUT"===s.nodeName&&"value"===t[n._const.name]&&delete s.value;break;case n._const.modifyTextElement:s.data=t[n._const.newValue],"TEXTAREA"===i.nodeName&&(i.value=t[n._const.newValue]);break;case n._const.modifyValue:s.value=t[n._const.newValue];break;case n._const.modifyComment:s.data=t[n._const.newValue];break;case n._const.modifyChecked:s.checked=t[n._const.newValue];break;case n._const.modifySelected:s.selected=t[n._const.newValue];break;case n._const.replaceElement:c=p(t[n._const.newValue]),i.childNodes[a]=c;break;case n._const.relocateGroup:s.childNodes.splice(t[n._const.from],t[n._const.groupLength]).reverse().forEach((function(e){return s.childNodes.splice(t[n._const.to],0,e)})),s.subsets&&s.subsets.forEach((function(e){if(t[n._const.from]<t[n._const.to]&&e.oldValue<=t[n._const.to]&&e.oldValue>t[n._const.from])e.oldValue-=t[n._const.groupLength],(o=e.oldValue+e.length-t[n._const.to])>0&&(u.push({oldValue:t[n._const.to]+t[n._const.groupLength],newValue:e.newValue+e.length-o,length:o}),e.length-=o);else if(t[n._const.from]>t[n._const.to]&&e.oldValue>t[n._const.to]&&e.oldValue<t[n._const.from]){var o;e.oldValue+=t[n._const.groupLength],(o=e.oldValue+e.length-t[n._const.to])>0&&(u.push({oldValue:t[n._const.to]+t[n._const.groupLength],newValue:e.newValue+e.length-o,length:o}),e.length-=o)}else e.oldValue===t[n._const.from]&&(e.oldValue=t[n._const.to])}));break;case n._const.removeElement:i.childNodes.splice(a,1),i.subsets&&i.subsets.forEach((function(e){e.oldValue>a?e.oldValue-=1:e.oldValue===a?e.delete=!0:e.oldValue<a&&e.oldValue+e.length>a&&(e.oldValue+e.length-1===a?e.length--:(u.push({newValue:e.newValue+a-e.oldValue,oldValue:a,length:e.length-a+e.oldValue-1}),e.length=a-e.oldValue))})),s=i;break;case n._const.addElement:var h=(r=t[n._const.route].slice()).splice(r.length-1,1)[0];s=null===(o=b(e,r))||void 0===o?void 0:o.node,c=p(t[n._const.element]),s.childNodes||(s.childNodes=[]),h>=s.childNodes.length?s.childNodes.push(c):s.childNodes.splice(h,0,c),s.subsets&&s.subsets.forEach((function(e){if(e.oldValue>=h)e.oldValue+=1;else if(e.oldValue<h&&e.oldValue+e.length>h){var t=e.oldValue+e.length-h;u.push({newValue:e.newValue+e.length-t,oldValue:h+1,length:t}),e.length-=t}}));break;case n._const.removeTextElement:i.childNodes.splice(a,1),"TEXTAREA"===i.nodeName&&delete i.value,i.subsets&&i.subsets.forEach((function(e){e.oldValue>a?e.oldValue-=1:e.oldValue===a?e.delete=!0:e.oldValue<a&&e.oldValue+e.length>a&&(e.oldValue+e.length-1===a?e.length--:(u.push({newValue:e.newValue+a-e.oldValue,oldValue:a,length:e.length-a+e.oldValue-1}),e.length=a-e.oldValue))})),s=i;break;case n._const.addTextElement:var f=(r=t[n._const.route].slice()).splice(r.length-1,1)[0];c={nodeName:"#text",data:t[n._const.value]},(s=b(e,r).node).childNodes||(s.childNodes=[]),f>=s.childNodes.length?s.childNodes.push(c):s.childNodes.splice(f,0,c),"TEXTAREA"===s.nodeName&&(s.value=t[n._const.newValue]),s.subsets&&s.subsets.forEach((function(e){if(e.oldValue>=f&&(e.oldValue+=1),e.oldValue<f&&e.oldValue+e.length>f){var t=e.oldValue+e.length-f;u.push({newValue:e.newValue+e.length-t,oldValue:f+1,length:t}),e.length-=t}}));break;default:console.log("unknown action")}s.subsets&&(s.subsets=s.subsets.filter((function(e){return!e.delete&&e.oldValue!==e.newValue})),u.length&&(s.subsets=s.subsets.concat(u))),n.postVirtualDiffApply({node:d.node,diff:d.diff,newNode:c})}(e,t,n)})),!0}function y(e,t){void 0===t&&(t={valueDiffing:!0});var n={nodeName:e.nodeName};if(s(e,"Text","Comment"))n.data=e.data;else{if(e.attributes&&e.attributes.length>0)n.attributes={},Array.prototype.slice.call(e.attributes).forEach((function(e){return n.attributes[e.name]=e.value}));if(e.childNodes&&e.childNodes.length>0)n.childNodes=[],Array.prototype.slice.call(e.childNodes).forEach((function(e){return n.childNodes.push(y(e,t))}));t.valueDiffing&&(s(e,"HTMLTextAreaElement")&&(n.value=e.value),s(e,"HTMLInputElement")&&["radio","checkbox"].includes(e.type.toLowerCase())&&void 0!==e.checked?n.checked=e.checked:s(e,"HTMLButtonElement","HTMLDataElement","HTMLInputElement","HTMLLIElement","HTMLMeterElement","HTMLOptionElement","HTMLProgressElement","HTMLParamElement")&&(n.value=e.value),s(e,"HTMLOptionElement")&&(n.selected=e.selected))}return n}var w=/<\s*\/*[a-zA-Z:_][a-zA-Z0-9:_\-.]*\s*(?:"[^"]*"['"]*|'[^']*'['"]*|[^'"/>])*\/*\s*>|<!--(?:.|\n|\r)*?-->/g,E=/\s([^'"/\s><]+?)[\s/>]|([^\s=]+)=\s?(".*?"|'.*?')/g;function k(e){return e.replace(/</g,"<").replace(/>/g,">").replace(/&/g,"&")}var x={area:!0,base:!0,br:!0,col:!0,embed:!0,hr:!0,img:!0,input:!0,keygen:!0,link:!0,menuItem:!0,meta:!0,param:!0,source:!0,track:!0,wbr:!0},T=function(e,t){var n={nodeName:"",attributes:{}},o=!1,s=e.match(/<\/?([^\s]+?)[/\s>]/);if(s&&(n.nodeName=t||"svg"===s[1]?s[1]:s[1].toUpperCase(),(x[s[1]]||"/"===e.charAt(e.length-2))&&(o=!0),n.nodeName.startsWith("!--"))){var i=e.indexOf("--\x3e");return{type:"comment",node:{nodeName:"#comment",data:-1!==i?e.slice(4,i):""},voidElement:o}}for(var a=new RegExp(E),l=null,c=!1;!c;)if(null===(l=a.exec(e)))c=!0;else if(l[0].trim())if(l[1]){var r=l[1].trim(),u=[r,""];r.indexOf("=")>-1&&(u=r.split("=")),n.attributes[u[0]]=u[1],a.lastIndex--}else l[2]&&(n.attributes[l[2]]=l[3].trim().substring(1,l[3].length-1));return{type:"tag",node:n,voidElement:o}},O=function(e,t){void 0===t&&(t={valueDiffing:!0,caseSensitive:!1});var n,o=[],s=-1,i=[],a=!1;if(0!==e.indexOf("<")){var l=e.indexOf("<");o.push({nodeName:"#text",data:-1===l?e:e.substring(0,l)})}return e.replace(w,(function(l,c){var r="/"!==l.charAt(1),u=l.startsWith("\x3c!--"),d=c+l.length,h=e.charAt(d);if(u){var f=T(l,t.caseSensitive).node;if(s<0)return o.push(f),"";var p=i[s];return p&&f.nodeName&&(p.node.childNodes||(p.node.childNodes=[]),p.node.childNodes.push(f)),""}if(r){if("svg"===(n=T(l,t.caseSensitive||a)).node.nodeName&&(a=!0),s++,!n.voidElement&&h&&"<"!==h){n.node.childNodes||(n.node.childNodes=[]);var m=k(e.slice(d,e.indexOf("<",d)));n.node.childNodes.push({nodeName:"#text",data:m}),t.valueDiffing&&"TEXTAREA"===n.node.nodeName&&(n.node.value=m)}0===s&&n.node.nodeName&&o.push(n.node);var _=i[s-1];_&&n.node.nodeName&&(_.node.childNodes||(_.node.childNodes=[]),_.node.childNodes.push(n.node)),i[s]=n}if((!r||n.voidElement)&&(s>-1&&(n.voidElement||t.caseSensitive&&n.node.nodeName===l.slice(2,-1)||!t.caseSensitive&&n.node.nodeName.toUpperCase()===l.slice(2,-1).toUpperCase())&&--s>-1&&("svg"===n.node.nodeName&&(a=!1),n=i[s]),"<"!==h&&h)){var v=-1===s?o:i[s].node.childNodes||[],g=e.indexOf("<",d);m=k(e.slice(d,-1===g?void 0:g));v.push({nodeName:"#text",data:m})}return""})),o[0]},A=function(){function e(e,t,n){this.options=n,this.t1="undefined"!=typeof Element&&s(e,"Element")?y(e,this.options):"string"==typeof e?O(e,this.options):JSON.parse(JSON.stringify(e)),this.t2="undefined"!=typeof Element&&s(t,"Element")?y(t,this.options):"string"==typeof t?O(t,this.options):JSON.parse(JSON.stringify(t)),this.diffcount=0,this.foundAll=!1,this.debug&&(this.t1Orig="undefined"!=typeof Element&&s(e,"Element")?y(e,this.options):"string"==typeof e?O(e,this.options):JSON.parse(JSON.stringify(e)),this.t2Orig="undefined"!=typeof Element&&s(t,"Element")?y(t,this.options):"string"==typeof t?O(t,this.options):JSON.parse(JSON.stringify(t))),this.tracker=new V}return e.prototype.init=function(){return this.findDiffs(this.t1,this.t2)},e.prototype.findDiffs=function(e,t){var n;do{if(this.options.debug&&(this.diffcount+=1,this.diffcount>this.options.diffcap))throw new Error("surpassed diffcap:".concat(JSON.stringify(this.t1Orig)," -> ").concat(JSON.stringify(this.t2Orig)));0===(n=this.findNextDiff(e,t,[])).length&&(m(e,t)||(this.foundAll?console.error("Could not find remaining diffs!"):(this.foundAll=!0,f(e),n=this.findNextDiff(e,t,[])))),n.length>0&&(this.foundAll=!1,this.tracker.add(n),N(e,n,this.options))}while(n.length>0);return this.tracker.list},e.prototype.findNextDiff=function(e,t,n){var o,s;if(this.options.maxDepth&&n.length>this.options.maxDepth)return[];if(!e.outerDone){if(o=this.findOuterDiff(e,t,n),this.options.filterOuterDiff&&(s=this.options.filterOuterDiff(e,t,o))&&(o=s),o.length>0)return e.outerDone=!0,o;e.outerDone=!0}if(Object.prototype.hasOwnProperty.call(e,"data"))return[];if(!e.innerDone){if((o=this.findInnerDiff(e,t,n)).length>0)return o;e.innerDone=!0}if(this.options.valueDiffing&&!e.valueDone){if((o=this.findValueDiff(e,t,n)).length>0)return e.valueDone=!0,o;e.valueDone=!0}return[]},e.prototype.findOuterDiff=function(e,t,n){var s,i,a,l,c,r,u=[];if(e.nodeName!==t.nodeName){if(!n.length)throw new Error("Top level nodes have to be of the same kind.");return[(new o).setValue(this.options._const.action,this.options._const.replaceElement).setValue(this.options._const.oldValue,p(e)).setValue(this.options._const.newValue,p(t)).setValue(this.options._const.route,n)]}if(n.length&&this.options.diffcap<Math.abs((e.childNodes||[]).length-(t.childNodes||[]).length))return[(new o).setValue(this.options._const.action,this.options._const.replaceElement).setValue(this.options._const.oldValue,p(e)).setValue(this.options._const.newValue,p(t)).setValue(this.options._const.route,n)];if(Object.prototype.hasOwnProperty.call(e,"data")&&e.data!==t.data)return"#text"===e.nodeName?[(new o).setValue(this.options._const.action,this.options._const.modifyTextElement).setValue(this.options._const.route,n).setValue(this.options._const.oldValue,e.data).setValue(this.options._const.newValue,t.data)]:[(new o).setValue(this.options._const.action,this.options._const.modifyComment).setValue(this.options._const.route,n).setValue(this.options._const.oldValue,e.data).setValue(this.options._const.newValue,t.data)];for(i=e.attributes?Object.keys(e.attributes).sort():[],a=t.attributes?Object.keys(t.attributes).sort():[],l=i.length,r=0;r<l;r++)s=i[r],-1===(c=a.indexOf(s))?u.push((new o).setValue(this.options._const.action,this.options._const.removeAttribute).setValue(this.options._const.route,n).setValue(this.options._const.name,s).setValue(this.options._const.value,e.attributes[s])):(a.splice(c,1),e.attributes[s]!==t.attributes[s]&&u.push((new o).setValue(this.options._const.action,this.options._const.modifyAttribute).setValue(this.options._const.route,n).setValue(this.options._const.name,s).setValue(this.options._const.oldValue,e.attributes[s]).setValue(this.options._const.newValue,t.attributes[s])));for(l=a.length,r=0;r<l;r++)s=a[r],u.push((new o).setValue(this.options._const.action,this.options._const.addAttribute).setValue(this.options._const.route,n).setValue(this.options._const.name,s).setValue(this.options._const.value,t.attributes[s]));return u},e.prototype.findInnerDiff=function(e,t,n){var s=e.childNodes?e.childNodes.slice():[],i=t.childNodes?t.childNodes.slice():[],a=Math.max(s.length,i.length),l=Math.abs(s.length-i.length),c=[],r=0;if(!this.options.maxChildCount||a<this.options.maxChildCount){var u=Boolean(e.subsets&&e.subsetsAge--),d=u?e.subsets:e.childNodes&&t.childNodes?g(e,t):[];if(d.length>0&&(c=this.attemptGroupRelocation(e,t,d,n,u)).length>0)return c}for(var h=0;h<a;h+=1){var f=s[h],_=i[h];l&&(f&&!_?"#text"===f.nodeName?(c.push((new o).setValue(this.options._const.action,this.options._const.removeTextElement).setValue(this.options._const.route,n.concat(r)).setValue(this.options._const.value,f.data)),r-=1):(c.push((new o).setValue(this.options._const.action,this.options._const.removeElement).setValue(this.options._const.route,n.concat(r)).setValue(this.options._const.element,p(f))),r-=1):_&&!f&&("#text"===_.nodeName?c.push((new o).setValue(this.options._const.action,this.options._const.addTextElement).setValue(this.options._const.route,n.concat(r)).setValue(this.options._const.value,_.data)):c.push((new o).setValue(this.options._const.action,this.options._const.addElement).setValue(this.options._const.route,n.concat(r)).setValue(this.options._const.element,p(_))))),f&&_&&(!this.options.maxChildCount||a<this.options.maxChildCount?c=c.concat(this.findNextDiff(f,_,n.concat(r))):m(f,_)||(s.length>i.length?("#text"===f.nodeName?c.push((new o).setValue(this.options._const.action,this.options._const.removeTextElement).setValue(this.options._const.route,n.concat(r)).setValue(this.options._const.value,f.data)):c.push((new o).setValue(this.options._const.action,this.options._const.removeElement).setValue(this.options._const.element,p(f)).setValue(this.options._const.route,n.concat(r))),s.splice(h,1),h-=1,r-=1,l-=1):s.length<i.length?(c=c.concat([(new o).setValue(this.options._const.action,this.options._const.addElement).setValue(this.options._const.element,p(_)).setValue(this.options._const.route,n.concat(r))]),s.splice(h,0,p(_)),l-=1):c=c.concat([(new o).setValue(this.options._const.action,this.options._const.replaceElement).setValue(this.options._const.oldValue,p(f)).setValue(this.options._const.newValue,p(_)).setValue(this.options._const.route,n.concat(r))]))),r+=1}return e.innerDone=!0,c},e.prototype.attemptGroupRelocation=function(e,t,n,s,i){for(var a,l,c,r,u,d=function(e,t,n){var o=e.childNodes?v(e.childNodes.length,!0):[],s=t.childNodes?v(t.childNodes.length,!0):[],i=0;return n.forEach((function(e){for(var t=e.oldValue+e.length,n=e.newValue+e.length,a=e.oldValue;a<t;a+=1)o[a]=i;for(a=e.newValue;a<n;a+=1)s[a]=i;i+=1})),{gaps1:o,gaps2:s}}(e,t,n),h=d.gaps1,f=d.gaps2,m=e.childNodes.slice(),g=t.childNodes.slice(),V=Math.min(h.length,f.length),b=[],N=0,y=0;N<V;y+=1,N+=1)if(!i||!0!==h[N]&&!0!==f[N]){if(!0===h[y])if("#text"===(r=m[y]).nodeName)if("#text"===g[N].nodeName){if(r.data!==g[N].data){for(var w=y;m.length>w+1&&"#text"===m[w+1].nodeName;)if(w+=1,g[N].data===m[w].data){u=!0;break}u||b.push((new o).setValue(this.options._const.action,this.options._const.modifyTextElement).setValue(this.options._const.route,s.concat(y)).setValue(this.options._const.oldValue,r.data).setValue(this.options._const.newValue,g[N].data))}}else b.push((new o).setValue(this.options._const.action,this.options._const.removeTextElement).setValue(this.options._const.route,s.concat(y)).setValue(this.options._const.value,r.data)),h.splice(y,1),m.splice(y,1),V=Math.min(h.length,f.length),y-=1,N-=1;else!0===f[N]?b.push((new o).setValue(this.options._const.action,this.options._const.replaceElement).setValue(this.options._const.oldValue,p(r)).setValue(this.options._const.newValue,p(g[N])).setValue(this.options._const.route,s.concat(y))):(b.push((new o).setValue(this.options._const.action,this.options._const.removeElement).setValue(this.options._const.route,s.concat(y)).setValue(this.options._const.element,p(r))),h.splice(y,1),m.splice(y,1),V=Math.min(h.length,f.length),y-=1,N-=1);else if(!0===f[N])"#text"===(r=g[N]).nodeName?(b.push((new o).setValue(this.options._const.action,this.options._const.addTextElement).setValue(this.options._const.route,s.concat(y)).setValue(this.options._const.value,r.data)),h.splice(y,0,!0),m.splice(y,0,{nodeName:"#text",data:r.data}),V=Math.min(h.length,f.length)):(b.push((new o).setValue(this.options._const.action,this.options._const.addElement).setValue(this.options._const.route,s.concat(y)).setValue(this.options._const.element,p(r))),h.splice(y,0,!0),m.splice(y,0,p(r)),V=Math.min(h.length,f.length));else if(h[y]!==f[N]){if(b.length>0)return b;if(c=n[h[y]],(l=Math.min(c.newValue,m.length-c.length))!==c.oldValue&&l>-1){a=!1;for(var E=0;E<c.length;E+=1)_(m[l+E],m[c.oldValue+E],{},!1,!0)||(a=!0);if(a)return[(new o).setValue(this.options._const.action,this.options._const.relocateGroup).setValue(this.options._const.groupLength,c.length).setValue(this.options._const.from,c.oldValue).setValue(this.options._const.to,l).setValue(this.options._const.route,s)]}}}else;return b},e.prototype.findValueDiff=function(e,t,n){var s=[];return e.selected!==t.selected&&s.push((new o).setValue(this.options._const.action,this.options._const.modifySelected).setValue(this.options._const.oldValue,e.selected).setValue(this.options._const.newValue,t.selected).setValue(this.options._const.route,n)),(e.value||t.value)&&e.value!==t.value&&"OPTION"!==e.nodeName&&s.push((new o).setValue(this.options._const.action,this.options._const.modifyValue).setValue(this.options._const.oldValue,e.value||"").setValue(this.options._const.newValue,t.value||"").setValue(this.options._const.route,n)),e.checked!==t.checked&&s.push((new o).setValue(this.options._const.action,this.options._const.modifyChecked).setValue(this.options._const.oldValue,e.checked).setValue(this.options._const.newValue,t.checked).setValue(this.options._const.route,n)),s},e}(),D={debug:!1,diffcap:10,maxDepth:!1,maxChildCount:50,valueDiffing:!0,textDiff:function(e,t,n,o){e.data=o},preVirtualDiffApply:function(){},postVirtualDiffApply:function(){},preDiffApply:function(){},postDiffApply:function(){},filterOuterDiff:null,compress:!1,_const:!1,document:!("undefined"==typeof window||!window.document)&&window.document,components:[]},L=function(){function e(e){if(void 0===e&&(e={}),Object.entries(D).forEach((function(t){var n=t[0],o=t[1];Object.prototype.hasOwnProperty.call(e,n)||(e[n]=o)})),!e._const){var t=["addAttribute","modifyAttribute","removeAttribute","modifyTextElement","relocateGroup","removeElement","addElement","removeTextElement","addTextElement","replaceElement","modifyValue","modifyChecked","modifySelected","modifyComment","action","route","oldValue","newValue","element","group","groupLength","from","to","name","value","data","attributes","nodeName","childNodes","checked","selected"],n={};e.compress?t.forEach((function(e,t){return n[e]=t})):t.forEach((function(e){return n[e]=e})),e._const=n}this.options=e}return e.prototype.apply=function(e,t){return function(e,t,n){return t.every((function(t){return l(e,t,n)}))}(e,t,this.options)},e.prototype.undo=function(e,t){return r(e,t,this.options)},e.prototype.diff=function(e,t){return new A(e,t,this.options).init()},e}(),M=function(){function e(e){void 0===e&&(e={});var t=this;this.pad="│ ",this.padding="",this.tick=1,this.messages=[];var n=function(e,n){var o=e[n];e[n]=function(){for(var s=arguments,i=[],a=0;a<arguments.length;a++)i[a]=s[a];t.fin(n,Array.prototype.slice.call(i));var l=o.apply(e,i);return t.fout(n,l),l}};for(var o in e)"function"==typeof e[o]&&n(e,o);this.log("┌ TRACELOG START")}return e.prototype.fin=function(e,t){this.padding+=this.pad,this.log("├─> entering ".concat(e),t)},e.prototype.fout=function(e,t){this.log("│<──┘ generated return value",t),this.padding=this.padding.substring(0,this.padding.length-this.pad.length)},e.prototype.format=function(e,t){return"".concat(function(e){for(var t="".concat(e);t.length<4;)t="0".concat(e);return t}(t),"> ").concat(this.padding).concat(e)},e.prototype.log=function(){for(var e=arguments,t=[],n=0;n<arguments.length;n++)t[n]=e[n];var o=function(e){return e?"string"==typeof e?e:s(e,"HTMLElement")?e.outerHTML||"<empty>":e instanceof Array?"[".concat(e.map(o).join(","),"]"):e.toString()||e.valueOf()||"<unknown>":"<falsey>"},i=t.map(o).join(", ");this.messages.push(this.format(i,this.tick++))},e.prototype.toString=function(){for(var e="└───";e.length<=this.padding.length+this.pad.length;)e+="× ";var t=this.padding;return this.padding="",e=this.format(e,this.tick),this.padding=t,"".concat(this.messages.join("\n"),"\n").concat(e)},e}();return e.DiffDOM=L,e.TraceLogger=M,e.nodeToObj=y,e.stringToObj=O,e}({});
|
2
|
+
//# sourceMappingURL=diffDOM.js.map
|