Skip to content

Commit

Permalink
fix: detect role=heading for outline
Browse files Browse the repository at this point in the history
Enables ace-extraction's getHeadings() to detect headings marked
via role=heading; uses aria-level or the spec fallback.

Fixes #308
  • Loading branch information
pkra committed Mar 3, 2020
1 parent 1bc9296 commit 9831cb2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
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.isNaN(level)) level = 2; // NOTE aria-level fallback value per ARIA spec
headings.push({
html: hx.textContent,
level: +hx.localName.slice(1)
level: level
});
});

Expand Down
14 changes: 14 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,20 @@ 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</h1>');
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</h1>');
expect(results.length).toBe(1);
expect(results[0].html).toBe('title 1');
expect(results[0].level).toBe(2);
});
});

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

0 comments on commit 9831cb2

Please sign in to comment.