Disallow empty comments and comments with only whitespaces (#7156)
* fix: disallow empty comments * fix: avoid full traversal to validate comment * fix: text * fix:review * fix: review
This commit is contained in:
@@ -166,6 +166,8 @@ export default class Image extends SimpleImage {
|
||||
["p", { class: "caption" }, 0],
|
||||
];
|
||||
},
|
||||
toPlainText: (node) =>
|
||||
node.attrs.alt ? `(image: ${node.attrs.alt})` : "(image)",
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -142,8 +142,35 @@ export class ProsemirrorHelper {
|
||||
*
|
||||
* @returns True if the editor is empty
|
||||
*/
|
||||
static isEmpty(doc: Node) {
|
||||
return !doc || doc.textContent.trim() === "";
|
||||
static isEmpty(doc: Node, schema?: Schema) {
|
||||
if (!schema) {
|
||||
return !doc || doc.textContent.trim() === "";
|
||||
}
|
||||
|
||||
const textSerializers = Object.fromEntries(
|
||||
Object.entries(schema.nodes)
|
||||
.filter(([, node]) => node.spec.toPlainText)
|
||||
.map(([name, node]) => [name, node.spec.toPlainText])
|
||||
);
|
||||
|
||||
let empty = true;
|
||||
doc.descendants((child: Node) => {
|
||||
// If we've already found non-empty data, we can stop descending further
|
||||
if (!empty) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const toPlainText = textSerializers[child.type.name];
|
||||
if (toPlainText) {
|
||||
empty = !toPlainText(child).trim();
|
||||
} else if (child.isText) {
|
||||
empty = !child.text?.trim();
|
||||
}
|
||||
|
||||
return empty;
|
||||
});
|
||||
|
||||
return empty;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user