Skip to content

Commit 3abfb8c

Browse files
meet-studentzombieJthinkasany
authored
chore: ci fix to remove parse5 (ant-design#56421)
* chore: ci fix * chore: update * chore: update * chore: fix lint * chore: remove outdated parse5 dependencies from package.json --------- Co-authored-by: 二货机器人 <smith3816@gmail.com> Co-authored-by: thinkasany <480968828@qq.com>
1 parent 38efd01 commit 3abfb8c

2 files changed

Lines changed: 75 additions & 10 deletions

File tree

package.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
"test:image": "cross-env MOCK_USE_ID=false jest --config .jest.image.js --no-cache -i -u --forceExit",
9090
"test:node": "npm run version && jest --config .jest.node.js --no-cache",
9191
"test:package-diff": "antd-tools run package-diff",
92-
"test:site": "jest --config .jest.site.js",
92+
"test:site": "jest --config .jest.site.js --no-cache",
9393
"test:site-update": "npm run site && npm run test:site -- -u",
9494
"test:update": "jest --config .jest.js --no-cache -u",
9595
"test:visual-regression": "tsx scripts/visual-regression/build.ts",
@@ -228,14 +228,14 @@
228228
"antd-token-previewer": "^3.0.0",
229229
"axios": "^1.13.2",
230230
"chalk": "^5.6.2",
231-
"cheerio": "^1.1.2",
232231
"circular-dependency-plugin": "^5.2.2",
233232
"cli-progress": "^3.12.0",
234233
"cross-env": "^10.1.0",
235234
"css-tree": "^3.1.0",
236235
"csstree-validator": "^4.0.1",
237236
"cypress-image-diff-html-report": "2.2.0",
238237
"dekko": "^0.2.1",
238+
"domparser-rs": "0.0.7",
239239
"dotenv": "^17.2.3",
240240
"dumi": "~2.4.21",
241241
"dumi-plugin-color-chunk": "^2.1.0",
@@ -284,9 +284,6 @@
284284
"ora": "^9.0.0",
285285
"p-all": "^5.0.1",
286286
"package-manager-detector": "^1.6.0",
287-
"parse5": "8.0.0",
288-
"parse5-htmlparser2-tree-adapter": "8.0.0",
289-
"parse5-parser-stream": "8.0.0",
290287
"pngjs": "^7.0.0",
291288
"portfinder": "^1.0.38",
292289
"prettier": "^3.7.4",

scripts/check-site.ts

Lines changed: 73 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
/* eslint-disable unicorn/prefer-dom-node-text-content */
2+
13
import type http from 'http';
24
import type https from 'https';
35
import { join } from 'path';
4-
import { load } from 'cheerio';
6+
import { DOMParser } from 'domparser-rs';
57
import { globSync } from 'glob';
68
import { createServer } from 'http-server';
79
import fetch from 'isomorphic-fetch';
@@ -23,8 +25,56 @@ describe('site test', () => {
2325
const port = await portPromise;
2426
const resp = await fetch(`http://127.0.0.1:${port}${path}`).then(async (res) => {
2527
const html: string = await res.text();
26-
const $ = load(html, { xml: true });
27-
return { status: res.status, $ };
28+
const root = new DOMParser().parseFromString(html, 'text/html');
29+
function getTextContent(node: any): string {
30+
if (!node) return '';
31+
if (typeof node.textContent === 'string') return node.textContent.trim();
32+
if (typeof node.innerText === 'string') return node.innerText.trim();
33+
// Fallback: recursively get text from children
34+
if (node.children && node.children.length > 0) {
35+
return Array.from(node.children)
36+
.map((child: any) => getTextContent(child))
37+
.join('')
38+
.trim();
39+
}
40+
return '';
41+
}
42+
function wrap(nodes: any[]) {
43+
const list = Array.isArray(nodes) ? nodes : [];
44+
return {
45+
length: list.length,
46+
text: () => {
47+
if (list.length === 0) return '';
48+
return list.map((n) => getTextContent(n)).join('');
49+
},
50+
first: () => wrap(list.slice(0, 1)),
51+
};
52+
}
53+
const $ = (selector: string) => {
54+
if (!root.querySelector) {
55+
console.warn('DOMParser does not support querySelector');
56+
return wrap([]);
57+
}
58+
59+
// Handle complex selectors that domparser-rs might not support
60+
if (selector === '.markdown table') {
61+
// Find all .markdown elements and then find tables within them
62+
const markdownElements = root.querySelectorAll('.markdown');
63+
const tables = [];
64+
for (const markdown of Array.from(markdownElements)) {
65+
const tablesInMarkdown = markdown.querySelectorAll('table');
66+
tables.push(...Array.from(tablesInMarkdown));
67+
}
68+
return wrap(tables);
69+
} else {
70+
// Use querySelectorAll for simple selectors
71+
const elements = root.querySelectorAll(selector);
72+
const elementsArray = Array.from(elements);
73+
return wrap(elementsArray);
74+
}
75+
};
76+
77+
return { status: res.status, $, root };
2878
});
2979
return resp;
3080
};
@@ -35,9 +85,27 @@ describe('site test', () => {
3585
};
3686

3787
const expectComponent = async (component: string) => {
38-
const { status, $ } = await render(`/${component}/`);
88+
const { status, $, root } = await render(`/${component}/`);
3989
expect(status).toBe(200);
40-
expect($('h1').text().toLowerCase()).toMatch(handleComponentName(component));
90+
91+
// Get all h1 elements and find the one in main content (not in header)
92+
const h1Elements = root.querySelectorAll('h1');
93+
let mainH1Text = '';
94+
95+
if (h1Elements.length >= 2) {
96+
// The second h1 should be the main content title
97+
const mainH1 = h1Elements[1];
98+
mainH1Text = mainH1.textContent || (mainH1 as any).innerText || '';
99+
} else if (h1Elements.length === 1) {
100+
// If only one h1, check its content
101+
const h1 = h1Elements[0];
102+
mainH1Text = h1.textContent || (h1 as any).innerText || '';
103+
}
104+
105+
// Clean up the text and extract the main component name
106+
mainH1Text = mainH1Text.trim();
107+
108+
expect(mainH1Text.toLowerCase()).toMatch(handleComponentName(component));
41109

42110
/**
43111
* 断言组件的 api table 数量是否符合预期。

0 commit comments

Comments
 (0)