From 4707fc46acca37ed5b9da55171b66655b02763b1 Mon Sep 17 00:00:00 2001 From: Kevin Jahns Date: Mon, 2 Aug 2021 16:43:25 +0200 Subject: [PATCH] fix formatting bug. fixes #319 closes #320 --- src/types/YText.js | 7 ++++--- tests/y-xml.tests.js | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/types/YText.js b/src/types/YText.js index 0bf4d567..6ded2658 100644 --- a/src/types/YText.js +++ b/src/types/YText.js @@ -164,12 +164,13 @@ const insertNegatedAttributes = (transaction, parent, currPos, negatedAttributes } const doc = transaction.doc const ownClientId = doc.clientID - let nextFormat = currPos.left - const right = currPos.right negatedAttributes.forEach((val, key) => { - nextFormat = new Item(createID(ownClientId, getState(doc.store, ownClientId)), nextFormat, nextFormat && nextFormat.lastId, right, right && right.id, parent, null, new ContentFormat(key, val)) + const left = currPos.left + const right = currPos.right + const nextFormat = new Item(createID(ownClientId, getState(doc.store, ownClientId)), left, left && left.lastId, right, right && right.id, parent, null, new ContentFormat(key, val)) nextFormat.integrate(transaction, 0) currPos.right = nextFormat + currPos.forward() }) } diff --git a/tests/y-xml.tests.js b/tests/y-xml.tests.js index 3a5c12a4..619d9ee9 100644 --- a/tests/y-xml.tests.js +++ b/tests/y-xml.tests.js @@ -168,3 +168,18 @@ export const testClone = tc => { t.assert(cloneYxml.length === 3) t.compare(cloneYxml.toJSON(), yxml.toJSON()) } + +/** + * @param {t.TestCase} tc + */ +export const testFormattingBug = tc => { + const ydoc = new Y.Doc() + const yxml = /** @type {Y.XmlText} */ (ydoc.get('', Y.XmlText)) + const delta = [ + { insert: 'A', attributes: { em: {}, strong: {} } }, + { insert: 'B', attributes: { em: {} } }, + { insert: 'C', attributes: { em: {}, strong: {} } } + ] + yxml.applyDelta(delta) + t.compare(yxml.toDelta(), delta) +}