Skip to content

fix: fix id with pure number. #2021

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/core/event/scroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)];
Expand Down
25 changes: 20 additions & 5 deletions test/unit/example.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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>`;
Expand Down Expand Up @@ -53,7 +53,7 @@ describe(`Example Tests`, function() {
});
});

describe('Fake Timers', function() {
describe('Fake Timers', function () {
// jest version issue
// test('data & time', () => {
// jest.useFakeTimers();
Expand All @@ -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');
Expand All @@ -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;
Expand Down Expand Up @@ -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);
});
});
});