Table of Contents

Class TextEditingExtensions

Namespace
DocWright.Dom.Editing
Assembly
DocWright.Dom.dll

Changes document text at DocumentPositions the way Word does when a person types, deletes, presses Enter or Backspace: insert text, delete a span, split a paragraph, merge a paragraph with the one before it, replace a span. Each returns a DocumentEdit that undoes the edit exactly.

public static class TextEditingExtensions
Inheritance
TextEditingExtensions
Inherited Members

Remarks

The rules are Word's, measured through its object model (WQ-0089 to WQ-0092):

  • Inserted text goes after every zero-width marker at the position, so text typed where a bookmark, hyperlink or field result starts joins it and text typed where one ends does not. It takes the formatting of the character before the position; at a paragraph's start, of the character after; in an empty paragraph, of the paragraph mark. Text typed just past a hyperlink does not take its character style.
  • Splitting keeps the paragraph's properties on both halves. The original mark goes with the second half; the first half's new mark is formatted like the character before the split. At the end of a paragraph whose style names a different next style, the new paragraph takes that style and no direct formatting.
  • Merging (Backspace) keeps the previous paragraph's properties and mark, unless the previous paragraph is empty, when it is the one that goes.
  • Deleting a span that crosses paragraphs removes the paragraphs between and does not join the two ends; replacing such a span joins them.

Every node the edit does not remove keeps its instance and id; nodes it creates get new ids. A field's code, a span that crosses a table cell or covers a table, a split inside an inline content control, an insert inside content a reader synthesized and any edit inside a shape's text box (which a DOCX save writes from the shape's preserved drawing) are refused, never half-done: an operation that throws InvalidOperationException or ArgumentException leaves the document as it was.

Methods

DeleteRange(WordDocument, DocumentSpan)

Deletes the content of a span. Within a paragraph the text and whole inlines it covers go. Across paragraphs the paragraphs between go, the first keeps what precedes the span (and goes too when the span starts at its beginning), the last keeps what follows it, and the two are not joined, as Word's Delete does. A span that is only a paragraph mark joins the two paragraphs, as the Delete key at a paragraph's end does.

InsertBreak(WordDocument, DocumentPosition, BreakKind)

Inserts a page, column or line break at a position, as Word's Ctrl+Enter, Ctrl+Shift+Enter and Shift+Enter do (WQ-0093).

InsertText(WordDocument, DocumentPosition, string, CharacterFormat?)

Inserts text at a position. A \t becomes a tab, a \v a line break, and \r\n, \n or \r a paragraph split, as plain-text paste does.

MergeWithPrevious(WordDocument, Paragraph)

Joins a paragraph to the one before it in the same container, as Backspace at its start does. The first paragraph of a container, and a paragraph that follows a table, have nothing to join: the record is empty.

ReplaceRange(WordDocument, DocumentSpan, string, CharacterFormat?)

Replaces a span with text as one edit, as typing over a selection or an input-method commit does. Across paragraphs the two ends are joined, keeping the first paragraph's properties (the last's when the span starts at the first's beginning). A span that ends at the very start of a later paragraph leaves that paragraph alone, as Word does. The text takes the formatting of the first character the span covers.

SplitParagraph(WordDocument, DocumentPosition)

Splits a paragraph at a position, as Enter does.