Enhancements for access to additional image/hyperlink tags
CIBIS

I have found I needed to write the following routines to support some additional tags.
For your consideration for incorporation into a future release.
Regards
Adam
{ Get Alt tag of selected Image }
function TCustomProfDHTMLEdit.GetImageAltTag: WideString;
var
D: IDispatch;
Document2: IHTMLDocument2;
SelectionObject: IHTMLSelectionObject;
type_, tagName, alt: WideString;
r: IDispatch;
ControlRange: IHTMLControlRange;
Element: IHTMLElement;
ImgElement: IHTMLImgElement;
begin
if not GetDOM(D) then
Result := BusyResult_WideString
else
try
Document2 := D as IHTMLDocument2;
SelectionObject := Document2.selection;
type_ := SelectionObject.type_;
if type_ = 'Control' then
begin
r := SelectionObject.createRange;
ControlRange := r as IHTMLControlRange;
Element := ControlRange.commonParentElement;
tagName := Element.tagName;
if WideSameText(tagName, 'IMG') then
begin
ImgElement := Element as IHTMLImgElement;
alt := ImgElement.alt;
Result := alt
end
else
Result := ''
end
else
Result := ''
except
Result := ErrorResult_WideString
end
end;
{ Update selected image with new source, alt tag, class name }
procedure TCustomProfDHTMLEdit.UpdateImage(newSource, newAlt, newClassName: WideString);
var
D: IDispatch;
SelectionObject: IHTMLSelectionObject;
ControlRange: IHTMLControlRange;
Element: IHTMLElement;
ImgElement: IHTMLImgElement;
begin
if GetDOM(D) then
try
SelectionObject := (D as IHTMLDocument2).selection;
if SelectionObject.type_ = 'Control' then
begin
ControlRange := SelectionObject.createRange as IHTMLControlRange;
Element := ControlRange.commonParentElement;
if Supports(Element, IHTMLImgElement, ImgElement) then
begin
ImgElement.alt := newAlt;
ImgElement.src := newSource;
Element.ClassName := newClassName;
end
end
except
end
end;
{ Is Hyperlink selected, return the title tag also }
function TCustomProfDHTMLEdit.IsHyperlinkSelectedEx(out href, title, target: WideString): Boolean;
var
D: IDispatch;
Document2: IHTMLDocument2;
SelectionObject: IHTMLSelectionObject;
type_, tagName: WideString;
r: IDispatch;
ControlRange: IHTMLControlRange;
TxtRange: IHTMLTxtRange;
Element: IHTMLElement;
WideStr: WideString;
begin
Result := False;
if GetDOM(D) then
try
Document2 := D as IHTMLDocument2;
SelectionObject := Document2.selection;
r := SelectionObject.createRange;
type_ := SelectionObject.type_;
if type_ = 'Control' then
begin
ControlRange := r as IHTMLControlRange;
Element := ControlRange.commonParentElement
end
else
begin
TxtRange := r as IHTMLTxtRange;
Element := TxtRange.parentElement
end;
while True do
begin
if not Assigned(Element) then Break;
tagName := Element.tagName;
if WideSameText(tagName, 'A') then
begin
WideStr := Element.getAttribute('href', 0);
href := AdjustURI(D, TAGID_A, WideStr);
target := Element.getAttribute('target', 0);
title := Element.getAttribute('title', 0);
Result := True;
Break
end;
Element := Element.parentElement
end
except
end
end;
{ Update Hyperlink with new href, title tag, target and class name }
function TCustomProfDHTMLEdit.UpdateHyperlink(const newHref, newTitle, newTarget, newClassName: WideString): Boolean;
var
D: IDispatch;
Document2: IHTMLDocument2;
SelectionObject: IHTMLSelectionObject;
type_, tagName, innerText: WideString;
r: IDispatch;
ControlRange: IHTMLControlRange;
TxtRange: IHTMLTxtRange;
Element: IHTMLElement;
AttributeValue: OleVariant;
begin
Result := False;
if GetDOM(D) then
try
Document2 := D as IHTMLDocument2;
SelectionObject := Document2.selection;
r := SelectionObject.createRange;
type_ := SelectionObject.type_;
if type_ = 'Control' then
begin
ControlRange := r as IHTMLControlRange;
Element := ControlRange.commonParentElement
end
else
begin
TxtRange := r as IHTMLTxtRange;
Element := TxtRange.parentElement
end;
while True do
begin
if not Assigned(Element) then Break;
tagName := Element.tagName;
Element.ClassName := newClassName;
if WideSameText(tagName, 'A') then
begin
innerText := Element.innerText;
AttributeValue := newHref;
Element.setAttribute('href', AttributeValue, 0);
if innerText <> Element.innerText then
Element.innerText := innerText;
if newTarget = '' then
Element.removeAttribute('target', 0)
else
begin
AttributeValue := newTarget;
Element.setAttribute('target', AttributeValue, 0)
end;
if newTitle = '' then
Element.removeAttribute('title', 0)
else
begin
AttributeValue := newTitle;
Element.setAttribute('title', AttributeValue, 0)
end;
Result := True;
Break
end;
Element := Element.parentElement
end
except
end
end;