Variable depth nodes fix

This commit is contained in:
mrfry 2022-05-15 11:37:08 +02:00
parent 03c4064de8
commit 6213c1a7b8

View file

@ -116,11 +116,11 @@
// Devel vars // Devel vars
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// forcing pages for testing. unless you test, do not set these to true! // forcing pages for testing. unless you test, do not set these to true!
const isDevel = true const isDevel = false
const forcedMatchString = isDevel ? 'default' : null const forcedMatchString = isDevel ? 'default' : null
// only one of these should be true for testing // only one of these should be true for testing
const forceTestPage = isDevel && false const forceTestPage = isDevel && false
const forceResultPage = isDevel && true const forceResultPage = isDevel && false
const forceDefaultPage = isDevel && false const forceDefaultPage = isDevel && false
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
@ -218,8 +218,12 @@
// : Basic processing helpers {{{ // : Basic processing helpers {{{
function getTextPromisesFromNode(node) { function getTextPromisesFromNode(inputNode) {
return Array.from(node.childNodes).reduce((promises, elem) => { const nodes = Array.from(inputNode.childNodes)
.map((x) => flattenNode(x))
.flat()
return nodes.reduce((promises, elem) => {
let img = elem let img = elem
if (elem.tagName !== 'IMG') { if (elem.tagName !== 'IMG') {
const t = elem.tagName ? elem.getElementsByTagName('img') : [] const t = elem.tagName ? elem.getElementsByTagName('img') : []
@ -246,17 +250,21 @@
} }
if (img.tagName === 'IMG') { if (img.tagName === 'IMG') {
promises.push( if (img.title) {
new Promise((resolve) => { promises.push({ type: 'txt', val: img.title, node: elem })
digestMessage(getBase64Image(img)).then((res) => { } else {
resolve({ promises.push(
type: 'img', new Promise((resolve) => {
val: res, digestMessage(getBase64Image(img)).then((res) => {
node: img, resolve({
type: 'img',
val: res,
node: img,
})
}) })
}) })
}) )
) }
} else if (elem.tagName === undefined) { } else if (elem.tagName === undefined) {
promises.push({ type: 'txt', val: elem.nodeValue, node: elem }) promises.push({ type: 'txt', val: elem.nodeValue, node: elem })
} else { } else {
@ -267,6 +275,16 @@
}, []) }, [])
} }
function flattenNode(node) {
if (node.childNodes && node.childNodes.length > 0) {
return Array.from(node.childNodes)
.map((x) => flattenNode(x))
.flat()
} else {
return node
}
}
function makeTextFromElements(acc, item) { function makeTextFromElements(acc, item) {
if (emptyOrWhiteSpace(item.val)) { if (emptyOrWhiteSpace(item.val)) {
return acc return acc
@ -1209,7 +1227,8 @@
) )
}) })
} catch (e) { } catch (e) {
console.warn(e) debugLog('Error in handleAVRQuiz')
debugLog(e)
} }
} }
@ -1905,8 +1924,8 @@
) )
} }
} catch (e) { } catch (e) {
console.warn('Error showing answers') debugLog('Error showing answers')
console.warn(e) debugLog(e)
} }
} }
@ -2410,8 +2429,8 @@
result.msgContainer.child.msgDiv.elem.innerText = getCurrMsg().m result.msgContainer.child.msgDiv.elem.innerText = getCurrMsg().m
} catch (e) { } catch (e) {
console.warn('Error in message updating') debugLog('Error in message updating')
console.warn(e) debugLog(e)
} }
} }
updateMessageText() updateMessageText()