Ejemplo n.º 1
0
        /// <summary>
        /// Creates a new Span instance covering existing content.
        /// </summary>
        /// <param name="start">
        /// Start position of the new Span.
        /// </param>
        /// <param name="end">
        /// End position of the new Span.
        /// </param>
        /// <remarks>
        /// start and end must both be parented by the same Paragraph, otherwise
        /// the method will raise an ArgumentException.
        /// </remarks>
        public Span(TextPointer start, TextPointer end)
        {
            if (start == null)
            {
                throw new ArgumentNullException("start");
            }
            if (end == null)
            {
                throw new ArgumentNullException("start");
            }
            if (start.TextContainer != end.TextContainer)
            {
                throw new ArgumentException(SR.Get(SRID.InDifferentTextContainers, "start", "end"));
            }
            if (start.CompareTo(end) > 0)
            {
                throw new ArgumentException(SR.Get(SRID.BadTextPositionOrder, "start", "end"));
            }

            start.TextContainer.BeginChange();
            try
            {
                start = TextRangeEditTables.EnsureInsertionPosition(start);
                Invariant.Assert(start.Parent is Run);
                end = TextRangeEditTables.EnsureInsertionPosition(end);
                Invariant.Assert(end.Parent is Run);

                if (start.Paragraph != end.Paragraph)
                {
                    throw new ArgumentException(SR.Get(SRID.InDifferentParagraphs, "start", "end"));
                }

                // If start or end positions have a Hyperlink ancestor, we cannot split them.
                Inline nonMergeableAncestor;
                if ((nonMergeableAncestor = start.GetNonMergeableInlineAncestor()) != null)
                {
                    throw new InvalidOperationException(SR.Get(SRID.TextSchema_CannotSplitElement, nonMergeableAncestor.GetType().Name));
                }
                if ((nonMergeableAncestor = end.GetNonMergeableInlineAncestor()) != null)
                {
                    throw new InvalidOperationException(SR.Get(SRID.TextSchema_CannotSplitElement, nonMergeableAncestor.GetType().Name));
                }

                TextElement commonAncestor = TextElement.GetCommonAncestor((TextElement)start.Parent, (TextElement)end.Parent);

                while (start.Parent != commonAncestor)
                {
                    start = SplitElement(start);
                }
                while (end.Parent != commonAncestor)
                {
                    end = SplitElement(end);
                }

                if (start.Parent is Run)
                {
                    start = SplitElement(start);
                }
                if (end.Parent is Run)
                {
                    end = SplitElement(end);
                }

                Invariant.Assert(start.Parent == end.Parent);
                Invariant.Assert(TextSchema.IsValidChild(/*position*/ start, /*childType*/ typeof(Span)));

                this.Reposition(start, end);
            }
            finally
            {
                start.TextContainer.EndChange();
            }
        }
Ejemplo n.º 2
0
 /// <summary>Initializes a new instance of the <see cref="T:System.Windows.Documents.Span" /> class, taking two <see cref="T:System.Windows.Documents.TextPointer" /> objects that indicate the beginning and end of a selection of content that the new <see cref="T:System.Windows.Documents.Span" /> will contain.</summary>
 /// <param name="start">A <see cref="T:System.Windows.Documents.TextPointer" /> that indicates the beginning of a selection of content that the new <see cref="T:System.Windows.Documents.Span" /> will contain.</param>
 /// <param name="end">A <see cref="T:System.Windows.Documents.TextPointer" /> that indicates the end of a selection of content that the new <see cref="T:System.Windows.Documents.Span" /> will contain.</param>
 /// <exception cref="T:System.ArgumentNullException">Raised when <paramref name="start" /> or <paramref name="end" /> is null.</exception>
 /// <exception cref="T:System.ArgumentException">Raised when <paramref name="start" /> and <paramref name="end" /> do not resolve to a range of content suitable for enclosure by a <see cref="T:System.Windows.Documents.Span" /> element; for example, if <paramref name="start" /> and <paramref name="end" /> indicate positions in different paragraphs.</exception>
 // Token: 0x06003543 RID: 13635 RVA: 0x000F1218 File Offset: 0x000EF418
 public Span(TextPointer start, TextPointer end)
 {
     if (start == null)
     {
         throw new ArgumentNullException("start");
     }
     if (end == null)
     {
         throw new ArgumentNullException("start");
     }
     if (start.TextContainer != end.TextContainer)
     {
         throw new ArgumentException(SR.Get("InDifferentTextContainers", new object[]
         {
             "start",
             "end"
         }));
     }
     if (start.CompareTo(end) > 0)
     {
         throw new ArgumentException(SR.Get("BadTextPositionOrder", new object[]
         {
             "start",
             "end"
         }));
     }
     start.TextContainer.BeginChange();
     try
     {
         start = TextRangeEditTables.EnsureInsertionPosition(start);
         Invariant.Assert(start.Parent is Run);
         end = TextRangeEditTables.EnsureInsertionPosition(end);
         Invariant.Assert(end.Parent is Run);
         if (start.Paragraph != end.Paragraph)
         {
             throw new ArgumentException(SR.Get("InDifferentParagraphs", new object[]
             {
                 "start",
                 "end"
             }));
         }
         Inline nonMergeableInlineAncestor;
         if ((nonMergeableInlineAncestor = start.GetNonMergeableInlineAncestor()) != null)
         {
             throw new InvalidOperationException(SR.Get("TextSchema_CannotSplitElement", new object[]
             {
                 nonMergeableInlineAncestor.GetType().Name
             }));
         }
         if ((nonMergeableInlineAncestor = end.GetNonMergeableInlineAncestor()) != null)
         {
             throw new InvalidOperationException(SR.Get("TextSchema_CannotSplitElement", new object[]
             {
                 nonMergeableInlineAncestor.GetType().Name
             }));
         }
         TextElement commonAncestor = TextElement.GetCommonAncestor((TextElement)start.Parent, (TextElement)end.Parent);
         while (start.Parent != commonAncestor)
         {
             start = this.SplitElement(start);
         }
         while (end.Parent != commonAncestor)
         {
             end = this.SplitElement(end);
         }
         if (start.Parent is Run)
         {
             start = this.SplitElement(start);
         }
         if (end.Parent is Run)
         {
             end = this.SplitElement(end);
         }
         Invariant.Assert(start.Parent == end.Parent);
         Invariant.Assert(TextSchema.IsValidChild(start, typeof(Span)));
         base.Reposition(start, end);
     }
     finally
     {
         start.TextContainer.EndChange();
     }
 }