public void RemoveElementsByTagId(_ELEMENT_TAG_ID tagId, bool onlyIfNoAttributes) { if (tagId == _ELEMENT_TAG_ID.TAGID_NULL) { return; } // Remove the tagId up the parent chain IHTMLElement currentElement = ParentElement(); while (currentElement != null) { if (MarkupServices.GetElementTagId(currentElement) == tagId && (!onlyIfNoAttributes || !HTMLElementHelper.HasMeaningfulAttributes(currentElement))) { try { MarkupServices.RemoveElement(currentElement); } catch (COMException e) { Trace.Fail(String.Format("Failed to remove element ({0}) with error: {1}", currentElement.outerHTML, // {0} e // {1} )); } } currentElement = currentElement.parentElement; } // Remove any other instances IHTMLElement[] elements = GetElements(ElementFilters.CreateTagIdFilter(MarkupServices.GetNameForTagId(tagId)), false); foreach (IHTMLElement e in elements) { if (MarkupServices.GetElementTagId(e) == tagId && (!onlyIfNoAttributes || !HTMLElementHelper.HasMeaningfulAttributes(e))) { try { MarkupServices.RemoveElement(e); } catch (COMException ex) { Trace.Fail(String.Format("Failed to remove element ({0}) with error: {1}", e.outerHTML, // {0} ex // {1} )); } } } }
/// <summary> /// Determines if a range has a particular _ELEMENT_TAG_ID applied. /// </summary> /// <param name="tagId"></param> /// <param name="partially">If true, then IsTagId will return true if any part of it is contained within a tagId element. /// If false, then IsTagId will return true only if the range is entirely contained within a tagId element.</param> /// <returns></returns> public bool IsTagId(_ELEMENT_TAG_ID tagId, bool partially) { // This first block of code will return true if the range is entirely contained within an element with the given tagId. IHTMLElement currentElement = ParentElement(); while (currentElement != null) { if (MarkupServices.GetElementTagId(currentElement) == tagId) { return(true); } currentElement = currentElement.parentElement; } // This second block of code will return true if the range is partially contained within an element with the given tagId. if (partially) { IHTMLElement[] elements = GetElements(ElementFilters.CreateTagIdFilter(MarkupServices.GetNameForTagId(tagId)), false); return(elements.Length > 0); } return(false); }