Browse Source

* Hopefully final fix for TDOMDocument.DocumentElement:
- Reading this property always delivers the first element in the document
- Removed SetDocumentElement. Use "AppendChild" or one of the other
generic methods for TDOMNode instead.

sg 25 years ago
parent
commit
3d6ba19140
3 changed files with 45 additions and 23 deletions
  1. 23 7
      fcl/xml/dom.pp
  2. 15 10
      fcl/xml/xmlcfg.pp
  3. 7 6
      fcl/xml/xmlread.pp

+ 23 - 7
fcl/xml/dom.pp

@@ -286,11 +286,11 @@ type
   protected
     FDocType: TDOMDocumentType;
     FImplementation: TDOMImplementation;
-    FDocumentElement: TDOMElement;
+    function GetDocumentElement: TDOMElement;
   public
     property DocType: TDOMDocumentType read FDocType;
     property Impl: TDOMImplementation read FImplementation;
-    property DocumentElement: TDOMElement read FDocumentElement;
+    property DocumentElement: TDOMElement read GetDocumentElement;
 
     function CreateElement(const tagName: DOMString): TDOMElement; virtual;
     function CreateDocumentFragment: TDOMDocumentFragment;
@@ -309,7 +309,6 @@ type
     // Extensions to DOM interface:
     constructor Create; virtual;
     function CreateEntity(const data: DOMString): TDOMEntity;
-    procedure SetDocumentElement(ADocumentElement: TDOMElement);
   end;
 
   TXMLDocument = class(TDOMDocument)
@@ -673,7 +672,8 @@ function TDOMNode_WithChildren.ReplaceChild(NewChild, OldChild: TDOMNode):
   TDOMNode;
 begin
   InsertBefore(NewChild, OldChild);
-  RemoveChild(OldChild);
+  if Assigned(OldChild) then
+    RemoveChild(OldChild);
   Result := NewChild;
 end;
 
@@ -932,9 +932,19 @@ begin
   FOwnerDocument := Self;
 end;
 
-procedure TDOMDocument.SetDocumentElement(ADocumentElement: TDOMElement);
+function TDOMDocument.GetDocumentElement: TDOMElement;
+var
+  node: TDOMNode;
 begin
-  FDocumentElement := ADocumentElement;
+  node := FFirstChild;
+  while Assigned(node) do begin
+    if node.FNodeType = ELEMENT_NODE then begin
+      Result := TDOMElement(node);
+      exit;
+    end;
+    node := node.NextSibling;
+  end;
+  Result := nil;
 end;
 
 function TDOMDocument.CreateElement(const tagName: DOMString): TDOMElement;
@@ -1283,7 +1293,13 @@ end.
 
 {
   $Log$
-  Revision 1.11  2000-01-30 22:18:16  sg
+  Revision 1.12  2000-02-13 10:03:31  sg
+  * Hopefully final fix for TDOMDocument.DocumentElement:
+    - Reading this property always delivers the first element in the document
+    - Removed SetDocumentElement. Use "AppendChild" or one of the other
+      generic methods for TDOMNode instead.
+
+  Revision 1.11  2000/01/30 22:18:16  sg
   * Fixed memory leaks, all nodes are now freed by their parent
   * Many cosmetic changes
 

+ 15 - 10
fcl/xml/xmlcfg.pp

@@ -78,15 +78,14 @@ begin
     Close(f);
   end;
 
-  if doc = nil then
+  if not Assigned(doc) then
     doc := TXMLDocument.Create;
 
   cfg :=TDOMElement(doc.FindNode('CONFIG'));
-  if cfg = nil then begin
+  if not Assigned(cfg) then begin
     cfg := doc.CreateElement('CONFIG');
     doc.AppendChild(cfg);
   end;
-  doc.SetDocumentElement(cfg);
 end;
 
 destructor TXMLConfig.Destroy;
@@ -121,17 +120,17 @@ begin
     name := Copy(path, 1, i - 1);
     path := Copy(path, i + 1, Length(path));
     subnode := node.FindNode(name);
-    if subnode = nil then begin
+    if not Assigned(subnode) then begin
       Result := ADefault;
       exit;
     end;
     node := subnode;
   end;
   attr := node.Attributes.GetNamedItem(path);
-  if attr = nil then
-    Result := ADefault
+  if Assigned(attr) then
+    Result := attr.NodeValue
   else
-    Result := attr.NodeValue;
+    Result := ADefault;
 end;
 
 function TXMLConfig.GetValue(const APath: String; ADefault: Integer): Integer;
@@ -172,14 +171,14 @@ begin
     name := Copy(path, 1, i - 1);
     path := Copy(path, i + 1, Length(path));
     subnode := node.FindNode(name);
-    if subnode = nil then begin
+    if not Assigned(subnode) then begin
       subnode := doc.CreateElement(name);
       node.AppendChild(subnode);
     end;
     node := subnode;
   end;
   attr := node.Attributes.GetNamedItem(path);
-  if attr = nil then begin
+  if not Assigned(attr) then begin
     attr := doc.CreateAttribute(path);
     node.Attributes.SetNamedItem(attr);
   end;
@@ -205,7 +204,13 @@ end.
 
 {
   $Log$
-  Revision 1.8  2000-01-30 22:20:08  sg
+  Revision 1.9  2000-02-13 10:03:31  sg
+  * Hopefully final fix for TDOMDocument.DocumentElement:
+    - Reading this property always delivers the first element in the document
+    - Removed SetDocumentElement. Use "AppendChild" or one of the other
+      generic methods for TDOMNode instead.
+
+  Revision 1.8  2000/01/30 22:20:08  sg
   * TXMLConfig now frees its XML document (major memory leak...)
 
   Revision 1.7  2000/01/07 01:24:34  peter

+ 7 - 6
fcl/xml/xmlread.pp

@@ -188,11 +188,6 @@ begin
   ExpectElement(doc);
   ParseMisc(doc);
 
-  if Assigned(LastNodeBeforeDoc) then
-    doc.SetDocumentElement(LastNodeBeforeDoc.NextSibling as TDOMElement)
-  else
-    doc.SetDocumentElement(doc.FirstChild as TDOMElement);
-
   if buf[0] <> #0 then
     RaiseExc('Text after end of document element found');
 
@@ -985,7 +980,13 @@ end.
 
 {
   $Log$
-  Revision 1.14  2000-01-30 22:19:13  sg
+  Revision 1.15  2000-02-13 10:03:31  sg
+  * Hopefully final fix for TDOMDocument.DocumentElement:
+    - Reading this property always delivers the first element in the document
+    - Removed SetDocumentElement. Use "AppendChild" or one of the other
+      generic methods for TDOMNode instead.
+
+  Revision 1.14  2000/01/30 22:19:13  sg
   * Made some optimizations and cosmetic changes
 
   Revision 1.13  2000/01/07 01:24:34  peter