/**
* @module plugins/backspace
*/
import type { IJodit, Nullable } from 'jodit/types';
import { Dom } from 'jodit/core/dom';
import { INSEPARABLE_TAGS } from 'jodit/core/constants';
import { checkJoinTwoLists } from './check-join-two-lists';
/**
* Check if the current empty item can be removed
*
* @example
* ```html
*
first stop
|
* ```
* result
* ```html
* first stop|
* ```
*
* @private
*/
export function checkRemoveEmptyParent(
jodit: IJodit,
fakeNode: Node,
backspace: boolean
): boolean {
let found: boolean = false;
const { setCursorBefore, setCursorIn } = jodit.s;
let prn: Nullable = Dom.closest(
fakeNode,
Dom.isElement,
jodit.editor
);
if (!prn || !Dom.isEmpty(prn)) {
return false;
}
const neighbor = Dom.findNotEmptyNeighbor(
fakeNode,
backspace,
jodit.editor
);
do {
if (prn && Dom.isEmpty(prn) && !Dom.isCell(prn)) {
Dom.after(prn, fakeNode);
const tmp: Nullable = Dom.closest(
prn,
n => Dom.isElement(n) && n !== prn,
jodit.editor
);
Dom.safeRemove(prn);
found = true;
prn = tmp;
} else {
break;
}
} while (prn);
if (found && checkJoinTwoLists(jodit, fakeNode, backspace)) {
return true;
}
if (
neighbor &&
!Dom.isText(neighbor) &&
!Dom.isTag(neighbor, INSEPARABLE_TAGS) &&
!Dom.isTag(neighbor.firstChild, [
...INSEPARABLE_TAGS,
'jodit-file',
'jodit-link-preview'
])
) {
setCursorIn(neighbor, !backspace);
} else {
setCursorBefore(fakeNode);
}
return found;
}