diff --git a/webodf/lib/odf/TextStyleApplicator.js b/webodf/lib/odf/TextStyleApplicator.js index 3e5791c1..bfab0690 100644 --- a/webodf/lib/odf/TextStyleApplicator.js +++ b/webodf/lib/odf/TextStyleApplicator.js @@ -138,9 +138,10 @@ odf.TextStyleApplicator = function TextStyleApplicator(styleNameGenerator, forma styledContainer, trailingContainer, moveTrailing, - node = startNode, + node, nextNode, - loopGuard = new core.LoopWatchDog(1000); + loopGuard = new core.LoopWatchDog(1000), + styledNodes = []; // Do we need a new style container? if (!isTextSpan(originalContainer)) { @@ -161,14 +162,20 @@ odf.TextStyleApplicator = function TextStyleApplicator(styleNameGenerator, forma } // Starting at the startNode, iterate forward until leaving the affected range - while (node && (node === startNode || domUtils.rangeContainsNode(limits, node))) { + styledNodes.push(startNode); + node = startNode.nextSibling; + // Need to fetch all nodes to move before starting to move any, in case + // the limits actually reference one of the nodes this loop is about to relocate + while (node && domUtils.rangeContainsNode(limits, node)) { loopGuard.check(); - nextNode = node.nextSibling; + styledNodes.push(node); + node = node.nextSibling; + } + styledNodes.forEach(function(node) { if (node.parentNode !== styledContainer) { styledContainer.appendChild(node); } - node = nextNode; - } + }); // Any trailing nodes? if (node && moveTrailing) { diff --git a/webodf/tests/odf/TextStyleApplicatorTests.js b/webodf/tests/odf/TextStyleApplicatorTests.js index 1d2978d4..23b06564 100644 --- a/webodf/tests/odf/TextStyleApplicatorTests.js +++ b/webodf/tests/odf/TextStyleApplicatorTests.js @@ -159,6 +159,17 @@ odf.TextStyleApplicatorTests = function TextStyleApplicatorTests(runner) { r.shouldBe(t, "t.doc.childNodes[2].textContent", "'D'"); } + function apply_ContainerInsertion_TextBracketingLink() { + t.doc = createDocument("ABCDE"); + t.range.setStart(t.doc.childNodes[0], 1); + t.range.setEnd(t.doc.childNodes[2], 1); + + t.styleHelper.applyStyle("tStyle", t.range, {"style:text-properties": {"fo:font-weight": "bold"}}); + + simplifyAutoStyleNames(t.doc); + t.expected = parseXML("ABCDE"); + r.shouldBe(t, "t.doc", "t.expected.firstChild"); + } function apply_ContainerInsertion_SimpleSpan() { t.doc = createDocument("ABCD"); t.range.setStart(t.doc.childNodes[0].childNodes[0], 1); @@ -167,7 +178,9 @@ odf.TextStyleApplicatorTests = function TextStyleApplicatorTests(runner) { t.styleHelper.applyStyle("tStyle", t.range, {"style:text-properties": {"fo:font-weight": "bold"}}); simplifyAutoStyleNames(t.doc); - t.expected = parseXML("ABCD"); + t.expected = parseXML("A" + + "BC" + + "D"); r.shouldBe(t, "t.doc", "t.expected.firstChild"); } function apply_ContainerInsertion_SimpleSpans() { @@ -184,6 +197,19 @@ odf.TextStyleApplicatorTests = function TextStyleApplicatorTests(runner) { "D"); r.shouldBe(t, "t.doc", "t.expected.firstChild"); } + function apply_ContainerInsertion_SpanBracketingLink() { + t.doc = createDocument("ABCDE"); + t.range.setStart(t.doc.childNodes[0].childNodes[0], 1); + t.range.setEnd(t.doc.childNodes[0].childNodes[2], 1); + + t.styleHelper.applyStyle("tStyle", t.range, {"style:text-properties": {"fo:font-weight": "bold"}}); + + simplifyAutoStyleNames(t.doc); + t.expected = parseXML("A" + + "BCD" + + "E"); + r.shouldBe(t, "t.doc", "t.expected.firstChild"); + } function apply_ContainerInsertion_SurroundedBySpans_EndsAfterText() { t.doc = createDocument("ABCD"); t.range.setStart(t.doc.childNodes[0].childNodes[0], 1); @@ -329,8 +355,10 @@ odf.TextStyleApplicatorTests = function TextStyleApplicatorTests(runner) { return [ apply_ContainerInsertion_SimpleTextRange, apply_ContainerInsertion_SimpleTextRange_EndsAtNodeBeginning, + apply_ContainerInsertion_TextBracketingLink, apply_ContainerInsertion_SimpleSpan, apply_ContainerInsertion_SimpleSpans, + apply_ContainerInsertion_SpanBracketingLink, apply_ContainerInsertion_SurroundedBySpans_EndsAfterText, apply_ContainerInsertion_SurroundedBySpans_BeforeText, apply_ContainerInsertion_SurroundedBySpans_StartsAfterText,