Fix <br> and false missing space grammar mistake report

Fixes: #1
This commit is contained in:
2025-02-24 10:39:32 +01:00
parent c68e512496
commit 5e52b71242
6 changed files with 47 additions and 26 deletions

View File

@@ -671,8 +671,22 @@ class BesTreeService extends BesService {
}
return [{ text: `<${node.tagName}/>`, node: node, markup: true }]
} else {
// Inline elements require no markup. Keep plain text only.
let data = []
if (this.doesElementAddSpace(node)) {
// Inline element adds some space between text. Convert node to spaces.
const inner = node.innerHTML
const len =
inner.length > 0
? node.outerHTML.indexOf(inner)
: node.outerHTML.length
data = data.concat({
text: ' '.repeat(len),
node: node,
markup: false
})
} else {
// Inline elements require no markup. Keep plain text only.
}
for (const el2 of node.childNodes)
data = data.concat(this.proofNode(el2, abortController))
return data
@@ -774,6 +788,26 @@ class BesTreeService extends BesService {
}
}
/**
* Tests if given element adds space before child/next text
*
* @param {Element} el DOM element
* @returns true if adds space; false otherwise.
*/
doesElementAddSpace(el) {
const prevNode = el.previousSibling
const nextNode = el.firstChild || el.nextSibling
if (!prevNode || !nextNode) return false
const range = document.createRange()
range.setStart(
prevNode,
prevNode.nodeType === Node.TEXT_NODE ? prevNode.length : 0
)
range.setEnd(nextNode, 0)
const bounds = range.getBoundingClientRect()
return bounds.width !== 0
}
/**
* Returns first block parent element of a node.
*