Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function workaroundForChrome(editor: Editor) {
let traverser = editor.getSelectionTraverser();
let block = traverser && traverser.currentBlockElement;
while (block) {
let container = block.getStartNode();
if (container) {
// Add a temp <img> tag before all other nodes in the block to avoid Chrome remove existing format when toggle list
const tempNode = fromHtml(TEMP_NODE_HTML, editor.getDocument())[0];
if (isVoidHtmlElement(container) || !isBlockElement(container)) {
container.parentNode.insertBefore(tempNode, container);
} else {
container.insertBefore(tempNode, container.firstChild);
}
}
block = traverser.getNextBlockElement();
}
}
if (option.updateCursor) {
core.api.focus(core);
}
switch (option.position) {
case ContentPosition.Begin:
case ContentPosition.End: {
let isBegin = option.position == ContentPosition.Begin;
let block = getFirstLastBlockElement(contentDiv, isBegin);
let insertedNode: Node;
if (block) {
let refNode = isBegin ? block.getStartNode() : block.getEndNode();
if (
option.insertOnNewLine ||
refNode.nodeType == NodeType.Text ||
isVoidHtmlElement(refNode)
) {
// For insert on new line, or refNode is text or void html element (HR, BR etc.)
// which cannot have children, i.e. <div>hello<br>world</div>. 'hello', 'world' are the
// first and last node. Insert before 'hello' or after 'world', but still inside DIV
insertedNode = refNode.parentNode.insertBefore(
node,
isBegin ? refNode : refNode.nextSibling
);
} else {
// if the refNode can have child, use appendChild (which is like to insert as first/last child)
// i.e. <div>hello</div>, the content will be inserted before/after hello
insertedNode = refNode.insertBefore(node, isBegin ? refNode.firstChild : null);
}
} else {
// No first block, this can happen when editor is empty. Use appendChild to insert the content in contentDiv
insertedNode = contentDiv.appendChild(node);
if (option.updateCursor) {
core.api.focus(core);
}
switch (option.position) {
case ContentPosition.Begin:
case ContentPosition.End:
let isBegin = option.position == ContentPosition.Begin;
let block = getFirstLastBlockElement(contentDiv, isBegin);
let insertedNode: Node;
if (block) {
let refNode = isBegin ? block.getStartNode() : block.getEndNode();
if (
option.insertOnNewLine ||
refNode.nodeType == NodeType.Text ||
isVoidHtmlElement(refNode)
) {
// For insert on new line, or refNode is text or void html element (HR, BR etc.)
// which cannot have children, i.e. <div>hello<br>world</div>. 'hello', 'world' are the
// first and last node. Insert before 'hello' or after 'world', but still inside DIV
insertedNode = refNode.parentNode.insertBefore(
node,
isBegin ? refNode : refNode.nextSibling
);
} else {
// if the refNode can have child, use appendChild (which is like to insert as first/last child)
// i.e. <div>hello</div>, the content will be inserted before/after hello
insertedNode = refNode.insertBefore(node, isBegin ? refNode.firstChild : null);
}
} else {
// No first block, this can happen when editor is empty. Use appendChild to insert the content in contentDiv
insertedNode = contentDiv.appendChild(node);
editor.runAsync(() => {
let newContainer = editor.getElementAtCursor();
if (
contains(vtable.table, newContainer) &&
!contains(td, newContainer, true /*treatSameNodeAsContain*/)
) {
let newPos = targetTd
? new Position(targetTd, PositionType.Begin)
: new Position(vtable.table, isUp ? PositionType.Before : PositionType.After);
if (hasShiftKey) {
newPos =
newPos.node.nodeType == NodeType.Element && isVoidHtmlElement(newPos.node)
? new Position(
newPos.node,
newPos.isAtEnd ? PositionType.After : PositionType.Before
)
: newPos;
editor
.getSelection()
.setBaseAndExtent(anchorNode, anchorOffset, newPos.node, newPos.offset);
} else {
editor.select(newPos);
}
}
});
},