program tidymove;
{
Elementary example of how to use a tTidyBus object.
Grab the contents of the element whose ID is "bar" and
append it to the contents of the element with an ID of "foo"
}
{$IFDEF FPC}{$H+}{$MODE OBJFPC}{$ENDIF}
{$IFDEF WIN32}{$APPTYPE CONSOLE}{$ENDIF}
uses tidyobj;
procedure MyPickupProc(const info:tNodeInfo; user_data:pointer);
var
att:pTidyAttr;
id:string;
b:tTidyBus;
begin
b:=tTidyBus(user_data); // The "TidyBus.Execute" method passes its "self" as user_data
if ( info.Attr <> nil ) then // If we have attributes
begin
att:=tidyAttrGetID(info.node); // Then check for an attribute named "id"
if ( att <> nil ) then // If we have an attribute named "id"
begin
id:=TidyAttrValue(att); // Then get its value
if (id = 'foo') then // If the ID value is "foo"
begin
b.TargetNode:=info.node // Then mark this node as the TARGET
end
else if ( id = 'bar' ) then // Else if the ID value is "bar"
begin
b.SourceNode:=info.node; // Then mark this node as the SOURCE...
b.AddCargo(info.content); // and save its contents.
end;
end;
end;
if ( b.SourceNode <> nil ) // If we have a source
and ( b.TargetNode <> nil ) // and we have a target
then info.Proceed:=False // then we can stop looking.
else ForEachNode(info.doc, info.child, @MyPickupProc, user_data); // Be sure to recurse the children!
end;
const
MyDocument=
'<html><head><title>tidybus test</title></head><body>' +
'<div id="foo">hello</div>' +
'<div id="bar">world</div>' +
'</body></html>';
var
tidy:tTidy;
bus:tTidyBus;
begin
bus:=tTidyBus.Create;
tidy:=tTidy.Create(nil);
tidy.Indent:=-2;
WriteLn('*** Before ***');
WriteLn(tidy.ParseString(MyDocument));
bus.InsertMode:=imAfterLastChild; // <- Insert TidyBus Cargo after TargetNode's last child.
bus.CopyMode:=cmMove; // <- Remove the SourceNode when we are done.
bus.Execute(tidy.Handle, @MyPickupProc); // <- Do it!
WriteLn('*** After ***');
WriteLn(tidy.ParseBuffer(@bus.ResultBuf)); // <- Pretty-up the result
tidy.Free;
bus.Free;
end.