Skip to content
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

fix: detect role=heading for outline #309

Merged
merged 3 commits into from
Apr 7, 2020
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
7 changes: 5 additions & 2 deletions packages/ace-core/src/scripts/ace-extraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ ace.getHTMLOutline = function() {
}

ace.getHeadings = function() {
let hxElems = document.querySelectorAll('h1, h2, h3, h4, h5, h6');
let hxElems = document.querySelectorAll('h1, h2, h3, h4, h5, h6, [role="heading"]');
let headings = [];
// FIXME filter headings in sectioning roots
// function findSectioningRoot (el, cls) {
Expand All @@ -136,9 +136,12 @@ ace.getHeadings = function() {
// }

hxElems.forEach(function(hx) {
let level = +hx.localName.slice(1);
if (Number.isNaN(level)) level = +hx.getAttribute('aria-level');
if (!Number.isInteger(level) || level < 1) level = 2; // NOTE aria-level fallback value per ARIA spec is 2 (and avoid Infinity)
headings.push({
html: hx.textContent,
level: +hx.localName.slice(1)
level: level
});
});

Expand Down
28 changes: 28 additions & 0 deletions packages/ace-core/src/scripts/ace-extraction.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,34 @@ describe('extracting headings', () => {
expect(results[0].html).toBe('title 1 link');
expect(results[0].level).toBe(1);
});

test('role=heading with aria-level', async () => {
const results = await run('getHeadings', '<div role="heading" aria-level="1">title 1</div>');
expect(results.length).toBe(1);
expect(results[0].html).toBe('title 1');
expect(results[0].level).toBe(1);
});

test('role=heading without aria-level', async () => {
const results = await run('getHeadings', '<div role="heading">title 1</div>');
expect(results.length).toBe(1);
expect(results[0].html).toBe('title 1');
expect(results[0].level).toBe(2);
});

test('role=heading with negative aria-level', async () => {
const results = await run('getHeadings', '<div role="heading" aria-level="-1">title 1</div>');
expect(results.length).toBe(1);
expect(results[0].html).toBe('title 1');
expect(results[0].level).toBe(2);
});

test('role=heading with infinite aria-level', async () => {
const results = await run('getHeadings', '<div role="heading" aria-level="Infinity">title 1</div>');
expect(results.length).toBe(1);
expect(results[0].html).toBe('title 1');
expect(results[0].level).toBe(2);
});
});

describe('extracting iframes', () => {
Expand Down