123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- {
- $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 htmlwriter;
- {$mode objfpc}{$H+}
- interface
- uses
- Classes, SysUtils, DOM, htmlelements;
- type
- HTMLWriterException = class (exception);
- { THTMLwriter }
- THTMLwriter = class
- private
- FCurrentElement : THTMLCustomElement;
- FDocument: THTMLDocument;
- procedure SetDocument(const AValue: THTMLDocument);
- procedure SetCurrentElement (AValue : THTMLCustomElement);
- protected
- function CreateElement (tag : THTMLElementClass; s : string) : THTMLCustomElement;
- function CreateElement (tag : THTMLElementClass; sub : THTMLCustomElement) : THTMLCustomElement;
- function CreateElement (tag : THTMLElementClass; subs : Array of THTMLCustomElement) : THTMLCustomElement;
- function CreateElement (tag : THTMLElementClass; subs : TDOMNodelist) : THTMLCustomElement;
- function StartElement (tag : THTMLElementClass) : THTMLCustomElement;
- function EndElement (tag : THTMLElementClass) : THTMLCustomElement;
- function AddElement (tag : THTMLElementClass) : THTMLCustomElement;
- public
- constructor create (aDocument : THTMLDocument);
- procedure AddElement (el : THTMLCustomElement);
- procedure AddElements (subs : TDOMNodelist);
- procedure AddElements (subs : array of THTMLCustomElement);
- function Text (s : string) : THTML_Text;
- function Text (Fmt : string; args : array of const) : THTML_Text;
- { Form input elements }
- function FormText (aname, avalue: DOMstring) : THTML_Input;
- function FormText (aname, avalue: DOMstring; alength : integer) : THTML_Input;
- function FormMemo (aname, avalue: DOMstring; arows,acols: integer) : THTML_Textarea;
- function FormSelect (aname: DOMstring; preselect, size: integer; Options: TStrings; UseValues:boolean) : THTML_Select;
- function FormSelect (aname, preselect: DOMstring; size: integer; Options: TStrings; UseValues:boolean) : THTML_Select;
- function FormPasswd (aname: DOMstring) : THTML_Input;
- function FormCheckbox (aname, avalue: DOMstring; achecked: boolean) : THTML_Input;
- function FormRadio (aname, avalue: DOMstring; achecked: boolean) : THTML_Input;
- function FormSubmit (aname, avalue: DOMstring) : THTML_Input;
- function FormImage (aname, imagesrc, ausemap: DOMstring) : THTML_Input;
- function FormReset : THTML_Input;
- function FormButton (aname, caption, aOnClick: DOMstring) : THTML_Input;
- function FormHidden (aname, aValue: DOMstring) : THTML_Input;
- function FormFile (aname, aValue:DOMstring) : THTML_Input;
- {$i wtagsintf.inc}
- property Document : THTMLDocument read FDocument write SetDocument;
- property CurrentElement : THTMLCustomElement read FCurrentElement write SetCurrentElement;
- end;
- implementation
- uses HTMLDefs;
- resourcestring
- sErrNoCorespondingParent = 'No open element found with tag "%s"';
- { THTMLwriter }
- procedure THTMLwriter.SetDocument(const AValue: THTMLDocument);
- begin
- if FDocument <> AValue then
- begin
- FDocument := AValue;
- FCurrentElement := nil;
- end;
- end;
- function THTMLwriter.CreateElement(tag: THTMLElementClass; s: string): THTMLCustomElement;
- begin
- result := StartElement (tag);
- Text (s);
- EndElement (tag);
- end;
- function THTMLwriter.CreateElement(tag: THTMLElementClass; sub: THTMLCustomElement): THTMLCustomElement;
- begin
- result := StartElement (tag);
- AddElement (sub);
- EndElement (tag);
- end;
- function THTMLwriter.CreateElement(tag: THTMLElementClass; subs: array of THTMLCustomElement): THTMLCustomElement;
- begin
- result := StartElement (tag);
- AddElements (subs);
- EndElement (tag);
- end;
- function THTMLwriter.CreateElement(tag: THTMLElementClass; subs: TDOMNodelist): THTMLCustomElement;
- begin
- result := StartElement (tag);
- AddElements (subs);
- EndElement (tag);
- end;
- function THTMLwriter.StartElement(tag: THTMLElementClass): THTMLCustomElement;
- begin
- result := AddElement (tag);
- FCurrentElement := result;
- end;
- function THTMLwriter.EndElement(tag: THTMLElementClass): THTMLCustomElement;
- var d : TDOMNode;
- begin
- d := FCurrentElement;
- while assigned(d) and not (d is tag) do
- d := d.ParentNode;
- if assigned (d) then
- begin
- result := THTMLCustomElement(d);
- FCurrentElement := THTMLCustomElement(result.ParentNode);
- end
- else
- raise HTMLWriterException.CreateFmt (sErrNoCorespondingParent, [tag.ClassName]);
- end;
- constructor THTMLwriter.create(aDocument: THTMLDocument);
- begin
- inherited create;
- FDocument := aDocument;
- end;
- procedure THTMLwriter.SetCurrentElement(AValue: THTMLCustomElement);
- begin
- if not assigned (AValue) then
- FCurrentElement := nil
- else
- if AValue.OwnerDocument = FDocument then
- FCurrentElement := AValue;
- end;
- function THTMLwriter.AddElement(tag: THTMLElementClass): THTMLCustomElement;
- begin
- result := tag.Create (Document);
- AddElement (result);
- end;
- procedure THTMLwriter.AddElement(el: THTMLCustomElement);
- begin
- if assigned (FCurrentElement) then
- FCurrentElement.AppendChild (el)
- else
- FDocument.AppendChild (el);
- end;
- procedure THTMLwriter.AddElements(subs: TDOMNodelist);
- var r : integer;
- d : TDOMNode;
- begin
- for r := 0 to subs.count-1 do
- begin
- d := subs.item[r];
- if d is THTMLCustomElement then
- AddElement (THTMLCustomElement(d));
- end;
- end;
- procedure THTMLwriter.AddElements(subs: array of THTMLCustomElement);
- var r : integer;
- begin
- for r := 0 to high(subs) do
- AddElement (subs[r]);
- end;
- function THTMLwriter.Text (s : string): THTML_Text;
- begin
- result := THTML_text(AddElement(THTML_Text));
- result.NodeValue := s;
- end;
- function THTMLwriter.Text(Fmt: string; args: array of const): THTML_Text;
- begin
- result := text(format(fmt, args));
- end;
- { Form input elements }
- function THTMLwriter.FormText(aname, avalue: DOMstring): THTML_Input;
- begin
- result := input;
- with result do
- begin
- thetype := itText;
- name := aname;
- value := avalue;
- end;
- end;
- function THTMLwriter.FormText(aname, avalue: DOMstring; alength: integer): THTML_Input;
- begin
- result := FormText (aname, avalue);
- result.size := inttostr(alength);
- end;
- function THTMLwriter.FormMemo(aname, avalue: DOMstring; arows, acols: integer): THTML_Textarea;
- begin
- result := textarea(avalue);
- with result do
- begin
- name := aname;
- rows := inttostr(arows);
- cols := inttostr(acols);
- end;
- end;
- function THTMLwriter.FormSelect(aname: DOMstring; preselect, size: integer;
- Options: TStrings; UseValues:boolean): THTML_Select;
- var r : integer;
- n,v : string;
- begin
- result := StartSelect;
- result.size := inttostr(size);
- result.name := aname;
- if UseValues then
- for r := 0 to options.count-1 do
- begin
- Options.GetNameValue (r, v, n);
- with Option (n) do
- begin
- selected := (preselect = r);
- Value := v;
- end;
- end
- else
- for r := 0 to options.count-1 do
- Option (Options[r]).selected := (preselect = r);
- EndSelect;
- end;
- function THTMLwriter.FormSelect(aname, preselect: DOMstring; size: integer;
- Options: TStrings; UseValues:boolean): THTML_Select;
- begin
- if UseValues then
- result := FormSelect (aname, Options.IndexOfName(preselect), size, Options, UseValues)
- else
- result := FormSelect (aname, Options.IndexOf(preselect), size, Options, UseValues);
- end;
- function THTMLwriter.FormPasswd(aname: DOMstring): THTML_Input;
- begin
- result := input;
- with result do
- begin
- thetype := itPassword;
- name := aname;
- end;
- end;
- function THTMLwriter.FormCheckbox(aname, avalue: DOMstring; achecked: boolean): THTML_Input;
- begin
- result := input;
- with result do
- begin
- thetype := itCheckbox;
- name := aname;
- value := avalue;
- checked := achecked;
- end;
- end;
- function THTMLwriter.FormRadio(aname, avalue: DOMstring; achecked: boolean): THTML_Input;
- begin
- result := input;
- with result do
- begin
- thetype := itCheckbox;
- name := aname;
- value := avalue;
- checked := achecked;
- end;
- end;
- function THTMLwriter.FormSubmit(aname, avalue: DOMstring): THTML_Input;
- begin
- result := input;
- with result do
- begin
- thetype := itSubmit;
- name := aname;
- value := avalue;
- end;
- end;
- function THTMLwriter.FormImage(aname, imagesrc, ausemap: DOMstring): THTML_Input;
- begin
- result := input;
- with result do
- begin
- thetype := itimage;
- name := aname;
- src := imagesrc;
- usemap := ausemap;
- end;
- end;
- function THTMLwriter.FormReset: THTML_Input;
- begin
- result := input;
- result.thetype := itReset;
- end;
- function THTMLwriter.FormButton(aname, caption, aOnClick: DOMstring): THTML_Input;
- begin
- result := input;
- with result do
- begin
- thetype := itButton;
- name := aname;
- value := caption;
- onclick := aonclick;
- end;
- end;
- function THTMLwriter.FormHidden(aname, aValue: DOMstring): THTML_Input;
- begin
- result := Input;
- with result do
- begin
- thetype := itHidden;
- name := aname;
- value := avalue;
- end;
- end;
- function THTMLwriter.FormFile(aname, aValue: DOMstring): THTML_Input;
- begin
- result := Input;
- with result do
- begin
- thetype := itFile;
- name := aname;
- value := aValue;
- end;
- end;
- {$i wtagsimpl.inc}
- end.
|