summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCarsten Hiesserich2013-05-27 15:07:26 (EDT)
committer Florian Thienel2013-05-27 15:07:26 (EDT)
commit6e7c6a0b031e96d83f35e42721e8f455c03e1a52 (patch)
treebc124a18f341cee36eaa50a51045d1fc6d407a10
parent86cdca16d9416f3a497bba9c340cc9246da9c0e3 (diff)
downloadorg.eclipse.mylyn.docs.vex-6e7c6a0b031e96d83f35e42721e8f455c03e1a52.zip
org.eclipse.mylyn.docs.vex-6e7c6a0b031e96d83f35e42721e8f455c03e1a52.tar.gz
org.eclipse.mylyn.docs.vex-6e7c6a0b031e96d83f35e42721e8f455c03e1a52.tar.bz2
Add element fails in nodes with mixed inline and text elements
https://bugs.eclipse.org/bugs/show_bug.cgi?id=408731
-rw-r--r--org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/dom/ParentTest.java74
-rw-r--r--org.eclipse.vex.core/src/org/eclipse/vex/core/internal/dom/Parent.java7
2 files changed, 79 insertions, 2 deletions
diff --git a/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/dom/ParentTest.java b/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/dom/ParentTest.java
index 8562c20..678b831 100644
--- a/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/dom/ParentTest.java
+++ b/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/dom/ParentTest.java
@@ -1,3 +1,14 @@
+/*******************************************************************************
+ * Copyright (c) 2012, 2013 Florian Thienel and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Florian Thienel - initial API and implementation
+ * Carsten Hiesserich - extended insert / remove tests for nodes with text (bug 408731)
+ *******************************************************************************/
package org.eclipse.vex.core.internal.dom;
import static org.junit.Assert.assertEquals;
@@ -86,6 +97,60 @@ public class ParentTest {
}
@Test
+ public void insertChildBeforeFirstInline() throws Exception {
+ content.insertText(parent.getEndOffset(), "1234");
+ final TestChild firstInlineElement = addTestChild();
+ content.insertText(parent.getEndOffset(), "5678");
+ addTestChild();
+
+ // insert element between 2 and 3
+ final TestChild insertedElement = insertTestChildAt(firstInlineElement.getStartOffset() - 2);
+
+ assertEquals(6, parent.children().count());
+ assertTrue(parent.children().get(0) instanceof IText);
+ assertEquals("12", parent.children().get(0).getText());
+ assertSame(insertedElement, parent.children().get(1));
+ assertTrue(parent.children().get(2) instanceof IText);
+ assertEquals("34", parent.children().get(2).getText());
+ }
+
+ @Test
+ public void insertChildAfterLastInline() throws Exception {
+ content.insertText(parent.getEndOffset(), "1234");
+ addTestChild();
+ content.insertText(parent.getEndOffset(), "5678");
+ final TestChild lastInlineElement = addTestChild();
+ content.insertText(parent.getEndOffset(), "90");
+
+ // insert element between 9 and 0
+ final TestChild insertedElement = insertTestChildAt(lastInlineElement.getEndOffset() + 2);
+
+ for (final INode child : parent.children()) {
+ System.out.println(child);
+ }
+ assertEquals(7, parent.children().count());
+ assertTrue(parent.children().get(4) instanceof IText);
+ assertEquals("9", parent.children().get(4).getText());
+ assertSame(insertedElement, parent.children().get(5));
+ assertTrue(parent.children().get(6) instanceof IText);
+ assertEquals("0", parent.children().get(6).getText());
+ }
+
+ @Test
+ public void removeChildExtended() throws Exception {
+ addTestChild();
+ content.insertText(parent.getEndOffset(), "12");
+ final TestChild secondChild = addTestChild();
+ content.insertText(parent.getEndOffset(), "34");
+ addTestChild();
+
+ parent.removeChild(secondChild);
+
+ assertTrue(parent.children().get(1) instanceof IText);
+ assertEquals("1234", parent.children().get(1).getText());
+ }
+
+ @Test
public void shouldSetParentOnAddedChild() throws Exception {
final TestChild child = new TestChild();
assertNull(child.getParent());
@@ -618,6 +683,15 @@ public class ParentTest {
return result;
}
+ private TestChild insertTestChildAt(final int offset) {
+ content.insertTagMarker(offset);
+ content.insertTagMarker(offset);
+ final TestChild result = new TestChild();
+ parent.insertChildAt(offset, result);
+ result.associate(content, new ContentRange(offset, offset + 1));
+ return result;
+ }
+
private static class TestParent extends Parent {
public void accept(final INodeVisitor visitor) {
throw new UnsupportedOperationException();
diff --git a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/dom/Parent.java b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/dom/Parent.java
index 07c24b8..8c1529c 100644
--- a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/dom/Parent.java
+++ b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/dom/Parent.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2012 Florian Thienel and others.
+ * Copyright (c) 2012, 2013 Florian Thienel and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -7,6 +7,7 @@
*
* Contributors:
* Florian Thienel - initial API and implementation
+ * Carsten Hiesserich - fixed insertion of elements to nodes with text (bug 408731)
*******************************************************************************/
package org.eclipse.vex.core.internal.dom;
@@ -61,8 +62,10 @@ public abstract class Parent extends Node implements IParent {
private int indexOfChildNextTo(final int offset) {
final ContentRange insertionRange = getRange().resizeBy(1, 0);
Assert.isTrue(insertionRange.contains(offset), MessageFormat.format("The offset must be within {0}.", insertionRange));
+
+ // We have to return an index in the children List, so Axis withoutText must be used!
int i = 0;
- for (final INode child : children()) {
+ for (final INode child : children().withoutText()) {
if (offset <= child.getStartOffset()) {
return i;
}