CKEditor 5通过Attibute查找模型元素



我的编辑器中存在一个自定义的html dom元素:

<content id="1">
<p>Paragraf is here and <a href="/url/to">link text</a> exists</p>
</content>

我想使用它的属性在模型中找到这个元素。我可以使用从根开始的递归方法找到它。但是有没有像querySelectorAll这样更简单的方法呢?

我还没有找到一个开箱即用的助手方法来实现这一点,但如果有人只是在寻找他们可以使用的函数定义,请尝试以下方法:

function findDescendant (modelElement, predicate) {
if (predicate(modelElement)) {
return modelElement
}
if (modelElement.getChildren) {
for (let child of modelElement.getChildren()) {
const found = findDescendantByName(child, name)
if (found) {
return found
}
}
}
}
...
...
const descendant = findDescendant(element, (element) => element.hasAttribute('foobar'))

最新更新