From c443dd7a32665c67c6730f0099859ee11ecf0395 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Fri, 21 Jun 2019 16:37:29 +0200 Subject: [PATCH] clarify why use a BOM marker for the caret nodes --- src/editor/dom.js | 6 +++--- src/editor/render.js | 13 ++++++++----- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/editor/dom.js b/src/editor/dom.js index 5c873034b2..1b683c2c5e 100644 --- a/src/editor/dom.js +++ b/src/editor/dom.js @@ -15,7 +15,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import {ZERO_WIDTH_SPACE, isCaretNode} from "./render"; +import {CARET_NODE_CHAR, isCaretNode} from "./render"; export function walkDOMDepthFirst(rootNode, enterNodeCallback, leaveNodeCallback) { let node = rootNode.firstChild; @@ -57,7 +57,7 @@ export function getCaretOffsetAndText(editor, sel) { function getCaret(focusNode, focusNodeOffset, focusOffset) { let atNodeEnd = focusOffset === focusNode.textContent.length; if (focusNode.nodeType === Node.TEXT_NODE && isCaretNode(focusNode.parentElement)) { - const zwsIdx = focusNode.nodeValue.indexOf(ZERO_WIDTH_SPACE); + const zwsIdx = focusNode.nodeValue.indexOf(CARET_NODE_CHAR); if (zwsIdx !== -1 && zwsIdx < focusOffset) { focusOffset -= 1; } @@ -120,7 +120,7 @@ function getTextNodeValue(node) { // typed in the caret node, so there is now something more in it than the ZWS // so filter out the ZWS, and take the typed text into account if (nodeText.length !== 1) { - return nodeText.replace(ZERO_WIDTH_SPACE, ""); + return nodeText.replace(CARET_NODE_CHAR, ""); } else { // only contains ZWS, which is ignored, so return emtpy string return ""; diff --git a/src/editor/render.js b/src/editor/render.js index 690851c4ea..9d42bbe947 100644 --- a/src/editor/render.js +++ b/src/editor/render.js @@ -33,22 +33,25 @@ function insertAfter(node, nodeToInsert) { } } -// this is a BOM marker actually -export const ZERO_WIDTH_SPACE = "\ufeff"; +// Use a BOM marker for caret nodes. +// On a first test, they seem to be filtered out when copying text out of the editor, +// but this could be platform dependent. +// As a precautionary measure, I chose the character that slate also uses. +export const CARET_NODE_CHAR = "\ufeff"; // a caret node is a node that allows the caret to be placed // where otherwise it wouldn't be possible // (e.g. next to a pill span without adjacent text node) function createCaretNode() { const span = document.createElement("span"); span.className = "caretNode"; - span.appendChild(document.createTextNode(ZERO_WIDTH_SPACE)); + span.appendChild(document.createTextNode(CARET_NODE_CHAR)); return span; } function updateCaretNode(node) { // ensure the caret node contains only a zero-width space - if (node.textContent !== ZERO_WIDTH_SPACE) { - node.textContent = ZERO_WIDTH_SPACE; + if (node.textContent !== CARET_NODE_CHAR) { + node.textContent = CARET_NODE_CHAR; } }