-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathHtmlDom.pas
114 lines (93 loc) · 3.23 KB
/
HtmlDom.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
unit HtmlDOM;
interface
uses
Windows, HtmlConst, HtmlTypes, HtmlDll;
type
HtmlElement = class;
IHtmlBehaviorListener = interface
{
procedure OnBehaviorAttach(he: HtmlElement; attached: Boolean);
function OnBehaviorMouse(he: HtmlElement; params: MouseParams): Boolean;
function OnBehaviorKey(he: HtmlElement; params: KeyParams): Boolean;
function OnBehaviorFocus(he: HtmlElement; params: FocusParams): Boolean;
function OnBehaviorTimer(he: HtmlElement; params: TimerParams): Boolean;
function OnBehaviorSize(he: HtmlElement): Boolean;
function OnBehaviorScroll(he: HtmlElement; params: ScrollParams): Boolean;
}
function OnBehaviorDraw(he: HtmlElement; params: pBehaviorDrawParams): Boolean;
{
function OnBehaviorMethodCall(he: HtmlElement; params: MethodParams): Boolean;
function OnBehaviorScriptCall(he: HtmlElement; params: XCallParams): Boolean;
}
function OnBehaviorEvent(he: HtmlElement; params: pBehaviorEventParams): Boolean;
{
function OnBehaviorDataArrived(he: HtmlElement; params: DataArrivedParams): Boolean;
function OnBehaviorExchange(he: HtmlElement; params: ExchangeParams): Boolean;
}
end;
HtmlElement = class
public
class function Create(tag: PAnsiChar; text: PWideChar = nil): HtmlElement;
function FindFirst(sel: PWideChar): HtmlElement;
procedure Insert(e: HtmlElement; index: UINT);
function GetHtml(): AnsiString;
procedure SetHtml(html: WideString; where: UINT);
procedure SetBehaviorHandler(cb: IHtmlBehaviorListener);
procedure RemoveBehaviorHandler(cb: IHtmlBehaviorListener);
end;
implementation
{ HtmlElement }
class function HtmlElement.Create(tag: PAnsiChar; text: PWideChar): HtmlElement;
begin
Result := HTMLayoutCreateElement(tag, text);
end;
function HtmlElement.FindFirst(sel: PWideChar): HtmlElement;
function FirstCallback(he: HELEMENT; param: Pointer): BOOL stdcall;
begin
pHELEMENT(param)^ := he;
Result := True; // stop enum
end;
begin
Result := nil;
HTMLayoutSelectElements(self, sel, @FirstCallback, @Result);
end;
function HtmlElement.GetHtml: AnsiString;
var
w: PChar;
begin
w := nil;
HTMLayoutGetElementHtml(self, w, True);
Assert(false, 'who the hell will free the results?');
Result := w;
end;
procedure HtmlElement.Insert(e: HtmlElement; index: UINT);
begin
HTMLayoutInsertElement(e, self, index);
end;
function HTMLayoutEventThunk(tag: Pointer; he: HELEMENT; event: UINT; params: Pointer): BOOL stdcall;
begin
Result := False;
if tag = nil then Exit;
case event of
HANDLE_BEHAVIOR_EVENT:
Result := IHtmlBehaviorListener(tag).OnBehaviorEvent(he, pBehaviorEventParams(params));
HANDLE_DRAW:
Result := IHtmlBehaviorListener(tag).OnBehaviorDraw(he, pBehaviorDrawParams(params));
end;
end;
procedure HtmlElement.SetBehaviorHandler(cb: IHtmlBehaviorListener);
begin
HTMLayoutAttachEventHandler(self, HTMLayoutEventThunk, Pointer(cb));
end;
procedure HtmlElement.RemoveBehaviorHandler(cb: IHtmlBehaviorListener);
begin
HTMLayoutDetachEventHandler(self, HTMLayoutEventThunk, Pointer(cb));;
end;
procedure HtmlElement.SetHtml(html: WideString; where: UINT);
var
u8: Utf8String;
begin
u8 := Utf8Encode(html);
HTMLayoutSetElementHtml(self, u8, Length(u8), where);
end;
end.