summaryrefslogtreecommitdiff
path: root/fpcsrc/packages/fcl-xml/src/xhtml.pp
blob: 729863aca6a6588ec56fca9e2777ab5d618c2274 (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
{
    This file is part of the Free Component Library

    XHTML helper classes
    Copyright (c) 2000 by
      Areca Systems GmbH / Sebastian Guenther, sg@freepascal.org

    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 XHTML;

{$MODE objfpc}
{$H+}

interface

uses DOM, DOM_HTML;

type

  TXHTMLTitleElement = class(THTMLTitleElement);

  TXHTMLHeadElement = class(THTMLHeadElement)
  private
    function GetTitleElement: TXHTMLTitleElement;
  public
    function RequestTitleElement: TXHTMLTitleElement;
    property TitleElement: TXHTMLTitleElement read GetTitleElement;
  end;

  TXHTMLBodyElement = class(THTMLBodyElement);


  TXHTMLType = (xhtmlStrict, xhtmlTransitional);

  TXHTMLDocument = class(THTMLDocument)
  private
    function GetHeadElement: TXHTMLHeadElement;
    function GetBodyElement: TXHTMLBodyElement;
  public
    procedure CreateRoot(XHTMLType: TXHTMLType);
    function RequestHeadElement: TXHTMLHeadElement;
    function RequestBodyElement(const Lang: DOMString): TXHTMLBodyElement;
    property HeadElement: TXHTMLHeadElement read GetHeadElement;
    property BodyElement: TXHTMLBodyElement read GetBodyElement;
  end;



implementation


function TXHTMLHeadElement.RequestTitleElement: TXHTMLTitleElement;
begin
  Result := TitleElement;
  if not Assigned(Result) then
  begin
    Result := TXHTMLTitleElement(OwnerDocument.CreateElement('title'));
    AppendChild(Result);
  end;
end;

function TXHTMLHeadElement.GetTitleElement: TXHTMLTitleElement;
begin
  Result := TXHTMLTitleElement(FindNode('title'));
end;


procedure TXHTMLDocument.CreateRoot(XHTMLType: TXHTMLType);
var
  s: DOMString;
  HtmlEl: TDOMElement;
begin
  case XHTMLType of
    xhtmlStrict:
      s := 'html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"';
    xhtmlTransitional:
      s := 'html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd"';
  end;
  AppendChild(CreateProcessingInstruction('DOCTYPE', s));
  HtmlEl := CreateElement('html');
  AppendChild(HtmlEl);
  HtmlEl['xmlns'] := 'http://www.w3.org/1999/xhtml';
end;

function TXHTMLDocument.RequestHeadElement: TXHTMLHeadElement;
begin
  Result := HeadElement;
  if not Assigned(Result) then
  begin
    Result := TXHTMLHeadElement(CreateElement('head'));
    DocumentElement.AppendChild(Result);
  end;
end;

function TXHTMLDocument.RequestBodyElement(const Lang: DOMString):
  TXHTMLBodyElement;
begin
  Result := BodyElement;
  if not Assigned(Result) then
  begin
    Result := TXHTMLBodyElement(CreateElement('body'));
    DocumentElement.AppendChild(Result);
    Result['xmlns'] := 'http://www.w3.org/1999/xhtml';
    Result['xml:lang'] := Lang;
    Result['lang'] := Lang;
  end;
end;

function TXHTMLDocument.GetHeadElement: TXHTMLHeadElement;
begin
  Result := TXHTMLHeadElement(DocumentElement.FindNode('head'));
end;

function TXHTMLDocument.GetBodyElement: TXHTMLBodyElement;
begin
  Result := TXHTMLBodyElement(DocumentElement.FindNode('body'));
end;


end.