Skip to content

function InsertBefore - Inserts a child to this node #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions Neslib.Xml.pas
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ TEnumerator = record
private
procedure Free;
procedure InternalAddChild(const AChild: TXmlNode);
procedure InternalInsertBefore(const AChild, AReferenceChild: TXmlNode);
function InternalAddAttribute(const ANameIndex: Integer; const AValue: XmlString): TXmlAttribute;
function GetBlock: PByte; inline;
{$ENDREGION 'Internal Declarations'}
Expand Down Expand Up @@ -460,6 +461,19 @@ TEnumerator = record
This is a shortcut for AddChild(TXmlNodeType.Comment, AComment). }
function AddComment(const AComment: XmlString): TXmlNode; inline;

{ Inserts a child to this node.

Parameters:
AType: the type of node to add.
AValue: the value of the node (depending on AType).
AReferenceChild: The node before which new node is inserted.
If it is null, the new node is inserted at the end of node's child nodes.

Returns:
The newly inserted child node, or nil in case this node is nil.}
function InsertBefore(const AType: TXmlNodeType; const AValue: XmlString;
const AReferenceChild: TXmlNode): TXmlNode;

{ Removes a child element with a given name.

Parameters:
Expand Down Expand Up @@ -1226,6 +1240,22 @@ function TXmlNode.AddText(const AText: XmlString): TXmlNode;
Result := AddChild(TXmlNodeType.Text, AText);
end;

function TXmlNode.InsertBefore(const AType: TXmlNodeType;
const AValue: XmlString; const AReferenceChild: TXmlNode): TXmlNode;
begin
if (FNode = nil) then
begin
Result.FNode := nil;
Exit;
end;

var Doc := GetDocument;
Assert(Doc <> nil);
Result := Doc.CreateNode(AType);
InternalInsertBefore(Result, AReferenceChild);
Result.SetValue(AValue);
end;

function TXmlNode.AddAttribute(const AName: XmlString;
const AValue: Single): TXmlAttribute;
begin
Expand Down Expand Up @@ -1703,6 +1733,27 @@ procedure TXmlNode.InternalAddChild(const AChild: TXmlNode);
end;
end;

procedure TXmlNode.InternalInsertBefore(const AChild,
AReferenceChild: TXmlNode);
begin
Assert(FNode <> nil);
Assert(NodeType = TXmlNodeType.Element);
Assert(AChild <> nil);

AChild.SetParent(Self);
if AReferenceChild = nil then
InternalAddChild(AChild)
else begin
var Prev := AReferenceChild.GetPrevSibling;
Prev.SetNextSibling(AChild);
AChild.SetPrevSibling(Prev);
AChild.SetNextSibling(AReferenceChild);
AReferenceChild.SetPrevSibling(AChild);
if Prev = nil then
SetFirstChild(AChild);
end;
end;

function TXmlNode.NextSiblingByName(const AElementName: XmlString): TXmlNode;
begin
Result.FNode := nil;
Expand Down