aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2024-11-26 23:10:45 +0800
committerGitHub <noreply@github.com>2024-11-26 23:10:45 +0800
commit722e703c6bc4df27f23a985b16f68e6c89e048c1 (patch)
tree7c07863f594303f3ddbaecfdac21a0858bd052d3
parent88f5d33ab267f330ffaf02eb019e772ed06ed34f (diff)
downloadgitea-722e703c6bc4df27f23a985b16f68e6c89e048c1.tar.gz
gitea-722e703c6bc4df27f23a985b16f68e6c89e048c1.zip
Bypass vitest bug (#32647)
-rw-r--r--web_src/js/utils/dom.test.ts8
-rw-r--r--web_src/js/utils/dom.ts5
2 files changed, 12 insertions, 1 deletions
diff --git a/web_src/js/utils/dom.test.ts b/web_src/js/utils/dom.test.ts
index cb99a85511..6e71596850 100644
--- a/web_src/js/utils/dom.test.ts
+++ b/web_src/js/utils/dom.test.ts
@@ -1,4 +1,4 @@
-import {createElementFromAttrs, createElementFromHTML, querySingleVisibleElem} from './dom.ts';
+import {createElementFromAttrs, createElementFromHTML, queryElemChildren, querySingleVisibleElem} from './dom.ts';
test('createElementFromHTML', () => {
expect(createElementFromHTML('<a>foo<span>bar</span></a>').outerHTML).toEqual('<a>foo<span>bar</span></a>');
@@ -26,3 +26,9 @@ test('querySingleVisibleElem', () => {
el = createElementFromHTML('<div><span>foo</span><span>bar</span></div>');
expect(() => querySingleVisibleElem(el, 'span')).toThrowError('Expected exactly one visible element');
});
+
+test('queryElemChildren', () => {
+ const el = createElementFromHTML('<div><span class="a">a</span><span class="b">b</span></div>');
+ const children = queryElemChildren(el, '.a');
+ expect(children.length).toEqual(1);
+});
diff --git a/web_src/js/utils/dom.ts b/web_src/js/utils/dom.ts
index a4c7c0e4c6..da9ce71644 100644
--- a/web_src/js/utils/dom.ts
+++ b/web_src/js/utils/dom.ts
@@ -76,6 +76,11 @@ export function queryElemSiblings<T extends Element>(el: Element, selector = '*'
// it works like jQuery.children: only the direct children are selected
export function queryElemChildren<T extends Element>(parent: Element | ParentNode, selector = '*', fn?: ElementsCallback<T>): ArrayLikeIterable<T> {
+ if (window.vitest) {
+ // bypass the vitest bug: it doesn't support ":scope >"
+ const selected = Array.from<T>(parent.children as any).filter((child) => child.matches(selector));
+ return applyElemsCallback<T>(selected, fn);
+ }
return applyElemsCallback<T>(parent.querySelectorAll(`:scope > ${selector}`), fn);
}