Fix suggestions for issues (#32380)

This commit is contained in:
wxiaoguang 2024-10-31 04:06:36 +08:00 committed by GitHub
parent f4d3aaeeb9
commit a4a121c684
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 82 additions and 88 deletions

View file

@ -8,11 +8,11 @@ test('createElementFromAttrs', () => {
const el = createElementFromAttrs('button', {
id: 'the-id',
class: 'cls-1 cls-2',
'data-foo': 'the-data',
disabled: true,
checked: false,
required: null,
tabindex: 0,
});
expect(el.outerHTML).toEqual('<button id="the-id" class="cls-1 cls-2" data-foo="the-data" disabled="" tabindex="0"></button>');
'data-foo': 'the-data',
}, 'txt', createElementFromHTML('<span>inner</span>'));
expect(el.outerHTML).toEqual('<button id="the-id" class="cls-1 cls-2" disabled="" tabindex="0" data-foo="the-data">txt<span>inner</span></button>');
});

View file

@ -298,22 +298,24 @@ export function replaceTextareaSelection(textarea: HTMLTextAreaElement, text: st
}
// Warning: Do not enter any unsanitized variables here
export function createElementFromHTML(htmlString: string) {
export function createElementFromHTML(htmlString: string): HTMLElement {
const div = document.createElement('div');
div.innerHTML = htmlString.trim();
return div.firstChild as Element;
return div.firstChild as HTMLElement;
}
export function createElementFromAttrs(tagName: string, attrs: Record<string, any>) {
export function createElementFromAttrs(tagName: string, attrs: Record<string, any>, ...children: (Node|string)[]): HTMLElement {
const el = document.createElement(tagName);
for (const [key, value] of Object.entries(attrs)) {
for (const [key, value] of Object.entries(attrs || {})) {
if (value === undefined || value === null) continue;
if (typeof value === 'boolean') {
el.toggleAttribute(key, value);
} else {
el.setAttribute(key, String(value));
}
// TODO: in the future we could make it also support "textContent" and "innerHTML" properties if needed
}
for (const child of children) {
el.append(child instanceof Node ? child : document.createTextNode(child));
}
return el;
}

View file

@ -1,6 +1,6 @@
import emojis from '../../../assets/emoji.json';
import type {Issue} from '../features/issue.ts';
import {GET} from '../modules/fetch.ts';
import type {Issue} from '../features/issue.ts';
const maxMatches = 6;
@ -49,8 +49,8 @@ export async function matchIssue(owner: string, repo: string, issueIndexStr: str
const res = await GET(`${window.config.appSubUrl}/${owner}/${repo}/issues/suggestions?q=${encodeURIComponent(query)}`);
const issues: Issue[] = await res.json();
const issueIndex = parseInt(issueIndexStr);
const issueNumber = parseInt(issueIndexStr);
// filter out issue with same id
return issues.filter((i) => i.id !== issueIndex);
return issues.filter((i) => i.number !== issueNumber);
}