Skip to content

Commit 3a93c36

Browse files
committed
refactor: pre-handle white space
1 parent 3f123c6 commit 3a93c36

File tree

7 files changed

+53
-23
lines changed

7 files changed

+53
-23
lines changed

build/rollup.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import rollupPostcss from 'rollup-plugin-postcss'
99
const resolve = file => path.join(import.meta.dirname, '../packages/html-diff', file)
1010

1111
const dtsTask = {
12-
input: resolve('dist-ts/index.d.ts'),
12+
input: resolve('dist-ts/src/index.d.ts'),
1313
output: {
1414
file: resolve('dist/index.d.ts'),
1515
format: 'es',

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"devDependencies": {
1010
"@rollup/plugin-node-resolve": "^15.3.0",
1111
"@rollup/plugin-typescript": "^12.1.1",
12+
"@types/node": "^20.17.6",
1213
"chalk": "^5.3.0",
1314
"enquirer": "^2.4.1",
1415
"postcss": "^8.4.49",

packages/html-diff/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ export default class HtmlDiff {
110110
...classNames,
111111
},
112112
}
113+
// white space is junk
114+
oldHtml = oldHtml.trim()
115+
newHtml = newHtml.trim()
113116

114117
// no need to diff
115118
if (oldHtml === newHtml) {

packages/html-diff/tests/__snapshots__/index.spec.ts.snap

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,12 @@ exports[`HtmlDiff > should work with equal start 2`] = `
155155
<p data-seq="2"><span class="html-diff-create-text-wrapper">今天天气很不错</span></p>",
156156
]
157157
`;
158+
159+
exports[`HtmlDiff > should work with white space 1`] = `"<div>hello</div>"`;
160+
161+
exports[`HtmlDiff > should work with white space 2`] = `
162+
[
163+
"<div data-seq="1">hello</div>",
164+
"<div data-seq="1">hello</div>",
165+
]
166+
`;

packages/html-diff/tests/index.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ import { describe, expect, it } from 'vitest'
22
import HtmlDiff from '../src'
33

44
describe('HtmlDiff', () => {
5+
it('should work with white space', function () {
6+
const oldHtml = `<div>hello</div>`
7+
const newHtml = ` <div>hello </div>`
8+
const diff = new HtmlDiff(oldHtml, newHtml)
9+
expect(diff.getUnifiedContent()).toMatchSnapshot()
10+
expect(diff.getSideBySideContents()).toMatchSnapshot()
11+
})
12+
513
it('should work with basic create', function () {
614
const oldHtml = `<div>hello</div>`
715
const newHtml = `<div>hello world</div>`

packages/html-diff/tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"compilerOptions": {
33
"target": "esnext",
44
"module": "esnext",
5-
"moduleResolution": "node",
5+
"moduleResolution": "bundler",
66
"resolveJsonModule": true,
77
"esModuleInterop": true,
88
"strict": true,
@@ -15,5 +15,5 @@
1515
"strictNullChecks": true,
1616
"allowUnreachableCode": false
1717
},
18-
"include": ["src/**/*.ts", "tests/**/*.test.ts"]
18+
"include": ["src/**/*.ts", "tests/**/*.spec.ts"]
1919
}

pnpm-lock.yaml

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

0 commit comments

Comments
 (0)