Skip to content

Commit e3e3123

Browse files
authored
Create collapse-href.spec.js
1 parent df81b82 commit e3e3123

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Node built-in test (no mocha) – Collapse data-api
2+
const test = require('node:test');
3+
const assert = require('node:assert/strict');
4+
5+
const EXPECT_FAST = process.env.EXPECT_FAST === '1';
6+
7+
test('Collapse data-api href sanitize timing', () => {
8+
const handlers = Object.create(null);
9+
function wrap(raw) {
10+
return {
11+
on(eventName, selectorOrHandler, maybeHandler) {
12+
const h = typeof maybeHandler === 'function' ? maybeHandler
13+
: (typeof selectorOrHandler === 'function' ? selectorOrHandler : null);
14+
if (eventName && h) handlers[eventName] = h;
15+
return this;
16+
},
17+
each() { return this; }, // 避免深入插件逻辑
18+
data() { return {}; },
19+
attr(name) {
20+
if (!raw) return null;
21+
if (name === 'href') return raw._href || null;
22+
if (name === 'data-target') return raw._dt || null;
23+
return null;
24+
},
25+
find() { return { hasClass: () => false }; }
26+
};
27+
}
28+
function $(x) { return wrap(x); }
29+
$.fn = { jquery: '3.4.1' };
30+
$.extend = Object.assign;
31+
global.window = global;
32+
global.document = {};
33+
global.jQuery = global.$ = $;
34+
35+
require('../dist/js/bootstrap.js');
36+
37+
const click = handlers['click.bs.collapse.data-api'] || handlers['click.bs.collapse'];
38+
assert.equal(typeof click, 'function', 'failed to capture collapse handler');
39+
40+
const N = 100000;
41+
const cases = [
42+
{ name: 'nul', s: '\u0000'.repeat(N) + '\u0000' },
43+
{ name: 'digits\\n@', s: '1'.repeat(N) + '\n@' },
44+
//{ name: '=#...@\\r', s: '=' + '#'.repeat(N) + '@\r' },
45+
//{ name: '=#*@\\r', s: '=#'.repeat(N) + '@\r' },
46+
];
47+
48+
for (const { name, s } of cases) {
49+
const t0 = Date.now();
50+
try { click.call({ _href: s, _dt: null }, { preventDefault(){} }); } catch {}
51+
const ms = Date.now() - t0;
52+
console.log(`[collapse] ${name.padEnd(9)} len=${s.length} -> ${ms} ms`);
53+
if (EXPECT_FAST) assert.ok(ms < 2000, `too slow: ${ms}ms`);
54+
}
55+
});

0 commit comments

Comments
 (0)