Skip to content

Commit 62848a9

Browse files
committed
fix tests
1 parent 1109665 commit 62848a9

File tree

8 files changed

+57
-238
lines changed

8 files changed

+57
-238
lines changed

dist/htmldiff.js

Lines changed: 1 addition & 74 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/htmldiff.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 0 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222
],
2323
"type": "module",
2424
"types": "dist/htmldiff.d.ts",
25-
"dependencies": {
26-
"xregexp": "^5.0.2"
27-
},
25+
"dependencies": {},
2826
"devDependencies": {
2927
"@typescript-eslint/eslint-plugin": "~4.16.1",
3028
"@typescript-eslint/parser": "~4.16.1",

src/htmldiff.ts

Lines changed: 1 addition & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@
2626
* == '<p>this is some <ins class="diff-class">more </ins>text</p>'
2727
*/
2828

29-
import XRegExp from 'xregexp';
30-
31-
const unicodeLetterExpr = XRegExp('\\p{L}|\\d');
32-
3329
function isEndOfTag(char: string): boolean {
3430
return char === '>';
3531
}
@@ -105,7 +101,6 @@ function isEndOfAtomicTag(word: string, tag: string){
105101
}
106102

107103
const styleTagsRegExp = /^<(strong|em|b|i|q|cite|blockquote|mark|dfn|sup|sub|u|s|nobr)(^(?!\w)|>)/;
108-
const styleTagsClosingRegExp = /<\/(strong|em|b|i|q|cite|blockquote|mark|dfn|sup|sub|u|s|nobr)(^(?!\w)|>)/;
109104

110105
/**
111106
* Checks if the current word is the beginning of an style tag. An style tag is one whose
@@ -300,7 +295,7 @@ export function htmlToTokens(html: string): Token[] {
300295
}
301296
currentWord = char;
302297
mode = 'whitespace';
303-
} else if (unicodeLetterExpr.test(char)){
298+
} else if (/[\w\d#@]/.test(char)){
304299
currentWord += char;
305300
} else if (/&/.test(char)){
306301
if (currentWord){
@@ -389,19 +384,6 @@ function getKeyForToken(token: string){
389384
return `<iframe src="${iframe[1]}"></iframe>`;
390385
}
391386

392-
// strip style tags from token key
393-
if (styleTagsRegExp.test(token) || styleTagsClosingRegExp.test(token)) {
394-
return token
395-
.replace(styleTagsRegExp, '')
396-
.replace(styleTagsClosingRegExp, '')
397-
.replace(/(\s+|&nbsp;|&#160;)/g, ' ');
398-
}
399-
// if (styleTag) {
400-
// const styleTagClose = styleTagsClosingRegExp.exec(token);
401-
// const currentToken = (styleTagClose && styleTagClose[0]) ? token.split(styleTagClose[0])[0] ?? token : token;
402-
// return currentToken.replace('<nobr>', '');
403-
// }
404-
405387
// If the token is any other element, just grab the tag name.
406388
const tagName = /<([^\s>]+)[\s>]/.exec(token);
407389
if (tagName){
@@ -932,67 +914,6 @@ function combineTokenNotes(
932914
return segments.map(mapFn).join('');
933915
}
934916

935-
// var nobrOpenRegExp = /<nobr>/g;
936-
// var nobrCloseRegExp = /<\/nobr>/g;
937-
938-
/**
939-
* Ensures that a string does not have a truncated <nobr> tag.
940-
*/
941-
// function wrapNobr(content) {
942-
// console.log(content)
943-
// var currentContent = content
944-
// var currentOpenTags = 0;
945-
// var isInTag = false;
946-
// var currentTag = "";
947-
// var shouldAddStartTag = false;
948-
// for (var charIdx = 0; charIdx < content.length; charIdx++) {
949-
// var char = content[charIdx];
950-
// if (isInTag) {
951-
// currentTag += char;
952-
// if (char === ">") {
953-
// if (nobrOpenRegExp.test(currentTag)) {
954-
// currentOpenTags += 1;
955-
// } else if (nobrCloseRegExp.test(currentTag)) {
956-
// currentOpenTags -= 1;
957-
// if (currentOpenTags < 0) {
958-
// shouldAddStartTag = true;
959-
// currentOpenTags = 0;
960-
// }
961-
// }
962-
// currentTag = "";
963-
// isInTag = false;
964-
// }
965-
// } else if (char === "<") {
966-
// isInTag = true;
967-
// currentTag += char;
968-
// }
969-
// }
970-
// if (currentOpenTags > 0) {
971-
// currentContent = content + "</nobr>";
972-
// }
973-
// if (shouldAddStartTag) {
974-
// currentContent = "<nobr>" + currentContent;
975-
// }
976-
// console.log(currentContent);
977-
// return currentContent;
978-
// }
979-
980-
// /**
981-
// * Ensures that a string does not have a truncated <nobr> tag.
982-
// */
983-
// function wrapNobr(content: string) {
984-
// const nobrOpenRegExp = /<nobr>/g;
985-
// const nobrCloseRegExp = /<\/nobr>/g;
986-
// const openNobr = content.match(nobrOpenRegExp) || [];
987-
// const closeNobr = content.match(nobrCloseRegExp) || [];
988-
// if (openNobr.length > closeNobr.length) {
989-
// return content + '</nobr>';
990-
// } else if (closeNobr.length > openNobr.length) {
991-
// return '<nobr>' + content;
992-
// }
993-
// return content;
994-
// }
995-
996917
function arrayDiff(a1: string[], a2: string[]) {
997918
let beforeArray: string[] = [];
998919
let afterArray: string[] = [];

test/find_matching_blocks.spec.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import {htmlToTokens, createToken, createSegment, findBestMatch, findMatchingBlocks, createMap} from "../dist/htmldiff.js";
1+
import {createMap, createSegment, createToken, findBestMatch, findMatchingBlocks, htmlToTokens} from '../dist/htmldiff.js';
22

33
describe('findMatchingBlocks', function(){
4-
var cut, res, tokenize;
4+
let cut, res, tokenize;
55

66
beforeEach(function(){
77
tokenize = function(tokens){
88
return tokens.map(function(token){
9-
return createToken(token);
9+
return createToken(token, []);
1010
});
1111
};
1212
});
@@ -25,40 +25,40 @@ describe('findMatchingBlocks', function(){
2525
res = cut(tokenize(['a', 'apple', 'has', 'a', 'worm']));
2626
});
2727

28-
it('should find "a" twice', function(){
29-
expect(res['a'].length).to.equal(2);
28+
it('should find "a[]" twice', function(){
29+
expect(res['a[]'].length).to.equal(2);
3030
});
3131

3232
it('should find "a" at 0', function(){
33-
expect(res['a'][0]).to.equal(0);
33+
expect(res['a[]'][0]).to.equal(0);
3434
});
3535

3636
it('should find "a" at 3', function(){
37-
expect(res['a'][1]).to.equal(3);
37+
expect(res['a[]'][1]).to.equal(3);
3838
});
3939

4040
it('should find "has" at 2', function(){
41-
expect(res['has'][0]).to.equal(2);
41+
expect(res['has[]'][0]).to.equal(2);
4242
});
4343
});
4444
});
4545

4646
describe('findBestMatch', function(){
47-
var invoke;
47+
let invoke;
4848

4949
beforeEach(function(){
5050
cut = findBestMatch;
5151
invoke = function(before, after){
52-
var segment = createSegment(before, after, 0, 0);
52+
const segment = createSegment(before, after, 0, 0);
5353

5454
res = cut(segment);
5555
};
5656
});
5757

5858
describe('When there is a match', function(){
5959
beforeEach(function(){
60-
var before = tokenize(['a', 'dog', 'bites']);
61-
var after = tokenize(['a', 'dog', 'bites', 'a', 'man']);
60+
const before = tokenize(['a', 'dog', 'bites']);
61+
const after = tokenize(['a', 'dog', 'bites', 'a', 'man']);
6262
invoke(before, after);
6363
});
6464

@@ -90,8 +90,8 @@ describe('findMatchingBlocks', function(){
9090

9191
describe('When there is no match', function(){
9292
beforeEach(function(){
93-
var before = tokenize(['the', 'rat', 'sqeaks']);
94-
var after = tokenize(['a', 'dog', 'bites', 'a', 'man']);
93+
const before = tokenize(['the', 'rat', 'sqeaks']);
94+
const after = tokenize(['a', 'dog', 'bites', 'a', 'man']);
9595
invoke(before, after);
9696
});
9797

@@ -102,7 +102,7 @@ describe('findMatchingBlocks', function(){
102102
});
103103

104104
describe('findMatchingBlocks', function(){
105-
var segment;
105+
let segment;
106106

107107
beforeEach(function(){
108108
cut = findMatchingBlocks;
@@ -114,8 +114,8 @@ describe('findMatchingBlocks', function(){
114114

115115
describe('When called with a single match', function(){
116116
beforeEach(function(){
117-
var before = htmlToTokens('a dog bites');
118-
var after = htmlToTokens('when a dog bites it hurts');
117+
const before = htmlToTokens('a dog bites');
118+
const after = htmlToTokens('when a dog bites it hurts');
119119
segment = createSegment(before, after, 0, 0);
120120

121121
res = cut(segment);
@@ -128,8 +128,8 @@ describe('findMatchingBlocks', function(){
128128

129129
describe('When called with multiple matches', function(){
130130
beforeEach(function(){
131-
var before = htmlToTokens('the dog bit a man');
132-
var after = htmlToTokens('the large brown dog bit a tall man');
131+
const before = htmlToTokens('the dog bit a man');
132+
const after = htmlToTokens('the large brown dog bit a tall man');
133133
segment = createSegment(before, after, 0, 0);
134134
res = cut(segment);
135135
});

0 commit comments

Comments
 (0)