Refactor proof() to remove isInitialCall requirement

Let's keep now recursiveProof() as clean as possible.
This commit is contained in:
Simon Rozman 2024-05-08 13:09:12 +02:00
parent 6392328e98
commit d6e823ed7a

View File

@ -71,12 +71,34 @@ class BesService {
* Recursively grammar-proofs a DOM tree. * Recursively grammar-proofs a DOM tree.
* *
* @param {Node} node DOM root node to proof * @param {Node} node DOM root node to proof
* @param {Boolean} isInitialCall Is this first-level call in recursion?
* @returns {Array} Markup of text to proof using BesStr
*/ */
async proof(node, isInitialCall = true) { proof(node) {
this.updateStatusIcon('bes-status-loading') this.updateStatusIcon('bes-status-loading')
this.statusDiv.title = 'BesService je v procesu preverjanja pravopisa.' this.statusDiv.title = 'BesService je v procesu preverjanja pravopisa.'
this.recursiveProof(node)
// TODO: Check count in 'makrofinančno' case.
const count = this.children?.reduce(
(total, child) => total + child.matches.length,
0
)
if (count > 0) {
this.updateStatusIcon('bes-status-mistakes')
this.statusDiv.title = 'Število napak: ' + count
} else {
this.updateStatusIcon('bes-status-success')
this.statusDiv.title = 'V besedilu ni napak.'
}
}
/**
* Recursively grammar-proofs a DOM tree.
*
* @param {Node} node DOM root node to proof
* @returns {Array} Markup of text to proof using BesStr
*/
async recursiveProof(node) {
switch (node.nodeType) { switch (node.nodeType) {
case Node.TEXT_NODE: case Node.TEXT_NODE:
return [{ text: node.textContent, node: node, markup: false }] return [{ text: node.textContent, node: node, markup: false }]
@ -91,7 +113,7 @@ class BesService {
} }
this.clearMistakeMarkup(node) this.clearMistakeMarkup(node)
let dataPromises = Array.from(node.childNodes).map(child => let dataPromises = Array.from(node.childNodes).map(child =>
this.proof(child, false) this.recursiveProof(child)
) )
let data = (await Promise.all(dataPromises)).flat() let data = (await Promise.all(dataPromises)).flat()
if (data.some(x => !x.markup && !/^\s*$/.test(x.text))) { if (data.some(x => !x.markup && !/^\s*$/.test(x.text))) {
@ -181,25 +203,11 @@ class BesService {
) )
}) })
} }
if (isInitialCall) {
// TODO: Check count in 'makrofinančno' case.
const count = this.children?.reduce(
(total, child) => total + child.matches.length,
0
)
if (count > 0) {
this.updateStatusIcon('bes-status-mistakes')
this.statusDiv.title = 'Število napak: ' + count
} else {
this.updateStatusIcon('bes-status-success')
this.statusDiv.title = 'V besedilu ni napak.'
}
}
return [{ text: '<' + node.tagName + '/>', node: node, markup: true }] return [{ text: '<' + node.tagName + '/>', node: node, markup: true }]
} else { } else {
// Inline elements require no markup. Keep plain text only. // Inline elements require no markup. Keep plain text only.
let dataPromises = Array.from(node.childNodes).map(child => let dataPromises = Array.from(node.childNodes).map(child =>
this.proof(child, false) this.recursiveProof(child)
) )
let data = (await Promise.all(dataPromises)).flat() let data = (await Promise.all(dataPromises)).flat()
return data return data