fix: fix id with pure number. (#2021)

This commit is contained in:
Koy ['kɔɪ] 2023-04-22 17:53:17 +08:00 committed by GitHub
parent c5ec51090e
commit f4f21a3f74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 6 deletions

View File

@ -147,7 +147,9 @@ export function scrollIntoView(path, id) {
return;
}
const topMargin = config().topMargin;
const section = dom.find('#' + id);
// Use [id='1234'] instead of #id to handle special cases such as reserved characters and pure number id
// https://stackoverflow.com/questions/37270787/uncaught-syntaxerror-failed-to-execute-queryselector-on-document
const section = dom.find("[id='" + id + "']");
section && scrollTo(section, topMargin);
const li = nav[getNavKey(path, id)];

View File

@ -9,10 +9,10 @@ import * as getTimeOfDayModule from './fixtures/get-time-of-day.js';
// Suite
// -----------------------------------------------------------------------------
describe(`Example Tests`, function() {
describe(`Example Tests`, function () {
// Tests
// ---------------------------------------------------------------------------
describe('Jest & JSDOM basics', function() {
describe('Jest & JSDOM basics', function () {
test('dom manipulation (jsdom)', () => {
const testText = 'This is a test';
const testHTML = `<h1>Test</h1><p>${testText}</p>`;
@ -53,7 +53,7 @@ describe(`Example Tests`, function() {
});
});
describe('Fake Timers', function() {
describe('Fake Timers', function () {
// jest version issue
// test('data & time', () => {
// jest.useFakeTimers();
@ -63,7 +63,7 @@ describe(`Example Tests`, function() {
// });
});
describe('Mocks & Spies', function() {
describe('Mocks & Spies', function () {
test('mock import/require dependency using jest.fn()', () => {
const testModule = require('./fixtures/get-time-of-day.js');
const { greet: testGreet } = require('./fixtures/greet.js');
@ -82,7 +82,7 @@ describe(`Example Tests`, function() {
jest.doMock(mockModulePath, () => ({
__esModule: true,
getTimeOfDay: jest.fn(() => 'night')
getTimeOfDay: jest.fn(() => 'night'),
}));
const mockGetTimeOfDay = require(mockModulePath).getTimeOfDay;
@ -116,4 +116,19 @@ describe(`Example Tests`, function() {
expect(greeting).toBe(`Good night, John!`);
});
});
describe('Verify Special Changes Test Case', function () {
test('document.querySelector with id=pure number', () => {
const testText = 'This is a test';
const testHTML = `<div id=24><p>${testText}</p></div>`;
// Inject HTML
document.body.innerHTML = testHTML;
expect(() => {
document.querySelector('#24');
}).toThrow(DOMException);
expect(document.querySelector("[id='24']").textContent).toBe(testText);
});
});
});