summaryrefslogtreecommitdiff
path: root/packages/fcl-xml/src/htmlelements.pp
blob: 5f0e5a25f32fabeda9399fed360b9c60233bfb75 (plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
{
    $Id: header,v 1.1 2000/07/13 06:33:45 michael Exp $
    This file is part of the Free Component Library (FCL)
    Copyright (c) 1999-2000 by the Free Pascal development team

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 **********************************************************************}
unit htmlelements;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, DOM, HtmlDefs;

type

  TURI = string;

  { THtmlCustomElement }

  THtmlCustomElement = class (TDOMElement)
  private
    FElementTag: THTMLElementTag;
    function GetAttributeName(index:integer): DOMString;
    function GetAttributeValue(index:integer): DOMString;
    function GetTagName: DOMString;
    procedure WriteAttributes (const aStream : TStream);
    procedure WriteSubNodes (const aStream : TStream);
  protected
    function  GetNodeName: DOMString; override;
    procedure StringToStream (const aStream : TStream; s : string);
    procedure StringToStream (const aStream : TStream; Fmt : string; Args : array of const);
    function EscapeString (s : string) : string;
  public
    constructor create (AOwner: TDOMDocument); virtual;

    function AsString : string;
    procedure WriteToStream (const aStream : TStream);  virtual;

    function  GetAttribute(const name: THTMLAttributeTag): DOMString;
    procedure SetAttribute(const name:THTMLAttributeTag; const value: DOMString);
    procedure RemoveAttribute(const name: THTMLAttributeTag);

    property ElementTag : THTMLElementTag read FElementTag write FElementTag;
    property TagName : DOMString read GetTagName;
    property AttributeNames [index:integer] : DOMString read GetAttributeName;
    property AttributeValues [index:integer] : DOMString read GetAttributeValue;
  end;
  THTMLElementClass = class of THTMLCustomELement;


  { THTMLDocument }

  THTMLDocument = class (TDOMDocument)
  public
    procedure SaveToStream (const aStream : TStream);
    procedure SaveToFile (const afilename : string);
    function Asstring : string;
  end;

  { THTMLIDElement }
  
  THTMLIDElement = class (THTMLCustomElement)
  public
    property ID : DOMString index atID read GetAttribute write SetAttribute;
  end;
  
  { THTMLs18nElement }

  THTMLs18nElement = class (THTMLCustomElement)
  private
    function GetDir: THTMLDir;
    procedure SetDir(const AValue: THTMLDir);
  public
    property Dir : THTMLDir read GetDir write SetDir;
    property Lang : DOMString index atLang read GetAttribute write SetAttribute;
  end;
  
  THTMLCoreAttrsElement = class (THTMLIDElement)
  public
    property elementclass : DOMString index atclass read GetAttribute write SetAttribute;
    property style : DOMString index atstyle read GetAttribute write SetAttribute;
    property title : DOMString index attitle read GetAttribute write SetAttribute;
  end;
  
  THTMLCores18nElement = class (THTMLCoreAttrsElement)
  private
    function GetDir: THTMLDir;
    procedure SetDir(const AValue: THTMLDir);
  public
    property Dir : THTMLDir read GetDir write SetDir;
    property Lang : DOMString index atLang read GetAttribute write SetAttribute;
  end;
  
  THTMLAttrsElement = class (THTMLCores18nElement)
    property onclick : DOMString index atonclick read GetAttribute write SetAttribute;
    property ondblclick : DOMString index atondblclick read GetAttribute write SetAttribute;
    property onmousedown : DOMString index atonmousedown read GetAttribute write SetAttribute;
    property onmouseup : DOMString index atonmouseup read GetAttribute write SetAttribute;
    property onmouseover : DOMString index atonmouseover read GetAttribute write SetAttribute;
    property onmousemove : DOMString index atonmousemove read GetAttribute write SetAttribute;
    property onmouseout : DOMString index atonmouseout read GetAttribute write SetAttribute;
    property onkeypress : DOMString index atonkeypress read GetAttribute write SetAttribute;
    property onkeydown : DOMString index atonkeydown read GetAttribute write SetAttribute;
    property onkeyup : DOMString index atonkeyup read GetAttribute write SetAttribute;
  end;
  
// Descendants for all the elements, generated
{$i tagsintf.inc}

  { THTML_text }

  THTML_text = class (THTMLCustomElement)
    FNodeValue : DOMString;
  protected
    function  GetNodeValue: DOMString; override;
    procedure SetNodeValue(const AValue: DOMString); override;
  public
    constructor create (AOwner: TDOMDocument); override;
    procedure WriteToStream (const aStream : TStream);  override;
  end;

function EscapeHTML(const s : String) : String;
function UnescapeHTML(const s : String) : String;

implementation

function EscapeHTML ( const S : String ) : String;
begin
  Result := StringReplace(s,      '&', '&',  [rfReplaceAll]);
  Result := StringReplace(Result, '<', '&lt;',   [rfReplaceAll]);
  Result := StringReplace(Result, '>', '&gt;',   [rfReplaceAll]);
  Result := StringReplace(Result, '"', '&quot;', [rfReplaceAll]);
  Result := StringReplace(Result, #39, '&#39;',  [rfReplaceAll]); // ' - &apos; does not work on ie :(
end;

function UnescapeHTML ( const S : String ) : String;
begin
  Result := StringReplace(s,      '&lt;',   '<', [rfReplaceAll]);
  Result := StringReplace(Result, '&gt;',   '>', [rfReplaceAll]);
  Result := StringReplace(Result, '&quot;', '"', [rfReplaceAll]);
  Result := StringReplace(Result, '&#39;',  #39, [rfReplaceAll]); // '
  Result := StringReplace(Result, '&apos;', #39, [rfReplaceAll]); // '
  Result := StringReplace(Result, '&amp;',  '&', [rfReplaceAll]);
end; 


{ THtmlCustomElement }

function THtmlCustomElement.GetAttributeName(index:integer): DOMString;
var d : TDOMNode;
begin
  d := TDOMNode(Attributes[index]);
  result := d.NodeName;
end;

function THtmlCustomElement.GetAttributeValue(index:integer): DOMString;
var d : TDOMNode;
begin
  d := TDOMNode(Attributes[index]);
  result := d.NodeValue;
end;

function THtmlCustomElement.GetTagName: DOMString;
begin
  result := HTMLElementProps[FElementTag].Name
end;

procedure THtmlCustomElement.WriteAttributes(const aStream: TStream);
var a : THTMLAttributeTag;
    attrs : THTMLAttributeSet;
    s : DOMstring;
begin
  attrs := HTMLElementProps[ElementTag].Attributes;
  for a := low(THTMLAttributeTag) to high(THTMLAttributeTag) do
    if a in attrs then
      begin
      s := GetAttribute (a);
      if s <> '' then
        if a in booleanAttributes then
          StringToStream (aStream, ' %s', [HTMLAttributeTag[a]])
        else
          StringToStream (aStream, ' %s="%s"', [HTMLAttributeTag[a], s]);
      end;
end;

procedure THtmlCustomElement.WriteSubNodes(const aStream: TStream);
var d : TDomNode;
begin
  d := GetFirstChild;
  while assigned (d) do
    begin
    if d is THtmlCustomElement then
      THtmlCustomElement(d).writetostream (aStream);
    d := d.NextSibling;
    end;
end;

function THtmlCustomElement.GetNodeName: DOMString;
begin
  Result:=GetTagName;
end;

procedure THtmlCustomElement.StringToStream(const aStream: TStream; s: string);
begin
  if s <> '' then
    astream.WriteBuffer (s[1], length(s));
end;

procedure THtmlCustomElement.StringToStream(const aStream: TStream; Fmt: string;
  Args: array of const);
begin
  StringToStream (aStream, format (Fmt, args));
end;

function THtmlCustomElement.EscapeString(s: string): string;
begin
  result := EscapeHTML(s);
end;

constructor THtmlCustomElement.create(AOwner: TDOMDocument);
begin
  inherited create (AOwner);
  FElementTag := etUnknown;
end;

function THtmlCustomElement.AsString: string;
var s : TStringStream;
begin
  s := TStringStream.Create ('');
  try
    WriteToStream (s);
    result := s.datastring;
  finally
    s.free;
  end;
end;

procedure THtmlCustomElement.WriteToStream(const aStream: TStream);
var f : THTMLElementFlags;
begin
  StringToStream (aStream, '<%s', [TagName]);
  WriteAttributes (aStream);
  StringToStream (aStream, '>'#13#10);
  f := HTMLElementProps[FELementTag].flags;
  if (efSubelementContent in f) or (efPCDATAContent in f) then
    begin
    WriteSubNodes (aStream);
    StringToStream (aStream, '</%s>'#13#10, [TagName]);
    end;
end;

function THtmlCustomElement.GetAttribute(const name: THTMLAttributeTag): DOMString;
begin
  result := inherited GetAttribute (HTMLAttributeTag[name]);
end;

procedure THtmlCustomElement.SetAttribute(const name: THTMLAttributeTag;
  const value: DOMString);
begin
  inherited SetAttribute (HTMLAttributeTag[name], value);
end;

procedure THtmlCustomElement.RemoveAttribute(const name: THTMLAttributeTag);
begin
  inherited RemoveAttribute (HTMLAttributeTag[name]);
end;

{ THTMLs18nElement }

function THTMLs18nElement.GetDir: THTMLDir;
var r : THTMLDir;
    s : DOMString;
begin
  s := GetAttribute (atDir);
  r := high(THTMLdir);
  while (r > low(THTMLDir)) and (comparetext(s,HTMLDir[r]) <> 0) do
    begin
    dec (r);
    end;
  result := r;
end;

procedure THTMLs18nElement.SetDir(const AValue: THTMLDir);
begin
  if AValue = dirEmpty then
    RemoveAttribute(atDir)
  else
    SetAttribute (atDir, HTMLDir[AValue]);
end;

{ THTMLCores18nElement }

function THTMLCores18nElement.GetDir: THTMLDir;
var r : THTMLDir;
    s : DOMString;
begin
  s := GetAttribute (atDir);
  r := high(THTMLdir);
  while (r > low(THTMLDir)) and (comparetext(s,HTMLDir[r]) <> 0) do
    begin
    dec (r);
    end;
  result := r;
end;

procedure THTMLCores18nElement.SetDir(const AValue: THTMLDir);
begin
  if AValue = dirEmpty then
    RemoveAttribute(atDir)
  else
    SetAttribute (atDir, HTMLDir[AValue]);
end;

// generated implementations
{$i tagsimpl.inc}

{ THTML_text }

function THTML_text.GetNodeValue: DOMString;
begin
  Result := FNodeValue;
end;

procedure THTML_text.SetNodeValue(const AValue: DOMString);
begin
  FNodeValue := AValue;
end;

constructor THTML_text.create (AOwner: TDOMDocument);
begin
  inherited create (AOwner);
  ElementTag := ettext;
end;

procedure THTML_text.WriteToStream(const aStream: TStream);
begin
  StringToStream (aStream, NodeValue+#13#10);
end;

{ THTMLDocument }

procedure THTMLDocument.SaveToStream(const aStream: TStream);
var d : TDOMNode;
begin
  d := FirstChild;
  while assigned(d) do
    begin
    if d is THTMLCustomElement then
      THTMLCustomELement(d).WriteToStream(aStream);
    d := d.NextSibling;
    end;
end;

procedure THTMLDocument.SaveToFile(const afilename: string);
var f : TFileStream;
begin
  f := TFileStream.Create (afilename, fmCreate);
  try
    SaveToStream (f);
  finally
    f.Free;
  end;
end;

function THTMLDocument.Asstring: string;
var s : TStringStream;
begin
  s := TStringStream.Create ('');
  try
    SaveToStream (s);
    result := s.DataString;
  finally
    s.Free;
  end;
end;

end.