Fix direct styling for selections that contain links

Usually the limits for a selection reference the first or last
text nodes. When the first text node is migrated to a new styled span,
the range limits will suddenly not include the following siblings that
should also be migrated. This causes moveToNewSpan to produce more
spans than necessary.
This commit is contained in:
Philip Peitsch
2013-09-20 14:28:00 +02:00
committed by Friedrich W. H. Kossebau
parent 0c456fcd89
commit 5a86bddc6e
2 changed files with 42 additions and 7 deletions
+13 -6
View File
@@ -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) {
+29 -1
View File
@@ -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("<text:p>AB<text:a>C</text:a>DE</text:p>");
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("<text:p>A<text:span text:style-name='auto0'>B<text:a>C</text:a>D</text:span>E</text:p>");
r.shouldBe(t, "t.doc", "t.expected.firstChild");
}
function apply_ContainerInsertion_SimpleSpan() {
t.doc = createDocument("<text:p><text:span>ABCD</text:span></text:p>");
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("<text:p><text:span>A</text:span><text:span text:style-name='auto0'>BC</text:span><text:span>D</text:span></text:p>");
t.expected = parseXML("<text:p><text:span>A</text:span>" +
"<text:span text:style-name='auto0'>BC</text:span>" +
"<text:span>D</text:span></text:p>");
r.shouldBe(t, "t.doc", "t.expected.firstChild");
}
function apply_ContainerInsertion_SimpleSpans() {
@@ -184,6 +197,19 @@ odf.TextStyleApplicatorTests = function TextStyleApplicatorTests(runner) {
"<text:span>D</text:span></text:p>");
r.shouldBe(t, "t.doc", "t.expected.firstChild");
}
function apply_ContainerInsertion_SpanBracketingLink() {
t.doc = createDocument("<text:p><text:span>AB<text:a>C</text:a>DE</text:span></text:p>");
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("<text:p><text:span>A</text:span>" +
"<text:span text:style-name='auto0'>B<text:a>C</text:a>D</text:span>" +
"<text:span>E</text:span></text:p>");
r.shouldBe(t, "t.doc", "t.expected.firstChild");
}
function apply_ContainerInsertion_SurroundedBySpans_EndsAfterText() {
t.doc = createDocument("<text:p><text:span>AB</text:span>C<text:span>D</text:span></text:p>");
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,