@createiq/htmldiff 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,4 @@
1
+ import fs from 'node:fs/promises'
1
2
  import { describe, expect, it } from 'vitest'
2
3
 
3
4
  import HtmlDiff from '../src/HtmlDiff'
@@ -12,7 +13,7 @@ describe('HtmlDiff', () => {
12
13
  ],
13
14
  ['a c', 'a b c', "a <ins class='diffins'>b </ins>c"],
14
15
  ['a b c', 'a c', "a <del class='diffdel'>b </del>c"],
15
- ['a b c', 'a <strong>b</strong> c', "a <strong><ins class='mod'>b</ins></strong> c"],
16
+ ['a b c', 'a <strong>b</strong> c', "a <strong><ins class='mod strong'>b</ins></strong> c"],
16
17
  ['a b c', 'a d c', "a <del class='diffmod'>b</del><ins class='diffmod'>d</ins> c"],
17
18
  ["<a title='xx'>test</a>", "<a title='yy'>test</a>", "<a title='yy'>test</a>"],
18
19
  ["<img src='logo.jpg'/>", '', "<del class='diffdel'><img src='logo.jpg'/></del>"],
@@ -30,7 +31,7 @@ describe('HtmlDiff', () => {
30
31
  [
31
32
  'This is a longer piece of text to ensure the new blocksize algorithm works',
32
33
  'This is a longer piece of text to <strong>ensure</strong> the new blocksize algorithm works decently',
33
- "This is a longer piece of text to <strong><ins class='mod'>ensure</ins></strong> the new blocksize algorithm works<ins class='diffins'>&nbsp;decently</ins>",
34
+ "This is a longer piece of text to <strong><ins class='mod strong'>ensure</ins></strong> the new blocksize algorithm works<ins class='diffins'>&nbsp;decently</ins>",
34
35
  ],
35
36
  [
36
37
  'By virtue of an agreement between xxx and the <b>yyy schools</b>, ...',
@@ -40,12 +41,12 @@ describe('HtmlDiff', () => {
40
41
  [
41
42
  'Some plain text',
42
43
  'Some <strong><i>plain</i></strong> text',
43
- "Some <strong><i><ins class='mod'>plain</ins></i></strong> text",
44
+ "Some <strong><i><ins class='mod strong i'>plain</ins></i></strong> text",
44
45
  ],
45
46
  [
46
47
  'Some <strong><i>formatted</i></strong> text',
47
48
  'Some formatted text',
48
- "Some <ins class='mod'>formatted</ins> text",
49
+ "Some <ins class='mod strong i'>formatted</ins> text",
49
50
  ],
50
51
  [
51
52
  '<table><tr><td>col1</td><td>col2</td></tr><tr><td>Data 1</td><td>Data 2</td></tr></table>',
@@ -55,7 +56,7 @@ describe('HtmlDiff', () => {
55
56
  [
56
57
  'text',
57
58
  '<span style="text-decoration: line-through;">text</span>',
58
- '<span style="text-decoration: line-through;"><ins class=\'mod\'>text</ins></span>',
59
+ '<span style="text-decoration: line-through;"><ins class=\'mod span\'>text</ins></span>',
59
60
  ],
60
61
 
61
62
  // TODO: Don't speak Chinese, this needs to be validated
@@ -145,35 +146,35 @@ describe('HtmlDiff', () => {
145
146
  [
146
147
  '<div class="dumb">Thiis a text without any sup-tags and other special things</div>',
147
148
  '<div class="dumb">Thiis a text <sup>1</sup>without any sup-tags and other special things</div>',
148
- "<div class=\"dumb\">Thiis a text <sup><ins class='mod'><ins class='diffins'>1</ins></ins></sup>without any sup-tags and other special things</div>",
149
+ "<div class=\"dumb\">Thiis a text <sup><ins class='mod sup'><ins class='diffins'>1</ins></ins></sup>without any sup-tags and other special things</div>",
149
150
  ],
150
151
 
151
152
  // Inserting a new word at the end of the reformatted text
152
153
  [
153
154
  '<span>text remains</span>',
154
155
  '<span><strong>text remains Test</strong></span>',
155
- "<span><strong><ins class='mod'>text remains<ins class='diffins'>&nbsp;Test</ins></ins></strong></span>",
156
+ "<span><strong><ins class='mod strong'>text remains<ins class='diffins'>&nbsp;Test</ins></ins></strong></span>",
156
157
  ],
157
158
 
158
159
  // Inserting a new word at the end of a reformatted text and another word at the end outside the reformatted text
159
160
  [
160
161
  '<span>text remains</span>',
161
162
  '<span><strong>text remains Test</strong> Test</span>',
162
- "<span><strong><ins class='mod'>text remains<ins class='diffins'>&nbsp;Test</ins></ins></strong><ins class='diffins'>&nbsp;Test</ins></span>",
163
+ "<span><strong><ins class='mod strong'>text remains<ins class='diffins'>&nbsp;Test</ins></ins></strong><ins class='diffins'>&nbsp;Test</ins></span>",
163
164
  ],
164
165
 
165
166
  // Twice reformatted text with an offset at the end
166
167
  [
167
168
  '<span>text remains</span>',
168
169
  '<span><strong><big>text </big>remains</strong></span>',
169
- "<span><strong><big><ins class='mod'>text </big>remains</ins></strong></span>",
170
+ "<span><strong><big><ins class='mod strong big'>text </big>remains</ins></strong></span>",
170
171
  ],
171
172
 
172
173
  // Inserting a new word at the beginning of a reformatted text.
173
174
  [
174
175
  '<span>text remains</span>',
175
176
  '<span><strong>Test text remains</strong></span>',
176
- "<span><strong><ins class='mod'><ins class='diffins'>Test </ins>text remains</ins></strong></span>",
177
+ "<span><strong><ins class='mod strong'><ins class='diffins'>Test </ins>text remains</ins></strong></span>",
177
178
  ],
178
179
  ] as const)('should handle missing closing tag in diff (%s, %s) -> %s', ([oldText, newText, expected]) => {
179
180
  expect(HtmlDiff.execute(oldText, newText)).toEqual(expected)
@@ -183,12 +184,12 @@ describe('HtmlDiff', () => {
183
184
  [
184
185
  '<p>The additional term only applies where 5 years have elapsed since the execution of this language.</p>',
185
186
  '<p>The additional term only applies where <strong><em>5 years</em></strong> have elapsed since the execution of this language.</p>',
186
- `<p>The additional term only applies where <strong><em><ins class='mod'>5 years</ins></em></strong> have elapsed since the execution of this language.</p>`,
187
+ `<p>The additional term only applies where <strong><em><ins class='mod strong em'>5 years</ins></em></strong> have elapsed since the execution of this language.</p>`,
187
188
  ],
188
189
  [
189
190
  '<p>The additional term only applies where 5 years have elapsed since the execution of this language.</p>',
190
191
  '<p>The additional term only applies where <em><strong>5 years</strong></em> have elapsed since the execution of this language.</p>',
191
- `<p>The additional term only applies where <em><strong><ins class='mod'>5 years</ins></strong></em> have elapsed since the execution of this language.</p>`,
192
+ `<p>The additional term only applies where <em><strong><ins class='mod em strong'>5 years</ins></strong></em> have elapsed since the execution of this language.</p>`,
192
193
  ],
193
194
  [
194
195
  '<p>The additional term only applies where 5 years have elapsed since the execution of this language.</p>',
@@ -198,12 +199,12 @@ describe('HtmlDiff', () => {
198
199
  [
199
200
  '<p>The additional term only applies where 5 years have elapsed since the execution of this language.</p>',
200
201
  '<p>The additional term only applies where [<strong><em>5 years</em></strong>] have elapsed since the execution of this language.</p>',
201
- `<p>The additional term only applies where <ins class='diffins'>[</ins><strong><em><ins class='mod'>5 years</ins></em></strong><ins class='diffins'>]</ins> have elapsed since the execution of this language.</p>`,
202
+ `<p>The additional term only applies where <ins class='diffins'>[</ins><strong><em><ins class='mod strong em'>5 years</ins></em></strong><ins class='diffins'>]</ins> have elapsed since the execution of this language.</p>`,
202
203
  ],
203
204
  [
204
205
  '<p>The additional term only applies where 5 years have elapsed since the execution of this language.</p>',
205
206
  '<p>The additional term only applies where [<em><strong>5 years</strong></em>] have elapsed since the execution of this language.</p>',
206
- `<p>The additional term only applies where <ins class='diffins'>[</ins><em><strong><ins class='mod'>5 years</ins></strong></em><ins class='diffins'>]</ins> have elapsed since the execution of this language.</p>`,
207
+ `<p>The additional term only applies where <ins class='diffins'>[</ins><em><strong><ins class='mod em strong'>5 years</ins></strong></em><ins class='diffins'>]</ins> have elapsed since the execution of this language.</p>`,
207
208
  ],
208
209
  ] as const)('should work when the comparison adds styling', ([oldText, newText, expected]) => {
209
210
  expect(HtmlDiff.execute(oldText, newText)).toEqual(expected)
@@ -213,12 +214,12 @@ describe('HtmlDiff', () => {
213
214
  [
214
215
  '<p>The additional term only applies where <strong><em>5 years</em></strong> have elapsed since the execution of this language.</p>',
215
216
  '<p>The additional term only applies where 5 years have elapsed since the execution of this language.</p>',
216
- `<p>The additional term only applies where <ins class='mod'>5 years</ins> have elapsed since the execution of this language.</p>`,
217
+ `<p>The additional term only applies where <ins class='mod strong em'>5 years</ins> have elapsed since the execution of this language.</p>`,
217
218
  ],
218
219
  [
219
220
  '<p>The additional term only applies where <em><strong>5 years</strong></em> have elapsed since the execution of this language.</p>',
220
221
  '<p>The additional term only applies where 5 years have elapsed since the execution of this language.</p>',
221
- `<p>The additional term only applies where <ins class='mod'>5 years</ins> have elapsed since the execution of this language.</p>`,
222
+ `<p>The additional term only applies where <ins class='mod em strong'>5 years</ins> have elapsed since the execution of this language.</p>`,
222
223
  ],
223
224
  [
224
225
  '<p>The additional term only applies where [5 years] have elapsed since the execution of this language.</p>',
@@ -228,24 +229,36 @@ describe('HtmlDiff', () => {
228
229
  [
229
230
  '<p>The additional term only applies where [<strong><em>5 years</em></strong>] have elapsed since the execution of this language.</p>',
230
231
  '<p>The additional term only applies where 5 years have elapsed since the execution of this language.</p>',
231
- `<p>The additional term only applies where <del class='diffdel'>[</del><ins class='mod'>5 years</ins><del class='diffdel'>]</del> have elapsed since the execution of this language.</p>`,
232
+ `<p>The additional term only applies where <del class='diffdel'>[</del><ins class='mod strong em'>5 years</ins><del class='diffdel'>]</del> have elapsed since the execution of this language.</p>`,
232
233
  ],
233
234
  [
234
235
  '<p>The additional term only applies where [<em><strong>5 years</strong></em>] have elapsed since the execution of this language.</p>',
235
236
  '<p>The additional term only applies where 5 years have elapsed since the execution of this language.</p>',
236
- `<p>The additional term only applies where <del class='diffdel'>[</del><ins class='mod'>5 years</ins><del class='diffdel'>]</del> have elapsed since the execution of this language.</p>`,
237
+ `<p>The additional term only applies where <del class='diffdel'>[</del><ins class='mod em strong'>5 years</ins><del class='diffdel'>]</del> have elapsed since the execution of this language.</p>`,
237
238
  ],
238
239
  [
239
240
  '<p>The additional term only applies where [<strong><em>5 years</em></strong>] have elapsed since the execution of this language.</p>',
240
241
  '<p>The additional term only applies where [<strong>5 years</strong>] have elapsed since the execution of this language.</p>',
241
- `<p>The additional term only applies where [<strong><ins class='mod'>5 years</ins></strong>] have elapsed since the execution of this language.</p>`,
242
+ `<p>The additional term only applies where [<strong><ins class='mod em'>5 years</ins></strong>] have elapsed since the execution of this language.</p>`,
242
243
  ],
243
244
  [
244
245
  '<p>The additional term only applies where <strong><em>5 years</em></strong> have elapsed since the execution of this language.</p>',
245
246
  '<p>The additional term only applies where [5 years] have elapsed since the execution of this language.</p>',
246
- `<p>The additional term only applies where <ins class='mod'><ins class='diffmod'>[</ins>5 years</ins><ins class='diffmod'>]</ins> have elapsed since the execution of this language.</p>`,
247
+ `<p>The additional term only applies where <ins class='mod strong em'><ins class='diffmod'>[</ins>5 years</ins><ins class='diffmod'>]</ins> have elapsed since the execution of this language.</p>`,
247
248
  ],
248
249
  ] as const)('should work when the comparison removes styling', ([oldText, newText, expected]) => {
249
250
  expect(HtmlDiff.execute(oldText, newText)).toEqual(expected)
250
251
  })
252
+
253
+ it(
254
+ 'should calculate a diff for a real-world example within 5s',
255
+ { timeout: process.env.CI ? 15_000 : 5_000 }, // CI is slower so give it more time
256
+ async () => {
257
+ const oldText = await fs.readFile('test/input1.html', 'utf-8')
258
+ const newText = await fs.readFile('test/input2.html', 'utf-8')
259
+ const expected = await fs.readFile('test/expected.html', 'utf-8')
260
+
261
+ expect(HtmlDiff.execute(oldText, newText)).toEqual(expected)
262
+ }
263
+ )
251
264
  })