TTS: fix error & nodes getting skipped

Fixes #115
This commit is contained in:
John Factotum
2026-04-09 05:22:49 +08:00
parent 6e13f240f7
commit 5ba02c71c9
2 changed files with 6 additions and 6 deletions
+1 -1
View File
@@ -32,7 +32,7 @@ export const textWalker = function* (x, func, filterFunc) {
const walker = document.createTreeWalker(root, filter, { acceptNode: filterFunc || acceptNode })
const walk = x.commonAncestorContainer ? walkRange : walkDocument
const nodes = walk(x, walker)
const strs = nodes.map(node => node.nodeValue)
const strs = nodes.map(node => node.nodeValue ?? '')
const makeRange = (startIndex, startOffset, endIndex, endOffset) => {
const range = document.createRange()
range.setStart(nodes[startIndex], startOffset)
+5 -5
View File
@@ -53,7 +53,7 @@ const fragmentToSSML = (fragment, inherited) => {
if (!node) return
if (node.nodeType === 3) return ssml.createTextNode(node.textContent)
if (node.nodeType === 4) return ssml.createCDATASection(node.textContent)
if (node.nodeType !== 1) return
if (node.nodeType !== 1 && node.nodeType !== 11) return
let el
const nodeName = node.nodeName.toLowerCase()
@@ -66,15 +66,15 @@ const fragmentToSSML = (fragment, inherited) => {
else if (nodeName === 'em' || nodeName === 'strong')
el = ssml.createElementNS(NS.SSML, 'emphasis')
const lang = node.lang || node.getAttributeNS(NS.XML, 'lang')
const lang = node.lang || node.getAttributeNS?.(NS.XML, 'lang')
if (lang) {
if (!el) el = ssml.createElementNS(NS.SSML, 'lang')
el.setAttributeNS(NS.XML, 'lang', lang)
}
const alphabet = node.getAttributeNS(NS.SSML, 'alphabet') || inheritedAlphabet
const alphabet = node.getAttributeNS?.(NS.SSML, 'alphabet') || inheritedAlphabet
if (!el) {
const ph = node.getAttributeNS(NS.SSML, 'ph')
const ph = node.getAttributeNS?.(NS.SSML, 'ph')
if (ph) {
el = ssml.createElementNS(NS.SSML, 'phoneme')
if (alphabet) el.setAttribute('alphabet', alphabet)
@@ -92,7 +92,7 @@ const fragmentToSSML = (fragment, inherited) => {
}
return el
}
convert(fragment.firstChild, ssml.documentElement, inherited.alphabet)
convert(fragment, ssml.documentElement, inherited.alphabet)
return ssml
}