Skip to content

Commit

Permalink
Replace textContent with innerText
Browse files Browse the repository at this point in the history
Prevents hidden child nodes, like <style> elements or labels for assistive technology, to appear in the table of contents.
  • Loading branch information
paulborm authored Nov 22, 2023
1 parent 7159dcd commit 2639937
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/js/build-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ module.exports = function (options) {
}

if (options.includeTitleTags) {
a.setAttribute('title', data.textContent)
a.setAttribute('title', data.innerText)
}

if (options.includeHtml && data.childNodes.length) {
Expand All @@ -91,7 +91,7 @@ module.exports = function (options) {
})
} else {
// Default behavior.
a.textContent = data.textContent
a.innerText = data.innerText
}
a.setAttribute('href', options.basePath + '#' + data.id)
a.setAttribute('class', options.linkClass +
Expand Down
2 changes: 1 addition & 1 deletion src/js/parse-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ module.exports = function parseContent (options) {
}

const headingLabel = heading.getAttribute('data-heading-label') ||
(options.headingLabelCallback ? String(options.headingLabelCallback(heading.textContent)) : heading.textContent.trim())
(options.headingLabelCallback ? String(options.headingLabelCallback(heading.innerText)) : heading.innerText.trim())
var obj = {
id: heading.id,
children: [],
Expand Down
2 changes: 1 addition & 1 deletion src/utils/make-ids.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function makeIds () { // eslint-disable-line
Array.prototype.forEach.call(headings, function (heading) {
var id = heading.id
? heading.id
: heading.textContent.trim().toLowerCase().replace(/\s+/g, '-').replace(/[^a-z0-9-]/g, '')
: heading.innerText.trim().toLowerCase().replace(/\s+/g, '-').replace(/[^a-z0-9-]/g, '')
headingMap[id] = !isNaN(headingMap[id]) ? ++headingMap[id] : 0
if (headingMap[id]) {
heading.id = id + '-' + headingMap[id]
Expand Down
10 changes: 5 additions & 5 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ before(function (done) {
GLOBAL.window = window
window.document.body.innerHTML = content
const scriptEl = window.document.createElement("script")
scriptEl.textContent = jsContent
scriptEl.innerText = jsContent
window.document.body.appendChild(scriptEl)
tocbot = window.tocbot

Expand Down Expand Up @@ -117,7 +117,7 @@ describe('Parse content', function () {
var contentEl = GLOBAL.window.document.querySelector(tocbot.options.contentSelector)
var defaultHeadings = selectHeadings(contentEl, tocbot.options.headingSelector)
defaultHeadings = [].map.call(defaultHeadings, function (node) {
return node.textContent
return node.innerText
})

expect(defaultHeadings).to.eql([
Expand Down Expand Up @@ -148,7 +148,7 @@ describe('Parse content', function () {
var contentEl = GLOBAL.window.document.querySelector(tocbot.options.contentSelector)
var defaultHeadings = selectHeadings(contentEl, 'h1, h2')
defaultHeadings = [].map.call(defaultHeadings, function (node) {
return node.textContent
return node.innerText
})

expect(defaultHeadings).to.eql([
Expand Down Expand Up @@ -200,7 +200,7 @@ describe('Build HTML', function () {
GLOBAL.window.document.createTextNode('What'),
GLOBAL.window.document.createElement('SUP')
]
nodes[1].textContent = 'sup'
nodes[1].innerText = 'sup'
var tocEl = GLOBAL.window.document.querySelector(tocbot.options.tocSelector)
var tocEl = render(tocEl, [{
'id': 'Whatsup',
Expand All @@ -225,7 +225,7 @@ describe('Build HTML', function () {
GLOBAL.window.document.createTextNode('What'),
GLOBAL.window.document.createElement('SUP')
]
nodes[1].textContent = 'sup'
nodes[1].innerText = 'sup'
var tocEl = GLOBAL.window.document.querySelector(tocbot.options.tocSelector)
var tocEl = render(tocEl, [{
'id': 'Whatsup',
Expand Down

0 comments on commit 2639937

Please sign in to comment.