/// <summary> /// Create a text range spanning the specified markup positions. /// </summary> /// <param name="start">the start point of the text range</param> /// <param name="end">the end point of the text range</param> /// <returns></returns> public IHTMLTxtRange CreateTextRange(MarkupPointer start, MarkupPointer end) { // when switching between wywiwyg and source view sometimes .body is null // and this throws a null ref exception that we catch (can be detected by enabling // exception breaking in the debugger) IHTMLTxtRange range = (Document.body as IHTMLBodyElement).createTextRange(); MarkupServices.MoveRangeToPointers(start, end, range); return(range); }
/// <summary> /// Create a MarkupRange from a TextRange. /// </summary> /// <param name="textRange"></param> /// <returns></returns> public MarkupRange CreateMarkupRange(IHTMLTxtRange textRange) { MarkupPointer Begin = CreateMarkupPointer(); MarkupPointer End = CreateMarkupPointer(); End.Gravity = _POINTER_GRAVITY.POINTER_GRAVITY_Right; MovePointersToRange(textRange, Begin, End); MarkupRange markupRange = new MarkupRange(Begin, End, this); return(markupRange); }
/// <summary> /// Create a MarkupRange from that surrounds an Element. /// </summary> /// <returns></returns> public MarkupRange CreateMarkupRange(IHTMLElement element, bool outside) { _ELEMENT_ADJACENCY beginAdj = outside ? _ELEMENT_ADJACENCY.ELEM_ADJ_BeforeBegin : _ELEMENT_ADJACENCY.ELEM_ADJ_AfterBegin; _ELEMENT_ADJACENCY endAdj = outside ? _ELEMENT_ADJACENCY.ELEM_ADJ_AfterEnd : _ELEMENT_ADJACENCY.ELEM_ADJ_BeforeEnd; MarkupPointer Begin = CreateMarkupPointer(element, beginAdj); MarkupPointer End = CreateMarkupPointer(element, endAdj); End.Gravity = _POINTER_GRAVITY.POINTER_GRAVITY_Right; MarkupRange markupRange = new MarkupRange(Begin, End, this); return(markupRange); }
/// <summary> /// Returns true if the specified element begins and ends within the range. /// </summary> /// <param name="e"></param> /// <returns></returns> private bool isInScope(IHTMLElement e) { MarkupPointer p = MarkupServices.CreateMarkupPointer(e, _ELEMENT_ADJACENCY.ELEM_ADJ_BeforeBegin); if (p.IsRightOfOrEqualTo(Start)) { p = MarkupServices.CreateMarkupPointer(e, _ELEMENT_ADJACENCY.ELEM_ADJ_AfterEnd); if (p.IsLeftOfOrEqualTo(End)) { return(true); } } return(false); }
///// <summary> ///// Gets the elements in the range that match the filter. ///// </summary> ///// <param name="filter">the delegate testing each element to determine if it should be added to the list of elements to return</param> ///// <param name="inScopeElementsOnly">if true, the only</param> ///// <returns></returns> //public IHTMLElement[] GetElements(IHTMLElementFilter filter, bool inScopeElementsOnly) //{ // ArrayList list = new ArrayList(); // if (!IsEmpty()) // { // Hashtable usedElements = new Hashtable(); // MarkupPointer p = MarkupServices.CreateMarkupPointer(Start); // MarkupPointer end = MarkupServices.CreateMarkupPointer(End); // MarkupContext context = p.Right(false); // //move p through the range to locate each the elements adding elements that pass the filter // while (p.IsLeftOfOrEqualTo(end)) // { // if (context.Context == _MARKUP_CONTEXT_TYPE.CONTEXT_TYPE_EnterScope // || context.Context == _MARKUP_CONTEXT_TYPE.CONTEXT_TYPE_ExitScope // || context.Context == _MARKUP_CONTEXT_TYPE.CONTEXT_TYPE_NoScope) // { // if (usedElements[context.Element] == null) // { // if ((inScopeElementsOnly && isInScope(context.Element)) || !inScopeElementsOnly) // if (filter(context.Element)) // { // list.Add(context.Element); // } // //cache the fact that we've already tested this element. // usedElements[context.Element] = context.Element; // } // } // p.Right(true, context); // } // } // return HTMLElementHelper.ToElementArray(list); //} /// <summary> /// Gets the first element in the range that match the filter. /// </summary> /// <param name="filter">the delegate testing each element to determine if it should be added to the list of elements to return</param> /// <param name="inScopeElementsOnly">if true, the only</param> /// <returns></returns> public IHTMLElement GetFirstElement(IHTMLElementFilter filter, bool inScopeElementsOnly) { ArrayList list = new ArrayList(); if (!IsEmpty()) { Hashtable usedElements = new Hashtable(); MarkupPointer p = MarkupServices.CreateMarkupPointer(Start); MarkupPointer end = MarkupServices.CreateMarkupPointer(End); MarkupContext context = p.Right(false); //move p through the range to locate each the elements adding elements that pass the filter while (p.IsLeftOfOrEqualTo(end)) { if (context.Context == _MARKUP_CONTEXT_TYPE.CONTEXT_TYPE_EnterScope || context.Context == _MARKUP_CONTEXT_TYPE.CONTEXT_TYPE_ExitScope || context.Context == _MARKUP_CONTEXT_TYPE.CONTEXT_TYPE_NoScope) { if (usedElements[context.Element] == null) { if ((inScopeElementsOnly && isInScope(context.Element)) || !inScopeElementsOnly) { if (filter(context.Element)) { return(context.Element); } } //cache the fact that we've already tested this element. usedElements[context.Element] = context.Element; } } p.Right(true, context); } } return(null); }
/// <summary> /// Initialize with begin and end pointers /// </summary> /// <param name="start">start</param> /// <param name="end">end</param> /// <param name="markupServices"></param> internal MarkupRange(MarkupPointer start, MarkupPointer end, MshtmlMarkupServices markupServices) { Start = start; End = end; MarkupServices = markupServices; }
/// <summary> /// Positions pointers at the edges of an existing range. /// </summary> /// <param name="start">the pointer positioned at the start of the range</param> /// <param name="end">the pointer position at the end of the range</param> /// <param name="range">the text range to move</param> public void MoveRangeToPointers(MarkupPointer start, MarkupPointer end, IHTMLTxtRange range) { MarkupServices.MoveRangeToPointers(start.PointerRaw, end.PointerRaw, range); }
/// <summary> /// Positions pointers at the edges of an existing range. /// </summary> /// <param name="range">the text range to move to</param> /// <param name="start">the pointer to position at the start of the range</param> /// <param name="end">the pointer to position at the end of the range</param> public void MovePointersToRange(IHTMLTxtRange range, MarkupPointer start, MarkupPointer end) { MarkupServices.MovePointersToRange(range, start.PointerRaw, end.PointerRaw); }
/// <summary> /// Create a MarkupRange from a set of MarkupPointers. /// </summary> /// <param name="start"></param> /// <param name="end"></param> /// <returns></returns> public MarkupRange CreateMarkupRange(MarkupPointer start, MarkupPointer end) { MarkupRange markupRange = new MarkupRange(start, end, this); return(markupRange); }
/// <summary> /// Moves this pointer to another pointer's location. /// </summary> /// <param name="p"></param> public void MoveToPointer(MarkupPointer p) { PointerRaw.MoveToPointer(p.PointerRaw); }