bug 257806: add separate tag markers for the document


Signed-off-by: Florian Thienel <florian@thienel.org>
diff --git a/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/css/CssTest.java b/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/css/CssTest.java
index 367e40c..03fe25b 100644
--- a/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/css/CssTest.java
+++ b/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/css/CssTest.java
@@ -170,7 +170,7 @@
 	public void testDefaultInheritance() throws Exception {
 		final Element simple = new Element("simple");
 		final Document doc = new Document(simple);
-		final Element defaults = doc.insertElement(1, new QualifiedName(null, "defaults"));
+		final Element defaults = doc.insertElement(2, new QualifiedName(null, "defaults"));
 
 		final StyleSheet ss = parseStyleSheetResource("test2.css");
 		final Styles styles = ss.getStyles(defaults);
@@ -432,7 +432,7 @@
 	public void testForcedInheritance() throws Exception {
 		final Element simple = new Element("simple");
 		final Document doc = new Document(simple);
-		final Element inherit = doc.insertElement(1, new QualifiedName(null, "inherit"));
+		final Element inherit = doc.insertElement(2, new QualifiedName(null, "inherit"));
 
 		final StyleSheet ss = parseStyleSheetResource("test2.css");
 		final Styles styles = ss.getStyles(inherit);
diff --git a/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/css/RuleTest.java b/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/css/RuleTest.java
index 54ff275..1368758 100644
--- a/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/css/RuleTest.java
+++ b/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/css/RuleTest.java
@@ -32,11 +32,11 @@
 
 		final Element a = new Element("a");
 		final Document doc = new Document(a);
-		final Element b = doc.insertElement(1, new QualifiedName(null, "b"));
-		final Element c = doc.insertElement(2, new QualifiedName(null, "c"));
-		final Element d = doc.insertElement(3, new QualifiedName(null, "d"));
-		final Element e = doc.insertElement(4, new QualifiedName(null, "e"));
-		final Element f = doc.insertElement(5, new QualifiedName(null, "f"));
+		final Element b = doc.insertElement(2, new QualifiedName(null, "b"));
+		final Element c = doc.insertElement(3, new QualifiedName(null, "c"));
+		final Element d = doc.insertElement(4, new QualifiedName(null, "d"));
+		final Element e = doc.insertElement(5, new QualifiedName(null, "e"));
+		final Element f = doc.insertElement(6, new QualifiedName(null, "f"));
 
 		b.setAttribute("color", "blue");
 		c.setAttribute("color", "blue red");
@@ -268,7 +268,7 @@
 
 		final Element a = new Element("a");
 		final Document doc = new Document(a);
-		final Element ns = doc.insertElement(1, new QualifiedName("http://namespace/uri", "b"));
+		final Element ns = doc.insertElement(2, new QualifiedName("http://namespace/uri", "b"));
 
 		ns.setAttribute("color", "blue");
 
diff --git a/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/dom/DTDValidatorTest.java b/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/dom/DTDValidatorTest.java
index 0e4b96f..41fd156 100644
--- a/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/dom/DTDValidatorTest.java
+++ b/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/dom/DTDValidatorTest.java
@@ -91,24 +91,25 @@
 		// 1 2 3 4 5 6 7
 		final Document doc = new Document(new Element("section"));
 		doc.setValidator(validator);
-		doc.insertElement(1, new QualifiedName(null, "title"));
-		doc.insertText(2, "ab");
-		doc.insertElement(5, new QualifiedName(null, "para"));
+		doc.insertElement(2, new QualifiedName(null, "title"));
+		doc.insertText(3, "ab");
+		doc.insertElement(6, new QualifiedName(null, "para"));
 
 		assertValidItemsAt(doc, 0);
-		assertValidItemsAt(doc, 1, "title", "para");
-		assertValidItemsAt(doc, 2);
+		assertValidItemsAt(doc, 1);
+		assertValidItemsAt(doc, 2, "title", "para");
 		assertValidItemsAt(doc, 3);
 		assertValidItemsAt(doc, 4);
-		assertValidItemsAt(doc, 5, "title", "para");
-		assertValidItemsAt(doc, 6, "emphasis");
-		assertValidItemsAt(doc, 7, "title", "para");
+		assertValidItemsAt(doc, 5);
+		assertValidItemsAt(doc, 6, "title", "para");
+		assertValidItemsAt(doc, 7, "emphasis");
+		assertValidItemsAt(doc, 8, "title", "para");
 	}
 
 	public void testOneKindOfChild() {
 		final Document doc = new Document(new Element("one-kind-of-child"));
 		doc.setValidator(validator);
-		assertValidItemsAt(doc, 1, "section");
+		assertValidItemsAt(doc, 2, "section");
 	}
 
 	private static void assertValidItemsAt(final Document doc, final int offset, final String... expectedItems) {
@@ -163,9 +164,9 @@
 	public void testValidateDocumentWithDTDAndNamespaces() throws Exception {
 		final Document doc = new Document(new Element(new QualifiedName("http://namespace/uri/is/not/registered", "section")));
 		doc.setValidator(validator);
-		doc.insertElement(1, new QualifiedName(null, "title"));
-		doc.insertText(2, "ab");
-		doc.insertElement(5, new QualifiedName(null, "para"));
+		doc.insertElement(2, new QualifiedName(null, "title"));
+		doc.insertText(3, "ab");
+		doc.insertElement(6, new QualifiedName(null, "para"));
 
 		validator.getAttributeDefinitions(doc.getRootElement());
 	}
diff --git a/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/dom/DocumentTest.java b/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/dom/DocumentTest.java
index 2d10ec1..7f294a6 100644
--- a/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/dom/DocumentTest.java
+++ b/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/dom/DocumentTest.java
@@ -55,7 +55,7 @@
 	@Test

 	public void createFragmentWithTextAndChild() throws Exception {

 		final Document document = new Document(new Element("root"));

-		final Element childElement = document.insertElement(1, new QualifiedName(null, "child"));

+		final Element childElement = document.insertElement(2, new QualifiedName(null, "child"));

 		document.insertText(childElement.getStartOffset(), "Hello ");

 		document.insertText(childElement.getEndOffset(), "Child");

 		document.insertText(childElement.getEndOffset() + 1, " World");

@@ -68,7 +68,7 @@
 	@Test

 	public void createFragmentWithExactlyOneChild() throws Exception {

 		final Document document = new Document(new Element("root"));

-		final Element childElement = document.insertElement(1, new QualifiedName(null, "child"));

+		final Element childElement = document.insertElement(2, new QualifiedName(null, "child"));

 		document.insertText(childElement.getEndOffset(), "Child");

 		final ContentRange range = childElement.getRange();

 		final DocumentFragment fragment = document.getFragment(range);

@@ -79,7 +79,7 @@
 	@Test

 	public void givenElementWithText_whenRangeBeginsFromStartOffset_shouldProvideParentAsCommenNode() throws Exception {

 		final Document document = new Document(new Element("root"));

-		final Element childElement = document.insertElement(1, new QualifiedName(null, "child"));

+		final Element childElement = document.insertElement(2, new QualifiedName(null, "child"));

 		document.insertText(childElement.getEndOffset(), "Hello World");

 

 		final Node commonNode = document.findCommonNode(childElement.getStartOffset(), childElement.getEndOffset() - 5);

@@ -90,7 +90,7 @@
 	@Test

 	public void givenElementWithText_whenRangeWithinText_shouldProvideElementAsCommonNode() throws Exception {

 		final Document document = new Document(new Element("root"));

-		final Element childElement = document.insertElement(1, new QualifiedName(null, "child"));

+		final Element childElement = document.insertElement(2, new QualifiedName(null, "child"));

 		document.insertText(childElement.getEndOffset(), "Hello World");

 

 		final Node commonNode = document.findCommonNode(childElement.getStartOffset() + 2, childElement.getEndOffset() - 5);

diff --git a/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/dom/L1CommentHandlingTest.java b/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/dom/L1CommentHandlingTest.java
index b7d6acc..79a6b70 100644
--- a/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/dom/L1CommentHandlingTest.java
+++ b/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/dom/L1CommentHandlingTest.java
@@ -34,7 +34,7 @@
 	public void setUp() throws Exception {

 		document = new Document(new Element("root"));

 		rootElement = document.getRootElement();

-		titleElement = document.insertElement(1, new QualifiedName(null, "title"));

+		titleElement = document.insertElement(2, new QualifiedName(null, "title"));

 	}

 

 	@Test

diff --git a/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/dom/L1ElementHandlingTest.java b/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/dom/L1ElementHandlingTest.java
index 2ab625e..be7118d 100644
--- a/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/dom/L1ElementHandlingTest.java
+++ b/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/dom/L1ElementHandlingTest.java
@@ -66,7 +66,7 @@
 		assertSame(rootElement, newElement.getParent());

 		assertEquals(contentLengthBefore + 2, content.length());

 		assertSame(content, newElement.getContent());

-		assertEquals(new ContentRange(1, 2), newElement.getRange());

+		assertEquals(new ContentRange(2, 3), newElement.getRange());

 	}

 

 	@Test(expected = DocumentValidationException.class)

diff --git a/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/dom/L1TextHandlingTest.java b/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/dom/L1TextHandlingTest.java
index 8d36b45..4a66bf8 100644
--- a/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/dom/L1TextHandlingTest.java
+++ b/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/dom/L1TextHandlingTest.java
@@ -32,7 +32,7 @@
 	@Before

 	public void setUp() throws Exception {

 		document = new Document(new Element("root"));

-		titleElement = document.insertElement(1, new QualifiedName(null, "title"));

+		titleElement = document.insertElement(2, new QualifiedName(null, "title"));

 		document.setValidator(new DummyValidator() {

 			@Override

 			public boolean isValidSequence(final QualifiedName element, final List<QualifiedName> nodes, final boolean partial) {

diff --git a/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/dom/SchemaValidatorTest.java b/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/dom/SchemaValidatorTest.java
index cf3eaff..a3a6c1c 100644
--- a/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/dom/SchemaValidatorTest.java
+++ b/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/dom/SchemaValidatorTest.java
@@ -139,12 +139,12 @@
 		final Validator validator = new WTPVEXValidator();

 		final Document doc = new Document(new Element(P));

 		doc.setValidator(validator);

-		doc.insertElement(1, B);

-		doc.insertElement(2, I);

+		doc.insertElement(2, B);

+		doc.insertElement(3, I);

 

 		assertValidItems(validator, doc.getRootElement(), B, I); // p

-		assertValidItems(validator, doc.getElementForInsertionAt(1), B, I); // b

-		assertValidItems(validator, doc.getElementForInsertionAt(2), B, I); // i

+		assertValidItems(validator, doc.getElementForInsertionAt(2), B, I); // b

+		assertValidItems(validator, doc.getElementForInsertionAt(3), B, I); // i

 	}

 

 	@Test

@@ -164,16 +164,16 @@
 		final Validator validator = new WTPVEXValidator();

 		final Document doc = new Document(new Element(CHAPTER));

 		doc.setValidator(validator);

-		doc.insertElement(1, TITLE);

-		doc.insertElement(3, P);

-		doc.insertElement(4, B);

-		doc.insertElement(5, I);

+		doc.insertElement(2, TITLE);

+		doc.insertElement(4, P);

+		doc.insertElement(5, B);

+		doc.insertElement(6, I);

 

 		assertValidItems(validator, doc.getRootElement(), CHAPTER, TITLE, P); // chapter

-		assertValidItems(validator, doc.getElementForInsertionAt(2)); // title

-		assertValidItems(validator, doc.getElementForInsertionAt(4), B, I); // p

-		assertValidItems(validator, doc.getElementForInsertionAt(5), B, I); // b

-		assertValidItems(validator, doc.getElementForInsertionAt(6), B, I); // i

+		assertValidItems(validator, doc.getElementForInsertionAt(3)); // title

+		assertValidItems(validator, doc.getElementForInsertionAt(5), B, I); // p

+		assertValidItems(validator, doc.getElementForInsertionAt(6), B, I); // b

+		assertValidItems(validator, doc.getElementForInsertionAt(7), B, I); // i

 	}

 

 	@Test

diff --git a/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/layout/TableLayoutTest.java b/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/layout/TableLayoutTest.java
index e1cfd53..feed82c 100644
--- a/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/layout/TableLayoutTest.java
+++ b/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/layout/TableLayoutTest.java
@@ -76,7 +76,7 @@
 	private void resetDocument() {
 		document = new Document(new Element("root"));
 		context.setDocument(document);
-		caretPosition = 1;
+		caretPosition = 2;
 		rootBox = new RootBox(context, document.getRootElement(), 500);
 	}
 
diff --git a/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/layout/TestBlockElementBox.java b/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/layout/TestBlockElementBox.java
index 30014c2..6ac82f8 100644
--- a/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/layout/TestBlockElementBox.java
+++ b/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/layout/TestBlockElementBox.java
@@ -41,7 +41,7 @@
 	public void testBeforeAfter() throws Exception {
 		final Element root = new Element("root");
 		final Document doc = new Document(root);
-		doc.insertElement(1, new QualifiedName(null, "beforeBlock"));
+		doc.insertElement(2, new QualifiedName(null, "beforeBlock"));
 		context.setDocument(doc);
 
 		final RootBox rootBox = new RootBox(context, root, 500);
diff --git a/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/layout/TestBlocksInInlines.java b/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/layout/TestBlocksInInlines.java
index 33924c5..5cf3629 100644
--- a/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/layout/TestBlocksInInlines.java
+++ b/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/layout/TestBlocksInInlines.java
@@ -55,11 +55,11 @@
 		final Document doc = new Document(root);
 		context.setDocument(doc);
 
-		doc.insertText(1, "one  five");
-		doc.insertElement(5, new QualifiedName(null, "b"));
-		doc.insertText(6, "two  four");
-		doc.insertElement(10, new QualifiedName(null, "p"));
-		doc.insertText(11, "three");
+		doc.insertText(2, "one  five");
+		doc.insertElement(6, new QualifiedName(null, "b"));
+		doc.insertText(7, "two  four");
+		doc.insertElement(11, new QualifiedName(null, "p"));
+		doc.insertText(12, "three");
 
 		final RootBox rootBox = new RootBox(context, root, 500);
 		rootBox.layout(context, 0, Integer.MAX_VALUE);
diff --git a/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/layout/TestDocumentTextBox.java b/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/layout/TestDocumentTextBox.java
index b9dea90..d2b7284 100644
--- a/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/layout/TestDocumentTextBox.java
+++ b/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/layout/TestDocumentTextBox.java
@@ -17,9 +17,9 @@
 import org.eclipse.vex.core.internal.css.StyleSheet;
 import org.eclipse.vex.core.internal.css.StyleSheetReader;
 import org.eclipse.vex.core.internal.css.Styles;
+import org.eclipse.vex.core.internal.dom.ContentRange;
 import org.eclipse.vex.core.internal.dom.Document;
 import org.eclipse.vex.core.internal.dom.Element;
-import org.eclipse.vex.core.internal.dom.ContentRange;
 
 /**
  * Tests the DocumentTestBox class. We focus here on proper offsets, since text splitting is tested thoroughly in
@@ -56,8 +56,8 @@
 		// / / / /
 		// baggy orange trousers
 
-		doc.insertText(1, "baggy orange trousers");
-		final DocumentTextBox box = new DocumentTextBox(context, root, 1, 22);
+		doc.insertText(2, "baggy orange trousers");
+		final DocumentTextBox box = new DocumentTextBox(context, root, 2, 23);
 		assertEquals(box.getText().length() * width, box.getWidth());
 		assertEquals(styles.getLineHeight(), box.getHeight());
 		assertSplit(box, 22, false, "baggy orange ", "trousers");
@@ -85,7 +85,7 @@
 		assertSplit(box, 0, true, "b", "aggy orange trousers");
 		assertSplit(box, -1, true, "b", "aggy orange trousers");
 
-		doc.delete(new ContentRange(1, 21));
+		doc.delete(new ContentRange(3, 22));
 	}
 
 	private void assertSplit(final DocumentTextBox box, final int splitPos, final boolean force, final String left, final String right) {
@@ -99,7 +99,7 @@
 		final DocumentTextBox leftBox = (DocumentTextBox) pair.getLeft();
 		final DocumentTextBox rightBox = (DocumentTextBox) pair.getRight();
 
-		final int leftOffset = 1;
+		final int leftOffset = 2;
 		final int midOffset = leftOffset + (left == null ? 0 : left.length());
 		final int rightOffset = leftOffset + box.getText().length();
 
diff --git a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/dom/Document.java b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/dom/Document.java
index dcb5209..fc8b903 100644
--- a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/dom/Document.java
+++ b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/dom/Document.java
@@ -52,7 +52,9 @@
 
 		this.rootElement = rootElement;
 		addChild(rootElement);
-		rootElement.associate(content, content.getRange());
+		content.insertTagMarker(1);
+		content.insertTagMarker(1);
+		rootElement.associate(content, getRange().resizeBy(1, -1));
 	}
 
 	/**
@@ -67,7 +69,10 @@
 	 */
 	public Document(final Content content, final Element rootElement) {
 		Assert.isTrue(content == rootElement.getContent(), "The given root element must already be associated with the given content.");
+		content.insertTagMarker(0);
+		content.insertTagMarker(content.length());
 		associate(content, content.getRange());
+
 		this.rootElement = rootElement;
 		addChild(rootElement);
 	}
@@ -355,7 +360,8 @@
 	 *             if an element with the given qualified name is not allowed a the given offset
 	 */
 	public Element insertElement(final int offset, final QualifiedName elementName) throws DocumentValidationException {
-		Assert.isTrue(offset > rootElement.getStartOffset() && offset <= rootElement.getEndOffset(), MessageFormat.format("Offset must be in [{0}, {1}]", getStartOffset() + 1, getEndOffset()));
+		Assert.isTrue(offset > rootElement.getStartOffset() && offset <= rootElement.getEndOffset(),
+				MessageFormat.format("Offset must be in [{0}, {1}]", rootElement.getStartOffset() + 1, rootElement.getEndOffset()));
 
 		final Element parent = getElementForInsertionAt(offset);
 		if (!canInsertAt(parent, offset, elementName)) {
@@ -550,6 +556,9 @@
 	 */
 	public Element getElementForInsertionAt(final int offset) {
 		final Element parent = getParentElement(getChildNodeAt(offset));
+		if (parent == null) {
+			return null;
+		}
 		if (offset == parent.getStartOffset()) {
 			return parent.getParentElement();
 		}
diff --git a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/widget/VexWidgetImpl.java b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/widget/VexWidgetImpl.java
index f852cd7..fec2339 100644
--- a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/widget/VexWidgetImpl.java
+++ b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/widget/VexWidgetImpl.java
@@ -559,6 +559,10 @@
 		final int endOffset = getEndOffset();
 
 		final Element parent = doc.getElementForInsertionAt(startOffset);
+		if (parent == null) {
+			return new ElementName[0];
+		}
+
 		final List<QualifiedName> nodesBefore = Node.getNodeNames(parent.getChildNodesBefore(startOffset));
 		final List<QualifiedName> nodesAfter = Node.getNodeNames(parent.getChildNodesAfter(endOffset));
 		final List<QualifiedName> selectedNodes = Node.getNodeNames(parent.getChildNodes(new ContentRange(startOffset, endOffset)));
@@ -1195,7 +1199,7 @@
 
 		createRootBox();
 
-		this.moveTo(1);
+		this.moveTo(this.document.getRootElement().getStartOffset() + 1);
 		this.document.addDocumentListener(documentListener);
 	}
 
@@ -1468,7 +1472,7 @@
 		}
 		final Element e1 = getDocument().getElementForInsertionAt(offset - 1);
 		final Element e2 = getDocument().getElementForInsertionAt(offset + 1);
-		return e1 != e2 && e1.getParent() == e2.getParent() && e1.isKindOf(e2);
+		return e1 != e2 && e1 != null && e2 != null && e1.getParent() == e2.getParent() && e1.isKindOf(e2);
 	}
 
 	/**