Changeset 24 for branches/lazarus/Common/UHtmlClasses.pas
- Timestamp:
- Sep 8, 2010, 4:17:21 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/lazarus/Common/UHtmlClasses.pas
r23 r24 6 6 7 7 uses 8 UXmlClasses, Classes, SysUtils ;8 UXmlClasses, Classes, SysUtils, Contnrs, UStringListEx; 9 9 10 10 type 11 TStringArray = array of string;12 13 11 TDomainAddress = class(TPersistent) 14 12 private … … 97 95 public 98 96 BlockType: TBlockType; 99 SubItems: T List; // of THtmlElement;97 SubItems: TObjectList; // of THtmlElement; 100 98 constructor Create; 101 99 destructor Destroy; override; … … 113 111 114 112 TSizeUnits = (suPixels, suPercents); 113 115 114 THtmlSize = record 116 115 Width: Integer; … … 130 129 end; 131 130 132 THtml Page= class131 THtmlDocument = class 133 132 private 134 133 function GetAsXmlDocument: TXmlDocument; 135 134 public 136 135 Title: string; 137 Charset: string; 136 ContentEncoding: string; 137 ContentLanguage: string; 138 138 Body: THtmlBlock; 139 Styles: TStringList; 140 Scripts: TStringList; 139 141 property AsXmlDocument: TXmlDocument read GetAsXmlDocument; 140 142 constructor Create; … … 142 144 end; 143 145 144 function Explode(Separator: Char; Source: string): TStringArray;145 146 146 implementation 147 148 function Explode(Separator: Char; Source: string): TStringArray;149 begin150 SetLength(Result, 0);151 while Pos(Separator, Source) > 0 do begin152 SetLength(Result, Length(Result) + 1);153 Result[High(Result)] := Copy(Source, 1, Pos(Separator, Source) - 1);154 Delete(Source, 1, Length(Result[High(Result)]) + 1);155 end;156 SetLength(Result, Length(Result) + 1);157 Result[High(Result)] := Source;158 end;159 147 160 148 function LeftCutString(var Source, Output: string; Delimiter: string; Allowed: string = ''): Boolean; … … 210 198 end; 211 199 212 { THtml Page}213 214 constructor THtml Page.Create;200 { THtmlDocument } 201 202 constructor THtmlDocument.Create; 215 203 begin 216 204 Body := THtmlBlock.Create; 217 end; 218 219 destructor THtmlPage.Destroy; 205 Styles := TStringList.Create; 206 Scripts := TStringList.Create; 207 ContentLanguage := 'en'; 208 ContentEncoding := 'utf-8'; 209 end; 210 211 destructor THtmlDocument.Destroy; 220 212 begin 221 213 Body.Free; 214 Styles.Free; 215 Scripts.Free; 222 216 inherited; 223 217 end; 224 218 225 function THtmlPage.GetAsXmlDocument: TXmlDocument; 219 function THtmlDocument.GetAsXmlDocument: TXmlDocument; 220 var 221 DocType: TXMLTag; 222 HTMLTag: TXMLTag; 223 I: Integer; 226 224 begin 227 225 Result := TXmlDocument.Create; 228 226 with Result, Content do begin 229 227 Formated := True; 230 TagName := 'html'; 231 with TXmlTag(SubElements[SubElements.Add(TXmlTag.Create)]) do begin 232 TagName := 'head'; 228 DocType := TXMlTag.Create; 229 DocType.Name := '!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"'; 230 Doctype.EndTagSymbol := ''; 231 SubElements.Add(DocType); 232 HTMLTag := TXMLTag.Create; 233 with HTMLTag do begin 234 Name := 'html'; 233 235 with TXmlTag(SubElements[SubElements.Add(TXmlTag.Create)]) do begin 234 TagName := 'title'; 235 with TXmlString(SubElements[SubElements.Add(TXmlString.Create)]) do begin 236 Text := Title; 236 Name := 'head'; 237 with TXmlTag(SubElements[SubElements.Add(TXmlTag.Create)]) do begin 238 Name := 'title'; 239 with TXmlString(SubElements[SubElements.Add(TXmlString.Create)]) do begin 240 Text := Title; 241 end; 242 end; 243 with TXmlTag(SubElements[SubElements.Add(TXmlTag.Create)]) do begin 244 Name := 'meta'; 245 Attributes.AddNameValue('http-equiv', 'Content-Language'); 246 Attributes.AddNameValue('content', ContentLanguage); 247 end; 248 with TXmlTag(SubElements[SubElements.Add(TXmlTag.Create)]) do begin 249 Name := 'meta'; 250 Attributes.AddNameValue('http-equiv', 'Content-Type'); 251 Attributes.AddNameValue('content', 'text/html; charset=' + ContentEncoding); 252 end; 253 for I := 0 to Styles.Count - 1 do 254 with TXmlTag(SubElements[SubElements.Add(TXmlTag.Create)]) do begin 255 Name := 'link'; 256 Attributes.AddNameValue('rel', 'stylesheet'); 257 Attributes.AddNameValue('href', Styles[I]); 258 Attributes.AddNameValue('type', 'text/css'); 259 Attributes.AddNameValue('media', 'all'); 260 end; 261 for I := 0 to Scripts.Count - 1 do 262 with TXmlTag(SubElements[SubElements.Add(TXmlTag.Create)]) do begin 263 Name := 'script'; 264 ShringEmpty := False; 265 Attributes.AddNameValue('type', 'text/javascript'); 266 Attributes.AddNameValue('src', Scripts[I]); 237 267 end; 238 268 end; 239 269 with TXmlTag(SubElements[SubElements.Add(TXmlTag.Create)]) do begin 240 TagName := 'meta'; 241 Attributes.AddNameValue('http-equiv', 'Content-Language'); 242 Attributes.AddNameValue('content', 'cs'); 243 end; 244 with TXmlTag(SubElements[SubElements.Add(TXmlTag.Create)]) do begin 245 TagName := 'meta'; 246 Attributes.AddNameValue('http-equiv', 'Content-Type'); 247 Attributes.AddNameValue('content', 'text/html; charset=' + Charset); 270 Name := 'body'; 271 SubElements.Add(Body.AsXmlElement); 248 272 end; 249 273 end; 250 with TXmlTag(SubElements[SubElements.Add(TXmlTag.Create)]) do begin 251 TagName := 'body'; 252 SubElements.Add(Body.AsXmlElement); 253 254 255 end; 274 SubElements.Add(HTMLTag); 256 275 end; 257 276 end; … … 261 280 constructor THtmlBlock.Create; 262 281 begin 263 SubItems := T List.Create;282 SubItems := TObjectList.Create; 264 283 end; 265 284 266 285 destructor THtmlBlock.Destroy; 267 var 268 I: Integer; 269 begin 270 for I := 0 to SubItems.Count - 1 do THtmlElement(SubItems[I]).Free; 286 begin 271 287 SubItems.Free; 272 288 inherited; … … 280 296 with TXmlTag(Result) do begin 281 297 case BlockType of 282 btBlockLevel: TagName := 'div';283 btInline: TagName := 'span';284 btNoTag: TagName := '';298 btBlockLevel: Name := 'div'; 299 btInline: Name := 'span'; 300 btNoTag: Name := ''; 285 301 end; 286 302 for I := 0 to SubItems.Count - 1 do … … 351 367 procedure TIpAddress.SetAsString(const Value: string); 352 368 var 353 Parts: TStringArray; 354 begin 355 Parts := Explode('.', Value); 369 Parts: TStringListEx; 370 begin 356 371 try 372 Parts := TStringListEx.Create; 373 Parts.Explode('.', Value); 374 try 357 375 // if Length(Parts) = 4 then begin 358 Octets[0] := StrToInt(Parts[3]);359 Octets[1] := StrToInt(Parts[2]);360 Octets[2] := StrToInt(Parts[1]);361 Octets[3] := StrToInt(Parts[0]);376 Octets[0] := StrToInt(Parts[3]); 377 Octets[1] := StrToInt(Parts[2]); 378 Octets[2] := StrToInt(Parts[1]); 379 Octets[3] := StrToInt(Parts[0]); 362 380 // end else raise EConvertError.Create('String to IP address conversion error'); 363 except 364 raise EConvertError.Create('String to IP address conversion error'); 381 except 382 raise EConvertError.Create('String to IP address conversion error'); 383 end; 384 finally 385 Parts.Free; 365 386 end; 366 387 end; … … 430 451 procedure TDomainAddress.SetAsString(const Value: string); 431 452 var 432 StrArray: TString Array;453 StrArray: TStringListEx; 433 454 I: Integer; 434 455 begin 435 StrArray := Explode('.', Value); 436 SetLength(Levels, Length(StrArray)); 437 for I := 0 to High(StrArray) do Levels[High(StrArray) - I] := StrArray[I]; 456 try 457 StrArray := TStringListEx.Create; 458 StrArray.Explode('.', Value); 459 SetLength(Levels, StrArray.Count); 460 for I := 0 to StrArray.Count do 461 Levels[StrArray.Count - I] := StrArray[I]; 462 finally 463 StrArray.Free; 464 end; 438 465 end; 439 466 … … 455 482 Result := TXmlTag.Create; 456 483 with TXmlTag(Result) do begin 457 TagName := 'a';484 Name := 'a'; 458 485 Attributes.Add('href='+Target.AsString); 459 486 if Assigned(Content) then SubElements.Add(Content.AsXmlElement); … … 522 549 Result := TXmlTag.Create; 523 550 with TXmlTag(Result) do begin 524 TagName := 'img';551 Name := 'img'; 525 552 Attributes.AddNameValue('src', Source.AsString); 526 553 Attributes.AddNameValue('width', IntToStr(Size.Width));
Note:
See TracChangeset
for help on using the changeset viewer.