12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587 |
- {
- This file is part of the Free Component Library
- Implementation of DOM interfaces
- Copyright (c) 1999-2000 by Sebastian Guenther, [email protected]
- Modified in 2006 by Sergei Gorelkin, [email protected]
- 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.
- **********************************************************************}
- {
- This unit provides classes which implement the interfaces defined in the
- DOM (Document Object Model) specification.
- The current state is:
- DOM Levels 1 and 2 - Completely implemented
- DOM Level 3 - Partially implemented
- Specification used for this implementation:
- "Document Object Model (DOM) Level 2 Specification Version 1.0
- W3C Recommendation 11 November, 2000"
- http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113
- }
- unit DOM;
- {$ifdef fpc}
- {$MODE objfpc}{$H+}
- {$endif}
- interface
- uses
- SysUtils, Classes, xmlutils, dtdmodel;
- // -------------------------------------------------------
- // DOMException
- // -------------------------------------------------------
- const
- // DOM Level 1 exception codes:
- INDEX_SIZE_ERR = 1; // index or size is negative, or greater than the allowed value
- DOMSTRING_SIZE_ERR = 2; // Specified range of text does not fit into a DOMString
- HIERARCHY_REQUEST_ERR = 3; // node is inserted somewhere it does not belong
- WRONG_DOCUMENT_ERR = 4; // node is used in a different document than the one that created it (that does not support it)
- INVALID_CHARACTER_ERR = 5; // invalid or illegal character is specified, such as in a name
- NO_DATA_ALLOWED_ERR = 6; // data is specified for a node which does not support data
- NO_MODIFICATION_ALLOWED_ERR = 7; // an attempt is made to modify an object where modifications are not allowed
- NOT_FOUND_ERR = 8; // an attempt is made to reference a node in a context where it does not exist
- NOT_SUPPORTED_ERR = 9; // implementation does not support the type of object requested
- INUSE_ATTRIBUTE_ERR = 10; // an attempt is made to add an attribute that is already in use elsewhere
- // DOM Level 2 exception codes:
- INVALID_STATE_ERR = 11; // an attempt is made to use an object that is not, or is no longer, usable
- SYNTAX_ERR = 12; // invalid or illegal string specified
- INVALID_MODIFICATION_ERR = 13; // an attempt is made to modify the type of the underlying object
- NAMESPACE_ERR = 14; // an attempt is made to create or change an object in a way which is incorrect with regard to namespaces
- INVALID_ACCESS_ERR = 15; // parameter or operation is not supported by the underlying object
- // -------------------------------------------------------
- // Node
- // -------------------------------------------------------
- const
- ELEMENT_NODE = 1;
- ATTRIBUTE_NODE = 2;
- TEXT_NODE = 3;
- CDATA_SECTION_NODE = 4;
- ENTITY_REFERENCE_NODE = 5;
- ENTITY_NODE = 6;
- PROCESSING_INSTRUCTION_NODE = 7;
- COMMENT_NODE = 8;
- DOCUMENT_NODE = 9;
- DOCUMENT_TYPE_NODE = 10;
- DOCUMENT_FRAGMENT_NODE = 11;
- NOTATION_NODE = 12;
- type
- TDOMDocument = class;
- TDOMNodeList = class;
- TDOMNamedNodeMap = class;
- TDOMAttr = class;
- TDOMElement = class;
- TDOMText = class;
- TDOMComment = class;
- TDOMCDATASection = class;
- TDOMDocumentType = class;
- TDOMEntityReference = class;
- TDOMProcessingInstruction = class;
- TNodePool = class;
- PNodePoolArray = ^TNodePoolArray;
- {$ifdef CPU16}
- TNodePoolArray = array[0..MaxSmallInt div sizeof(Pointer)-1] of TNodePool;
- {$else CPU16}
- TNodePoolArray = array[0..MaxInt div sizeof(Pointer)-1] of TNodePool;
- {$endif CPU16}
- {$ifndef fpc}
- TFPList = TList;
- {$endif}
- // -------------------------------------------------------
- // DOMString
- // -------------------------------------------------------
- TSetOfChar = xmlutils.TSetOfChar; { to be removed: not used in DOM unit }
- DOMString = XMLString;
- DOMPChar = PXMLChar;
- PDOMString = ^DOMString;
- EDOMError = class(Exception)
- public
- Code: Integer;
- constructor Create(ACode: Integer; const ASituation: String);
- end;
- EDOMIndexSize = class(EDOMError)
- public
- constructor Create(const ASituation: String);
- end;
- EDOMHierarchyRequest = class(EDOMError)
- public
- constructor Create(const ASituation: String);
- end;
- EDOMWrongDocument = class(EDOMError)
- public
- constructor Create(const ASituation: String);
- end;
- EDOMNotFound = class(EDOMError)
- public
- constructor Create(const ASituation: String);
- end;
- EDOMNotSupported = class(EDOMError)
- public
- constructor Create(const ASituation: String);
- end;
- EDOMInUseAttribute = class(EDOMError)
- public
- constructor Create(const ASituation: String);
- end;
- EDOMInvalidState = class(EDOMError)
- public
- constructor Create(const ASituation: String);
- end;
- EDOMSyntax = class(EDOMError)
- public
- constructor Create(const ASituation: String);
- end;
- EDOMInvalidModification = class(EDOMError)
- public
- constructor Create(const ASituation: String);
- end;
- EDOMNamespace = class(EDOMError)
- public
- constructor Create(const ASituation: String);
- end;
- EDOMInvalidAccess = class(EDOMError)
- public
- constructor Create(const ASituation: String);
- end;
- { NodeType, NodeName and NodeValue had been moved from fields to functions.
- This lowers memory usage and also obsoletes most constructors,
- at a slight performance penalty. However, NodeName and NodeValue are
- accessible via fields using specialized properties of descendant classes,
- e.g. TDOMElement.TagName, TDOMCharacterData.Data etc.}
- TNodeFlagEnum = (
- nfReadonly,
- nfRecycled,
- nfLevel2,
- nfIgnorableWS,
- nfSpecified,
- nfDestroying,
- nfFirstChild
- );
- TNodeFlags = set of TNodeFlagEnum;
- TDOMNode = class
- protected
- FPool: TObject;
- FFlags: TNodeFlags;
- FParentNode: TDOMNode;
- FPreviousSibling, FNextSibling: TDOMNode;
- FOwnerDocument: TDOMDocument;
- function GetNodeName: DOMString; virtual; abstract;
- function GetNodeValue: DOMString; virtual;
- function GetParentNode: TDOMNode; virtual;
- procedure SetNodeValue(const AValue: DOMString); virtual;
- function GetFirstChild: TDOMNode; virtual;
- function GetLastChild: TDOMNode; virtual;
- function GetPreviousSibling: TDOMNode; virtual;
- function GetAttributes: TDOMNamedNodeMap; virtual;
- function GetRevision: Integer;
- function GetNodeType: Integer; virtual; abstract;
- function GetTextContent: DOMString; virtual;
- procedure SetTextContent(const AValue: DOMString); virtual;
- function GetLocalName: DOMString; virtual;
- function GetNamespaceURI: DOMString; virtual;
- function GetPrefix: DOMString; virtual;
- procedure SetPrefix(const Value: DOMString); virtual;
- function GetOwnerDocument: TDOMDocument; virtual;
- function GetBaseURI: DOMString;
- procedure SetReadOnly(Value: Boolean);
- procedure Changing;
- public
- constructor Create(AOwner: TDOMDocument);
- destructor Destroy; override;
- procedure FreeInstance; override;
- function GetChildNodes: TDOMNodeList;
- property NodeName: DOMString read GetNodeName;
- property NodeValue: DOMString read GetNodeValue write SetNodeValue;
- property NodeType: Integer read GetNodeType;
- property ParentNode: TDOMNode read GetParentNode;
- property FirstChild: TDOMNode read GetFirstChild;
- property LastChild: TDOMNode read GetLastChild;
- property ChildNodes: TDOMNodeList read GetChildNodes;
- property PreviousSibling: TDOMNode read GetPreviousSibling;
- property NextSibling: TDOMNode read FNextSibling;
- property Attributes: TDOMNamedNodeMap read GetAttributes;
- property OwnerDocument: TDOMDocument read GetOwnerDocument;
- function InsertBefore(NewChild, RefChild: TDOMNode): TDOMNode; virtual;
- function ReplaceChild(NewChild, OldChild: TDOMNode): TDOMNode; virtual;
- function DetachChild(OldChild: TDOMNode): TDOMNode; virtual;
- function RemoveChild(OldChild: TDOMNode): TDOMNode;
- function AppendChild(NewChild: TDOMNode): TDOMNode;
- function HasChildNodes: Boolean; virtual;
- function CloneNode(deep: Boolean): TDOMNode; overload; virtual;
- // DOM level 2
- function IsSupported(const Feature, Version: DOMString): Boolean;
- function HasAttributes: Boolean; virtual;
- procedure Normalize; virtual;
- property NamespaceURI: DOMString read GetNamespaceURI;
- property LocalName: DOMString read GetLocalName;
- property Prefix: DOMString read GetPrefix write SetPrefix;
- // DOM level 3
- property TextContent: DOMString read GetTextContent write SetTextContent;
- function LookupPrefix(const nsURI: DOMString): DOMString;
- function LookupNamespaceURI(const APrefix: DOMString): DOMString;
- function IsDefaultNamespace(const nsURI: DOMString): Boolean;
- property baseURI: DOMString read GetBaseURI;
- // Extensions to DOM interface:
- function CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode; overload; virtual;
- function FindNode(const ANodeName: DOMString): TDOMNode; virtual;
- function CompareName(const name: DOMString): Integer; virtual;
- property Flags: TNodeFlags read FFlags;
- end;
- TDOMNodeClass = class of TDOMNode;
- TDOMElementClass = class of TDOMElement;
- { The following class is an implementation specific extension, it is just an
- extended implementation of TDOMNode, the generic DOM::Node interface
- implementation. (Its main purpose is to save memory in a big node tree) }
- TDOMNode_WithChildren = class(TDOMNode)
- protected
- FFirstChild: TDOMNode;
- FChildNodes: TDOMNodeList;
- function GetFirstChild: TDOMNode; override;
- function GetLastChild: TDOMNode; override;
- procedure CloneChildren(ACopy: TDOMNode; ACloneOwner: TDOMDocument);
- procedure FreeChildren;
- function GetTextContent: DOMString; override;
- procedure SetTextContent(const AValue: DOMString); override;
- public
- destructor Destroy; override;
- function InsertBefore(NewChild, RefChild: TDOMNode): TDOMNode; override;
- function ReplaceChild(NewChild, OldChild: TDOMNode): TDOMNode; override;
- function DetachChild(OldChild: TDOMNode): TDOMNode; override;
- function HasChildNodes: Boolean; override;
- function FindNode(const ANodeName: DOMString): TDOMNode; override;
- procedure InternalAppend(NewChild: TDOMNode);
- end;
- { A common ancestor for Document and Entity nodes. }
- TDOMNode_TopLevel = class(TDOMNode_WithChildren)
- protected
- FInputEncoding: DOMString;
- FXMLEncoding: DOMString;
- FURI: DOMString;
- FXMLVersion: TXMLVersion;
- function GetXMLVersion: DOMString;
- public
- property InputEncoding: DOMString read FInputEncoding;
- property XMLEncoding: DOMString read FXMLEncoding;
- end;
- // -------------------------------------------------------
- // NodeList
- // -------------------------------------------------------
- TFilterResult = (frFalse, frNorecurseFalse, frTrue, frNorecurseTrue);
- TDOMNodeList = class(TObject)
- protected
- FNode: TDOMNode;
- FRevision: Integer;
- FList: TFPList;
- function GetCount: LongWord;
- function GetItem(index: LongWord): TDOMNode;
- function NodeFilter(aNode: TDOMNode): TFilterResult; virtual;
- // now deprecated in favor of NodeFilter
- procedure BuildList; virtual;
- public
- constructor Create(ANode: TDOMNode);
- destructor Destroy; override;
- property Item[index: LongWord]: TDOMNode read GetItem; default;
- property Count: LongWord read GetCount;
- property Length: LongWord read GetCount;
- end;
- { an extension to DOM interface, used to build recursive lists of elements }
- TDOMElementList = class(TDOMNodeList)
- protected
- filter: DOMString;
- FNSIndexFilter: Integer;
- localNameFilter: DOMString;
- FMatchNS: Boolean;
- FMatchAnyNS: Boolean;
- UseFilter: Boolean;
- function NodeFilter(aNode: TDOMNode): TFilterResult; override;
- public
- constructor Create(ANode: TDOMNode; const AFilter: DOMString); overload;
- constructor Create(ANode: TDOMNode; const nsURI, localName: DOMString); overload;
- end;
- // -------------------------------------------------------
- // NamedNodeMap
- // -------------------------------------------------------
- TDOMNamedNodeMap = class(TObject)
- protected
- FOwner: TDOMNode;
- FList: TFPList;
- function GetItem(index: LongWord): TDOMNode;
- function GetLength: LongWord;
- function Find(const name: DOMString; out Index: LongWord): Boolean;
- function Delete(index: LongWord): TDOMNode; virtual;
- function InternalRemove(const name: DOMString): TDOMNode;
- function ValidateInsert(arg: TDOMNode): Integer; virtual;
- public
- constructor Create(AOwner: TDOMNode);
- destructor Destroy; override;
- function GetNamedItem(const name: DOMString): TDOMNode;
- function SetNamedItem(arg: TDOMNode): TDOMNode; virtual;
- function RemoveNamedItem(const name: DOMString): TDOMNode;
- // Introduced in DOM Level 2:
- function getNamedItemNS(const namespaceURI, localName: DOMString): TDOMNode; virtual;
- function setNamedItemNS(arg: TDOMNode): TDOMNode; virtual;
- function removeNamedItemNS(const namespaceURI,localName: DOMString): TDOMNode; virtual;
- property Item[index: LongWord]: TDOMNode read GetItem; default;
- property Length: LongWord read GetLength;
- end;
- // -------------------------------------------------------
- // CharacterData
- // -------------------------------------------------------
- TDOMCharacterData = class(TDOMNode)
- private
- FNodeValue: DOMString;
- protected
- function GetLength: LongWord;
- function GetNodeValue: DOMString; override;
- procedure SetNodeValue(const AValue: DOMString); override;
- public
- property Data: DOMString read FNodeValue write SetNodeValue;
- property Length: LongWord read GetLength;
- function SubstringData(offset, count: LongWord): DOMString;
- procedure AppendData(const arg: DOMString);
- procedure InsertData(offset: LongWord; const arg: DOMString);
- procedure DeleteData(offset, count: LongWord);
- procedure ReplaceData(offset, count: LongWord; const arg: DOMString);
- end;
- // -------------------------------------------------------
- // DOMImplementation
- // -------------------------------------------------------
- TDOMImplementation = class
- public
- function HasFeature(const feature, version: DOMString): Boolean;
- // Introduced in DOM Level 2:
- function CreateDocumentType(const QualifiedName, PublicID,
- SystemID: DOMString): TDOMDocumentType;
- function CreateDocument(const NamespaceURI, QualifiedName: DOMString;
- doctype: TDOMDocumentType): TDOMDocument;
- end;
- // -------------------------------------------------------
- // DocumentFragment
- // -------------------------------------------------------
- TDOMDocumentFragment = class(TDOMNode_WithChildren)
- protected
- function GetNodeType: Integer; override;
- function GetNodeName: DOMString; override;
- public
- function CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode; overload; override;
- end;
- // -------------------------------------------------------
- // Document
- // -------------------------------------------------------
- // TODO: to be replaced by more suitable container
- TNamespaces = array of DOMString;
- TDOMDocument = class(TDOMNode_TopLevel)
- protected
- FIDList: THashTable;
- FRevision: Integer;
- FImplementation: TDOMImplementation;
- FNamespaces: TNamespaces;
- FNames: THashTable;
- FEmptyNode: TDOMElement;
- FNodeLists: THashTable;
- FMaxPoolSize: Integer;
- FPools: PNodePoolArray;
- FXmlStandalone: Boolean;
- FStdUri_xml: PHashItem;
- FStdUri_xmlns: PHashItem;
- function GetDocumentElement: TDOMElement;
- function GetDocType: TDOMDocumentType;
- function GetNodeType: Integer; override;
- function GetNodeName: DOMString; override;
- function GetTextContent: DOMString; override;
- function GetOwnerDocument: TDOMDocument; override;
- procedure SetTextContent(const value: DOMString); override;
- procedure RemoveID(Elem: TDOMElement);
- function GetChildNodeList(aNode: TDOMNode): TDOMNodeList;
- function GetElementList(aNode: TDOMNode; const nsURI, aLocalName: DOMString; UseNS: Boolean): TDOMNodeList;
- procedure NodeListDestroyed(aList: TDOMNodeList);
- function Alloc(AClass: TDOMNodeClass): TDOMNode;
- procedure SetXMLVersion(const aValue: DOMString); virtual;
- procedure SetXMLStandalone(aValue: Boolean); virtual;
- function ValidateQName(const nsUri, qName: DOMString; out nsidx: PHashItem): Integer;
- public
- function IndexOfNS(const nsURI: DOMString; AddIfAbsent: Boolean = False): Integer;
- function InsertBefore(NewChild, RefChild: TDOMNode): TDOMNode; override;
- function ReplaceChild(NewChild, OldChild: TDOMNode): TDOMNode; override;
- property DocType: TDOMDocumentType read GetDocType;
- property Impl: TDOMImplementation read FImplementation;
- property DocumentElement: TDOMElement read GetDocumentElement;
- function CreateElement(const tagName: DOMString): TDOMElement; virtual;
- function CreateElementBuf(Buf: DOMPChar; Length: Integer): TDOMElement;
- function CreateDocumentFragment: TDOMDocumentFragment;
- function CreateTextNode(const data: DOMString): TDOMText;
- function CreateTextNodeBuf(Buf: DOMPChar; Length: Integer; IgnWS: Boolean): TDOMText;
- function CreateComment(const data: DOMString): TDOMComment;
- function CreateCommentBuf(Buf: DOMPChar; Length: Integer): TDOMComment;
- function CreateCDATASection(const data: DOMString): TDOMCDATASection;
- virtual;
- function CreateProcessingInstruction(const target, data: DOMString):
- TDOMProcessingInstruction; virtual;
- function CreateAttribute(const name: DOMString): TDOMAttr;
- function CreateAttributeBuf(Buf: DOMPChar; Length: Integer): TDOMAttr;
- function CreateEntityReference(const name: DOMString): TDOMEntityReference;
- virtual;
- function GetElementsByTagName(const tagname: DOMString): TDOMNodeList;
- // DOM level 2 methods
- function ImportNode(ImportedNode: TDOMNode; Deep: Boolean): TDOMNode;
- function CreateElementNS(const nsURI, QualifiedName: DOMString): TDOMElement; overload;
- function CreateElementNS(const nsURI, QualifiedName: DOMString;
- AClass: TDOMElementClass): TDOMElement; overload;
- function CreateAttributeNS(const nsURI, QualifiedName: DOMString): TDOMAttr;
- function GetElementsByTagNameNS(const nsURI, alocalName: DOMString): TDOMNodeList;
- function GetElementById(const ElementID: DOMString): TDOMElement;
- // DOM level 3:
- property documentURI: DOMString read FURI write FURI;
- property XMLVersion: DOMString read GetXMLVersion write SetXMLVersion;
- property XMLStandalone: Boolean read FXmlStandalone write SetXmlStandalone;
- // Extensions to DOM interface:
- constructor Create; virtual;
- destructor Destroy; override;
- function CloneNode(deep: Boolean): TDOMNode; overload; override;
- property Names: THashTable read FNames;
- property IDs: THashTable read FIDList write FIDList;
- end;
- TXMLDocument = class(TDOMDocument)
- protected
- procedure SetXMLVersion(const aValue: DOMString); override;
- procedure SetXMLStandalone(aValue: Boolean); override;
- public
- // These fields are extensions to the DOM interface:
- StylesheetType, StylesheetHRef: DOMString;
- constructor Create; override;
- function CreateCDATASection(const data: DOMString): TDOMCDATASection; override;
- function CreateProcessingInstruction(const target, data: DOMString):
- TDOMProcessingInstruction; override;
- function CreateEntityReference(const name: DOMString): TDOMEntityReference; override;
- end;
- // This limits number of namespaces per document to 65535,
- // and prefix length to 65535, too.
- // I believe that higher values may only be found in deliberately malformed documents.
- TNamespaceInfo = packed record
- NSIndex: Word;
- PrefixLen: Word;
- QName: PHashItem;
- end;
- // -------------------------------------------------------
- // Attr
- // -------------------------------------------------------
- TAttrDataType = xmlutils.TAttrDataType;
- TDOMNode_NS = class(TDOMNode_WithChildren)
- protected
- FNSI: TNamespaceInfo;
- function GetNodeName: DOMString; override;
- function GetLocalName: DOMString; override;
- function GetNamespaceURI: DOMString; override;
- function GetPrefix: DOMString; override;
- procedure SetPrefix(const Value: DOMString); override;
- public
- { Used by parser }
- procedure SetNSI(const nsUri: DOMString; ColonPos: Integer);
- function CompareName(const AName: DOMString): Integer; override;
- property NSI: TNamespaceInfo read FNSI;
- end;
- TDOMAttr = class(TDOMNode_NS)
- protected
- FDataType: TAttrDataType;
- function GetNodeValue: DOMString; override;
- function GetNodeType: Integer; override;
- function GetParentNode: TDOMNode; override;
- function GetSpecified: Boolean;
- function GetIsID: Boolean;
- function GetOwnerElement: TDOMElement;
- procedure SetNodeValue(const AValue: DOMString); override;
- public
- destructor Destroy; override;
- function CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode; overload; override;
- property Name: DOMString read GetNodeName;
- property Specified: Boolean read GetSpecified;
- property Value: DOMString read GetNodeValue write SetNodeValue;
- property OwnerElement: TDOMElement read GetOwnerElement;
- property IsID: Boolean read GetIsID;
- // extensions
- // TODO: this is to be replaced with DOM 3 TypeInfo
- property DataType: TAttrDataType read FDataType write FDataType;
- end;
- // -------------------------------------------------------
- // Element
- // -------------------------------------------------------
- TDOMElement = class(TDOMNode_NS)
- protected
- FAttributes: TDOMNamedNodeMap;
- function GetNodeType: Integer; override;
- function GetAttributes: TDOMNamedNodeMap; override;
- procedure AttachDefaultAttrs;
- function InternalLookupPrefix(const nsURI: DOMString; Original: TDOMElement): DOMString;
- procedure RestoreDefaultAttr(AttrDef: TAttributeDef);
- public
- destructor Destroy; override;
- function CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode; overload; override;
- procedure Normalize; override;
- property TagName: DOMString read GetNodeName;
- function GetAttribute(const name: DOMString): DOMString;
- procedure SetAttribute(const name, value: DOMString);
- procedure RemoveAttribute(const name: DOMString);
- function GetAttributeNode(const name: DOMString): TDOMAttr;
- function SetAttributeNode(NewAttr: TDOMAttr): TDOMAttr;
- function RemoveAttributeNode(OldAttr: TDOMAttr): TDOMAttr;
- function GetElementsByTagName(const name: DOMString): TDOMNodeList;
- // Introduced in DOM Level 2:
- function GetAttributeNS(const nsURI, aLocalName: DOMString): DOMString;
- procedure SetAttributeNS(const nsURI, qualifiedName, value: DOMString);
- procedure RemoveAttributeNS(const nsURI, aLocalName: DOMString);
- function GetAttributeNodeNS(const nsURI, aLocalName: DOMString): TDOMAttr;
- function SetAttributeNodeNS(newAttr: TDOMAttr): TDOMAttr;
- function GetElementsByTagNameNS(const nsURI, aLocalName: DOMString): TDOMNodeList;
- function hasAttribute(const name: DOMString): Boolean;
- function hasAttributeNS(const nsURI, aLocalName: DOMString): Boolean;
- function HasAttributes: Boolean; override;
- // extension
- property AttribStrings[const Name: DOMString]: DOMString
- read GetAttribute write SetAttribute; default;
- end;
- // -------------------------------------------------------
- // Text
- // -------------------------------------------------------
- TDOMText = class(TDOMCharacterData)
- protected
- function GetNodeType: Integer; override;
- function GetNodeName: DOMString; override;
- procedure SetNodeValue(const aValue: DOMString); override;
- public
- function CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode; overload; override;
- function SplitText(offset: LongWord): TDOMText;
- function IsElementContentWhitespace: Boolean;
- end;
- // -------------------------------------------------------
- // Comment
- // -------------------------------------------------------
- TDOMComment = class(TDOMCharacterData)
- protected
- function GetNodeType: Integer; override;
- function GetNodeName: DOMString; override;
- public
- function CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode; overload; override;
- end;
- // -------------------------------------------------------
- // CDATASection
- // -------------------------------------------------------
- TDOMCDATASection = class(TDOMText)
- protected
- function GetNodeType: Integer; override;
- function GetNodeName: DOMString; override;
- public
- function CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode; overload; override;
- end;
- // -------------------------------------------------------
- // DocumentType
- // -------------------------------------------------------
- TDOMDocumentType = class(TDOMNode)
- protected
- FModel: TDTDModel;
- FEntities, FNotations: TDOMNamedNodeMap;
- function GetEntities: TDOMNamedNodeMap;
- function GetNotations: TDOMNamedNodeMap;
- function GetNodeType: Integer; override;
- function GetNodeName: DOMString; override;
- function GetPublicID: DOMString;
- function GetSystemID: DOMString;
- function GetInternalSubset: DOMString;
- public
- constructor Create(aOwner: TDOMDocument; aModel: TDTDModel);
- destructor Destroy; override;
- property Name: DOMString read GetNodeName;
- property Entities: TDOMNamedNodeMap read GetEntities;
- property Notations: TDOMNamedNodeMap read GetNotations;
- // Introduced in DOM Level 2:
- property PublicID: DOMString read GetPublicID;
- property SystemID: DOMString read GetSystemID;
- property InternalSubset: DOMString read GetInternalSubset;
- end;
- // -------------------------------------------------------
- // Notation
- // -------------------------------------------------------
- TDOMNotation = class(TDOMNode)
- protected
- FDecl: TNotationDecl;
- FBaseURI: DOMString;
- function GetNodeType: Integer; override;
- function GetNodeName: DOMString; override;
- function GetPublicID: DOMString;
- function GetSystemID: DOMString;
- public
- function CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode; overload; override;
- property PublicID: DOMString read GetPublicID;
- property SystemID: DOMString read GetSystemID;
- end;
- // -------------------------------------------------------
- // Entity
- // -------------------------------------------------------
- TDOMEntity = class(TDOMNode_TopLevel)
- protected
- FDecl: TEntityDecl;
- FBaseURI: DOMString;
- function GetNodeType: Integer; override;
- function GetNodeName: DOMString; override;
- function GetPublicID: DOMString;
- function GetSystemID: DOMString;
- function GetNotationName: DOMString;
- public
- function CloneNode(deep: Boolean; aCloneOwner: TDOMDocument): TDOMNode; override;
- property PublicID: DOMString read GetPublicID;
- property SystemID: DOMString read GetSystemID;
- property NotationName: DOMString read GetNotationName;
- property XMLVersion: DOMString read GetXMLVersion;
- end;
- // -------------------------------------------------------
- // EntityReference
- // -------------------------------------------------------
- TDOMEntityReference = class(TDOMNode_WithChildren)
- protected
- FName: DOMString;
- function GetNodeType: Integer; override;
- function GetNodeName: DOMString; override;
- public
- function CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode; overload; override;
- end;
- // -------------------------------------------------------
- // ProcessingInstruction
- // -------------------------------------------------------
- TDOMProcessingInstruction = class(TDOMNode)
- private
- FTarget: DOMString;
- FNodeValue: DOMString;
- protected
- function GetNodeType: Integer; override;
- function GetNodeName: DOMString; override;
- function GetNodeValue: DOMString; override;
- procedure SetNodeValue(const AValue: DOMString); override;
- public
- function CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode; overload; override;
- property Target: DOMString read FTarget;
- property Data: DOMString read FNodeValue write SetNodeValue;
- end;
- // TNodePool - custom memory management for TDOMNode's
- // One pool manages objects of the same InstanceSize (may be of various classes)
- PExtent = ^TExtent;
- TExtent = record
- Next: PExtent;
- // following: array of TDOMNode instances
- end;
- TNodePool = class(TObject)
- private
- FCurrExtent: PExtent;
- FCurrExtentSize: Integer;
- FElementSize: Integer;
- FCurrBlock: TDOMNode;
- FFirstFree: TDOMNode;
- procedure AddExtent(AElemCount: Integer);
- public
- constructor Create(AElementSize: Integer; AElementCount: Integer = 32);
- destructor Destroy; override;
- function AllocNode(AClass: TDOMNodeClass): TDOMNode;
- procedure FreeNode(ANode: TDOMNode);
- end;
- // temporary until things are settled
- function LoadElement(doc: TDOMDocument; src: PNodeData; attrCount: Integer): TDOMElement;
- // =======================================================
- // =======================================================
- implementation
- uses
- UriParser;
- { a namespace-enabled NamedNodeMap }
- type
- TAttributeMap = class(TDOMNamedNodeMap)
- private
- function FindNS(nsIndex: Integer; const aLocalName: DOMString;
- out Index: LongWord): Boolean;
- function InternalRemoveNS(const nsURI, aLocalName: DOMString): TDOMNode;
- procedure RestoreDefault(aName: PHashItem);
- protected
- function Delete(index: LongWord): TDOMNode; override;
- function ValidateInsert(arg: TDOMNode): Integer; override;
- public
- function setNamedItem(arg: TDOMNode): TDOMNode; override;
- function getNamedItemNS(const namespaceURI, localName: DOMString): TDOMNode; override;
- function setNamedItemNS(arg: TDOMNode): TDOMNode; override;
- function removeNamedItemNS(const namespaceURI,localName: DOMString): TDOMNode; override;
- end;
- // -------------------------------------------------------
- // DOM Exception
- // -------------------------------------------------------
- constructor EDOMError.Create(ACode: Integer; const ASituation: String);
- begin
- Code := ACode;
- inherited Create(Self.ClassName + ' in ' + ASituation);
- end;
- constructor EDOMIndexSize.Create(const ASituation: String); // 1
- begin
- inherited Create(INDEX_SIZE_ERR, ASituation);
- end;
- constructor EDOMHierarchyRequest.Create(const ASituation: String); // 3
- begin
- inherited Create(HIERARCHY_REQUEST_ERR, ASituation);
- end;
- constructor EDOMWrongDocument.Create(const ASituation: String); // 4
- begin
- inherited Create(WRONG_DOCUMENT_ERR, ASituation);
- end;
- constructor EDOMNotFound.Create(const ASituation: String); // 8
- begin
- inherited Create(NOT_FOUND_ERR, ASituation);
- end;
- constructor EDOMNotSupported.Create(const ASituation: String); // 9
- begin
- inherited Create(NOT_SUPPORTED_ERR, ASituation);
- end;
- constructor EDOMInUseAttribute.Create(const ASituation: String); // 10
- begin
- inherited Create(INUSE_ATTRIBUTE_ERR, ASituation);
- end;
- constructor EDOMInvalidState.Create(const ASituation: String); // 11
- begin
- inherited Create(INVALID_STATE_ERR, ASituation);
- end;
- constructor EDOMSyntax.Create(const ASituation: String); // 12
- begin
- inherited Create(SYNTAX_ERR, ASituation);
- end;
- constructor EDOMInvalidModification.Create(const ASituation: String); // 13
- begin
- inherited Create(INVALID_MODIFICATION_ERR, ASituation);
- end;
- constructor EDOMNamespace.Create(const ASituation: String); // 14
- begin
- inherited Create(NAMESPACE_ERR, ASituation);
- end;
- constructor EDOMInvalidAccess.Create(const ASituation: String); // 15
- begin
- inherited Create(INVALID_ACCESS_ERR, ASituation);
- end;
- // -------------------------------------------------------
- // Node
- // -------------------------------------------------------
- constructor TDOMNode.Create(AOwner: TDOMDocument);
- begin
- FOwnerDocument := AOwner;
- inherited Create;
- end;
- destructor TDOMNode.Destroy;
- begin
- if Assigned(FParentNode) then
- FParentNode.DetachChild(Self);
- inherited Destroy;
- end;
- procedure TDOMNode.FreeInstance;
- begin
- if Assigned(FPool) then
- begin
- CleanupInstance;
- TNodePool(FPool).FreeNode(Self);
- end
- else
- inherited FreeInstance;
- end;
- function TDOMNode.GetNodeValue: DOMString;
- begin
- Result := '';
- end;
- function TDOMNode.GetParentNode: TDOMNode;
- begin
- Result := FParentNode;
- end;
- procedure TDOMNode.SetNodeValue(const AValue: DOMString);
- begin
- // do nothing
- end;
- function TDOMNode.GetChildNodes: TDOMNodeList;
- begin
- Result := FOwnerDocument.GetChildNodeList(Self);
- end;
- function TDOMNode.GetFirstChild: TDOMNode;
- begin
- Result := nil;
- end;
- function TDOMNode.GetLastChild: TDOMNode;
- begin
- Result := nil;
- end;
- function TDOMNode.GetPreviousSibling: TDOMNode;
- begin
- if nfFirstChild in FFlags then
- Result := nil
- else
- Result := FPreviousSibling;
- end;
- function TDOMNode.GetAttributes: TDOMNamedNodeMap;
- begin
- Result := nil;
- end;
- function TDOMNode.InsertBefore(NewChild, RefChild: TDOMNode): TDOMNode;
- begin
- Changing; // merely to comply with core3/nodeinsertbefore14
- raise EDOMHierarchyRequest.Create('Node.InsertBefore');
- Result:=nil;
- end;
- function TDOMNode.ReplaceChild(NewChild, OldChild: TDOMNode): TDOMNode;
- begin
- Changing; // merely to comply with core3/nodereplacechild21
- raise EDOMHierarchyRequest.Create('Node.ReplaceChild');
- Result:=nil;
- end;
- function TDOMNode.DetachChild(OldChild: TDOMNode): TDOMNode;
- begin
- // OldChild isn't in our child list
- raise EDOMNotFound.Create('Node.RemoveChild');
- Result:=nil;
- end;
- function TDOMNode.RemoveChild(OldChild: TDOMNode): TDOMNode;
- begin
- Result := DetachChild(OldChild);
- end;
- function TDOMNode.AppendChild(NewChild: TDOMNode): TDOMNode;
- begin
- Result := InsertBefore(NewChild, nil);
- end;
- function TDOMNode.HasChildNodes: Boolean;
- begin
- Result := False;
- end;
- function TDOMNode.CloneNode(deep: Boolean): TDOMNode;
- begin
- Result := CloneNode(deep, FOwnerDocument);
- end;
- function TDOMNode.CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode;
- begin
- // !! CreateFmt() does not set Code property !!
- raise EDOMNotSupported.Create(Format('Cloning/importing of %s is not supported', [ClassName]));
- Result:=nil;
- end;
- function TDOMNode.FindNode(const ANodeName: DOMString): TDOMNode;
- begin
- // FIX: we have no children, hence cannot find anything
- Result := nil;
- end;
- function TDOMNode.GetRevision: Integer;
- begin
- Result := FOwnerDocument.FRevision;
- end;
- function TDOMNode.IsSupported(const Feature, Version: DOMString): Boolean;
- begin
- Result := FOwnerDocument.Impl.HasFeature(Feature, Version);
- end;
- function TDOMNode.HasAttributes: Boolean;
- begin
- Result := False;
- end;
- procedure TDOMNode.Normalize;
- var
- Child, tmp: TDOMNode;
- Txt: TDOMText;
- begin
- Child := FirstChild;
- Txt := nil;
- while Assigned(Child) do
- begin
- if Child.NodeType = TEXT_NODE then
- begin
- tmp := Child.NextSibling;
- if TDOMText(Child).Data <> '' then
- begin
- if Assigned(Txt) then
- begin
- Txt.AppendData(TDOMText(Child).Data);
- // TODO: maybe should be smarter
- Exclude(Txt.FFlags, nfIgnorableWS);
- end
- else
- begin
- Txt := TDOMText(Child);
- Child := Child.NextSibling;
- Continue;
- end;
- end;
- Child.Free;
- Child := tmp;
- end
- else
- begin
- Child.Normalize; // should be recursive!
- Child := Child.NextSibling;
- Txt := nil;
- end;
- end;
- end;
- function TDOMNode.GetTextContent: DOMString;
- begin
- Result := NodeValue;
- end;
- procedure TDOMNode.SetTextContent(const AValue: DOMString);
- begin
- SetNodeValue(AValue);
- end;
- function TDOMNode.GetNamespaceURI: DOMString;
- begin
- Result := '';
- end;
- function TDOMNode.GetLocalName: DOMString;
- begin
- Result := '';
- end;
- function TDOMNode.GetPrefix: DOMString;
- begin
- Result := '';
- end;
- procedure TDOMNode.SetPrefix(const Value: DOMString);
- begin
- // do nothing, override for Elements and Attributes
- end;
- function TDOMNode.GetOwnerDocument: TDOMDocument;
- begin
- Result := FOwnerDocument;
- end;
- procedure TDOMNode.SetReadOnly(Value: Boolean);
- var
- child: TDOMNode;
- attrs: TDOMNamedNodeMap;
- I: Integer;
- begin
- if Value then
- Include(FFlags, nfReadOnly)
- else
- Exclude(FFlags, nfReadOnly);
- child := FirstChild;
- while Assigned(child) do
- begin
- child.SetReadOnly(Value);
- child := child.NextSibling;
- end;
- if HasAttributes then
- begin
- attrs := Attributes;
- for I := 0 to attrs.Length-1 do
- attrs[I].SetReadOnly(Value);
- end;
- end;
- procedure TDOMNode.Changing;
- begin
- if (nfReadOnly in FFlags) and not (nfDestroying in FOwnerDocument.FFlags) then
- raise EDOMError.Create(NO_MODIFICATION_ALLOWED_ERR, 'Node.CheckReadOnly');
- end;
- function CompareDOMStrings(const s1, s2: DOMPChar; l1, l2: integer): integer;
- var i: integer;
- begin
- Result:=l1-l2;
- i:=0;
- while (i<l1) and (Result=0) do begin
- Result:=ord(s1[i])-ord(s2[i]);
- inc(i);
- end;
- end;
- // generic version (slow)
- function TDOMNode.CompareName(const name: DOMString): Integer;
- var
- SelfName: DOMString;
- begin
- SelfName := NodeName;
- Result := CompareDOMStrings(DOMPChar(name), DOMPChar(SelfName), Length(name), Length(SelfName));
- end;
- // This will return nil for Entity, Notation, DocType and DocFragment's
- function GetAncestorElement(n: TDOMNode): TDOMElement;
- var
- parent: TDOMNode;
- begin
- case n.nodeType of
- DOCUMENT_NODE:
- result := TDOMDocument(n).documentElement;
- ATTRIBUTE_NODE:
- result := TDOMAttr(n).OwnerElement;
- else
- parent := n.ParentNode;
- while Assigned(parent) and (parent.NodeType <> ELEMENT_NODE) do
- parent := parent.ParentNode;
- Result := TDOMElement(parent);
- end;
- end;
- // TODO: specs prescribe to return default namespace if APrefix=null,
- // but we aren't able to distinguish null from an empty string.
- // This breaks level3/nodelookupnamespaceuri08 which passes an empty string.
- function TDOMNode.LookupNamespaceURI(const APrefix: DOMString): DOMString;
- var
- Attr: TDOMAttr;
- Map: TDOMNamedNodeMap;
- I: Integer;
- begin
- Result := '';
- if Self = nil then
- Exit;
- if nodeType = ELEMENT_NODE then
- begin
- if (nfLevel2 in FFlags) and (TDOMElement(Self).Prefix = APrefix) then
- begin
- result := Self.NamespaceURI;
- Exit;
- end;
- if HasAttributes then
- begin
- Map := Attributes;
- for I := 0 to Map.Length-1 do
- begin
- Attr := TDOMAttr(Map[I]);
- // should ignore level 1 atts here
- if ((Attr.Prefix = 'xmlns') and (Attr.localName = APrefix)) or
- ((Attr.localName = 'xmlns') and (APrefix = '')) then
- begin
- result := Attr.NodeValue;
- Exit;
- end;
- end
- end;
- end;
- result := GetAncestorElement(Self).LookupNamespaceURI(APrefix);
- end;
- function TDOMNode.LookupPrefix(const nsURI: DOMString): DOMString;
- begin
- Result := '';
- if (nsURI = '') or (Self = nil) then
- Exit;
- if nodeType = ELEMENT_NODE then
- result := TDOMElement(Self).InternalLookupPrefix(nsURI, TDOMElement(Self))
- else
- result := GetAncestorElement(Self).LookupPrefix(nsURI);
- end;
- function TDOMNode.IsDefaultNamespace(const nsURI: DOMString): Boolean;
- var
- Attr: TDOMAttr;
- Map: TDOMNamedNodeMap;
- I: Integer;
- begin
- Result := False;
- if Self = nil then
- Exit;
- if nodeType = ELEMENT_NODE then
- begin
- if TDOMElement(Self).FNSI.PrefixLen = 0 then
- begin
- result := (nsURI = namespaceURI);
- Exit;
- end
- else if HasAttributes then
- begin
- Map := Attributes;
- for I := 0 to Map.Length-1 do
- begin
- Attr := TDOMAttr(Map[I]);
- if Attr.LocalName = 'xmlns' then
- begin
- result := (Attr.Value = nsURI);
- Exit;
- end;
- end;
- end;
- end;
- result := GetAncestorElement(Self).IsDefaultNamespace(nsURI);
- end;
- function GetParentURI(n: TDOMNode): DOMString;
- var
- entity, parent: TDOMNode;
- begin
- parent := n.ParentNode;
- if Assigned(parent) then
- begin
- entity := nil;
- case parent.nodeType of
- ENTITY_NODE:
- entity := parent;
- ENTITY_REFERENCE_NODE:
- if Assigned(n.OwnerDocument.DocType) then
- entity := n.OwnerDocument.DocType.Entities.GetNamedItem(parent.NodeName);
- end;
- if entity = nil then
- result := parent.BaseURI
- else
- { TODO: this will need fix when resource resolving is implemented;
- it should return the URI of actually fetched entity. }
- ResolveRelativeURI(TDOMEntity(entity).FDecl.FURI, TDOMEntity(entity).SystemID, result);
- end
- else
- result := n.OwnerDocument.DocumentURI;
- end;
- function TDOMNode.GetBaseURI: DOMString;
- var
- base: DOMString;
- dtype: TDOMDocumentType;
- ent: TDOMEntity;
- begin
- case NodeType of
- ELEMENT_NODE:
- begin
- result := GetParentURI(Self);
- { 'xml' prefix is restricted to xml namespace, so this will work
- regardless of namespace processing enabled }
- base := TDOMElement(Self).GetAttribute('xml:base');
- if base <> '' then
- begin
- ResolveRelativeUri(result, base, result);
- end;
- end;
- DOCUMENT_NODE:
- result := TDOMDocument(Self).FURI;
- PROCESSING_INSTRUCTION_NODE:
- result := GetParentURI(Self);
- { BaseUri of entities and notations is the URI where they're defined;
- cloning should cause this property to get lost. }
- ENTITY_NODE:
- result := TDOMEntity(Self).FBaseURI;
- NOTATION_NODE:
- result := TDOMNotation(Self).FBaseURI;
- ENTITY_REFERENCE_NODE:
- begin
- result := '';
- dtype := OwnerDocument.DocType;
- if Assigned(dtype) then
- begin
- ent := TDOMEntity(dtype.Entities.GetNamedItem(NodeName));
- if Assigned(ent) then
- result := ent.FDecl.FURI;
- end;
- end
- else
- result := '';
- end;
- end;
- //------------------------------------------------------------------------------
- type
- TNodeTypeEnum = ELEMENT_NODE..NOTATION_NODE;
- TNodeTypeSet = set of TNodeTypeEnum;
- const
- stdChildren = [TEXT_NODE, ENTITY_REFERENCE_NODE, PROCESSING_INSTRUCTION_NODE,
- COMMENT_NODE, CDATA_SECTION_NODE, ELEMENT_NODE];
- ValidChildren: array [TNodeTypeEnum] of TNodeTypeSet = (
- stdChildren, { element }
- [TEXT_NODE, ENTITY_REFERENCE_NODE], { attribute }
- [], { text }
- [], { cdata }
- stdChildren, { ent ref }
- stdChildren, { entity }
- [], { pi }
- [], { comment }
- [ELEMENT_NODE, DOCUMENT_TYPE_NODE, PROCESSING_INSTRUCTION_NODE, COMMENT_NODE], { document }
- [], { doctype }
- stdChildren, { fragment }
- [] { notation }
- );
- function TDOMNode_WithChildren.GetFirstChild: TDOMNode;
- begin
- Result := FFirstChild;
- end;
- function TDOMNode_WithChildren.GetLastChild: TDOMNode;
- begin
- if FFirstChild = nil then
- Result := nil
- else
- Result := FFirstChild.FPreviousSibling;
- end;
- destructor TDOMNode_WithChildren.Destroy;
- begin
- FreeChildren;
- FChildNodes.Free; // its destructor will zero the field
- inherited Destroy;
- end;
- function TDOMNode_WithChildren.InsertBefore(NewChild, RefChild: TDOMNode):
- TDOMNode;
- var
- Tmp: TDOMNode;
- NewChildType: Integer;
- begin
- Result := NewChild;
- NewChildType := NewChild.NodeType;
- Changing;
- if NewChild.FOwnerDocument <> FOwnerDocument then
- begin
- if (NewChildType <> DOCUMENT_TYPE_NODE) or
- (NewChild.FOwnerDocument <> nil) then
- raise EDOMWrongDocument.Create('NodeWC.InsertBefore');
- end;
- if Assigned(RefChild) and (RefChild.ParentNode <> Self) then
- raise EDOMNotFound.Create('NodeWC.InsertBefore');
- // TODO: skip checking Fragments as well? (Fragment itself cannot be in the tree)
- if not (NewChildType in [TEXT_NODE, CDATA_SECTION_NODE, COMMENT_NODE, PROCESSING_INSTRUCTION_NODE]) and (NewChild.FirstChild <> nil) then
- begin
- Tmp := Self;
- while Assigned(Tmp) do
- begin
- if Tmp = NewChild then
- raise EDOMHierarchyRequest.Create('NodeWC.InsertBefore (cycle in tree)');
- Tmp := Tmp.ParentNode;
- end;
- end;
- if NewChild = RefChild then // inserting node before itself is a no-op
- Exit;
- Inc(FOwnerDocument.FRevision); // invalidate nodelists
- if NewChildType = DOCUMENT_FRAGMENT_NODE then
- begin
- Tmp := NewChild.FirstChild;
- if Assigned(Tmp) then
- begin
- while Assigned(Tmp) do
- begin
- if not (Tmp.NodeType in ValidChildren[NodeType]) then
- raise EDOMHierarchyRequest.Create('NodeWC.InsertBefore');
- Tmp := Tmp.NextSibling;
- end;
- while Assigned(TDOMDocumentFragment(NewChild).FFirstChild) do
- InsertBefore(TDOMDocumentFragment(NewChild).FFirstChild, RefChild);
- end;
- Exit;
- end;
- if not (NewChildType in ValidChildren[NodeType]) then
- raise EDOMHierarchyRequest.Create('NodeWC.InsertBefore');
- if Assigned(NewChild.FParentNode) then
- NewChild.FParentNode.DetachChild(NewChild);
- NewChild.FNextSibling := RefChild;
- if RefChild = nil then // append to the end
- begin
- if Assigned(FFirstChild) then
- begin
- Tmp := FFirstChild.FPreviousSibling; { our last child }
- Tmp.FNextSibling := NewChild;
- NewChild.FPreviousSibling := Tmp;
- end
- else
- begin
- FFirstChild := NewChild;
- Include(NewChild.FFlags, nfFirstChild);
- end;
- FFirstChild.FPreviousSibling := NewChild; { becomes our last child }
- end
- else // insert before RefChild
- begin
- NewChild.FPreviousSibling := RefChild.FPreviousSibling;
- if RefChild = FFirstChild then
- begin
- Exclude(RefChild.FFlags, nfFirstChild);
- FFirstChild := NewChild;
- Include(NewChild.FFlags, nfFirstChild);
- end
- else
- RefChild.FPreviousSibling.FNextSibling := NewChild;
- RefChild.FPreviousSibling := NewChild;
- end;
- NewChild.FParentNode := Self;
- end;
- function TDOMNode_WithChildren.ReplaceChild(NewChild, OldChild: TDOMNode):
- TDOMNode;
- begin
- InsertBefore(NewChild, OldChild);
- if Assigned(OldChild) and (OldChild <> NewChild) then
- RemoveChild(OldChild);
- Result := OldChild;
- end;
- function TDOMNode_WithChildren.DetachChild(OldChild: TDOMNode): TDOMNode;
- var
- prev, next: TDOMNode;
- begin
- Changing;
- if OldChild.ParentNode <> Self then
- raise EDOMNotFound.Create('NodeWC.RemoveChild');
- Inc(FOwnerDocument.FRevision); // invalidate nodelists
- if OldChild = FFirstChild then
- begin
- Exclude(OldChild.FFlags, nfFirstChild);
- FFirstChild := FFirstChild.FNextSibling;
- if Assigned(FFirstChild) then
- begin
- { maintain lastChild }
- Include(FFirstChild.FFlags, nfFirstChild);
- FFirstChild.FPreviousSibling := OldChild.FPreviousSibling;
- end;
- end
- else
- begin
- prev := OldChild.FPreviousSibling;
- next := OldChild.FNextSibling;
- prev.FNextSibling := next;
- if Assigned(next) then { removing node in the middle }
- next.FPreviousSibling := prev
- else { removing the last child }
- FFirstChild.FPreviousSibling := prev;
- end;
- // Make sure removed child does not contain references to nowhere
- OldChild.FPreviousSibling := nil;
- OldChild.FNextSibling := nil;
- OldChild.FParentNode := nil;
- Result := OldChild;
- end;
- procedure TDOMNode_WithChildren.InternalAppend(NewChild: TDOMNode);
- var
- Tmp: TDOMNode;
- begin
- if Assigned(FFirstChild) then
- begin
- Tmp := FFirstChild.FPreviousSibling; { our last child }
- Tmp.FNextSibling := NewChild;
- NewChild.FPreviousSibling := Tmp;
- end
- else
- begin
- FFirstChild := NewChild;
- Include(NewChild.FFlags, nfFirstChild);
- end;
- FFirstChild.FPreviousSibling := NewChild; { becomes our last child }
- NewChild.FParentNode := Self;
- end;
- function TDOMNode_WithChildren.HasChildNodes: Boolean;
- begin
- Result := Assigned(FFirstChild);
- end;
- function TDOMNode_WithChildren.FindNode(const ANodeName: DOMString): TDOMNode;
- begin
- Result := FFirstChild;
- while Assigned(Result) do
- begin
- if Result.CompareName(ANodeName)=0 then
- Exit;
- Result := Result.NextSibling;
- end;
- end;
- procedure TDOMNode_WithChildren.CloneChildren(ACopy: TDOMNode;
- ACloneOwner: TDOMDocument);
- var
- node: TDOMNode;
- begin
- node := FirstChild;
- while Assigned(node) do
- begin
- TDOMNode_WithChildren(ACopy).InternalAppend(node.CloneNode(True, ACloneOwner));
- node := node.NextSibling;
- end;
- end;
- procedure TDOMNode_WithChildren.FreeChildren;
- var
- child, next: TDOMNode;
- begin
- child := FFirstChild;
- while Assigned(child) do
- begin
- next := child.NextSibling;
- child.FParentNode := nil;
- child.Destroy; // we know it's not nil, so save a call
- child := next;
- end;
- FFirstChild := nil;
- end;
- function TDOMNode_WithChildren.GetTextContent: DOMString;
- var
- child: TDOMNode;
- begin
- Result := '';
- child := FFirstChild;
- // TODO: probably very slow, optimization needed
- while Assigned(child) do
- begin
- case child.NodeType of
- TEXT_NODE: if not (nfIgnorableWS in child.FFlags) then
- Result := Result + TDOMText(child).Data;
- COMMENT_NODE, PROCESSING_INSTRUCTION_NODE: ; // ignored
- else
- Result := Result + child.TextContent;
- end;
- child := child.NextSibling;
- end;
- end;
- procedure TDOMNode_WithChildren.SetTextContent(const AValue: DOMString);
- begin
- Changing;
- while Assigned(FFirstChild) do
- DetachChild(FFirstChild);
- if AValue <> '' then
- AppendChild(FOwnerDocument.CreateTextNode(AValue));
- end;
- // -------------------------------------------------------
- // NodeList
- // -------------------------------------------------------
- constructor TDOMNodeList.Create(ANode: TDOMNode);
- begin
- inherited Create;
- FNode := ANode;
- FRevision := ANode.GetRevision-1; // force BuildList at first access
- FList := TFPList.Create;
- end;
- destructor TDOMNodeList.Destroy;
- begin
- if (FNode is TDOMNode_WithChildren) and
- (TDOMNode_WithChildren(FNode).FChildNodes = Self) then
- TDOMNode_WithChildren(FNode).FChildNodes := nil
- else
- FNode.FOwnerDocument.NodeListDestroyed(Self);
- FList.Free;
- inherited Destroy;
- end;
- function TDOMNodeList.NodeFilter(aNode: TDOMNode): TFilterResult;
- begin
- // accept all nodes but don't allow recursion
- Result := frNorecurseTrue;
- end;
- procedure TDOMNodeList.BuildList;
- var
- current, next: TDOMNode;
- res: TFilterResult;
- begin
- FList.Clear;
- FRevision := FNode.GetRevision; // refresh
- current := FNode.FirstChild;
- while Assigned(current) do
- begin
- res := NodeFilter(current);
- if res in [frTrue, frNorecurseTrue] then
- FList.Add(current);
- next := nil;
- if res in [frTrue, frFalse] then
- next := current.FirstChild;
- if next = nil then
- begin
- while current <> FNode do
- begin
- next := current.NextSibling;
- if Assigned(next) then
- Break;
- current := current.ParentNode;
- end;
- end;
- current := next;
- end;
- end;
- function TDOMNodeList.GetCount: LongWord;
- begin
- if FRevision <> FNode.GetRevision then
- BuildList;
- Result := FList.Count;
- end;
- function TDOMNodeList.GetItem(index: LongWord): TDOMNode;
- begin
- if FRevision <> FNode.GetRevision then
- BuildList;
- if index < LongWord(FList.Count) then
- Result := TDOMNode(FList.List^[index])
- else
- Result := nil;
- end;
- { TDOMElementList }
- constructor TDOMElementList.Create(ANode: TDOMNode; const AFilter: DOMString);
- begin
- inherited Create(ANode);
- filter := AFilter;
- UseFilter := filter <> '*';
- end;
- constructor TDOMElementList.Create(ANode: TDOMNode; const nsURI, localName: DOMString);
- begin
- inherited Create(ANode);
- localNameFilter := localName;
- FMatchNS := True;
- FMatchAnyNS := (nsURI = '*');
- if not FMatchAnyNS then
- FNSIndexFilter := ANode.FOwnerDocument.IndexOfNS(nsURI);
- UseFilter := (localName <> '*');
- end;
- function TDOMElementList.NodeFilter(aNode: TDOMNode): TFilterResult;
- var
- I, L: Integer;
- begin
- Result := frFalse;
- if aNode.NodeType = ELEMENT_NODE then with TDOMElement(aNode) do
- begin
- if FMatchNS then
- begin
- if (FMatchAnyNS or (FNSI.NSIndex = Word(FNSIndexFilter))) then
- begin
- I := FNSI.PrefixLen;
- L := system.Length(FNSI.QName^.Key);
- if (not UseFilter or ((L-I = system.Length(localNameFilter)) and
- CompareMem(@FNSI.QName^.Key[I+1], DOMPChar(localNameFilter), system.Length(localNameFilter)*sizeof(WideChar)))) then
- Result := frTrue;
- end;
- end
- else if (not UseFilter or (TagName = Filter)) then
- Result := frTrue;
- end;
- end;
- // -------------------------------------------------------
- // NamedNodeMap
- // -------------------------------------------------------
- constructor TDOMNamedNodeMap.Create(AOwner: TDOMNode);
- begin
- inherited Create;
- FOwner := AOwner;
- FList := TFPList.Create;
- end;
- destructor TDOMNamedNodeMap.Destroy;
- var
- I: Integer;
- begin
- for I := FList.Count-1 downto 0 do
- TDOMNode(FList.List^[I]).Free;
- FList.Free;
- inherited Destroy;
- end;
- function TDOMNamedNodeMap.GetItem(index: LongWord): TDOMNode;
- begin
- if index < LongWord(FList.Count) then
- Result := TDOMNode(FList.List^[index])
- else
- Result := nil;
- end;
- function TDOMNamedNodeMap.GetLength: LongWord;
- begin
- Result := FList.Count;
- end;
- function TDOMNamedNodeMap.Find(const name: DOMString; out Index: LongWord): Boolean;
- var
- L, H, I, C: Integer;
- begin
- Result := False;
- L := 0;
- H := FList.Count - 1;
- while L <= H do
- begin
- I := (L + H) shr 1;
- C := TDOMNode(FList.List^[I]).CompareName(name);
- if C > 0 then L := I + 1 else
- begin
- H := I - 1;
- if C = 0 then
- begin
- Result := True;
- L := I;
- end;
- end;
- end;
- Index := L;
- end;
- function TDOMNamedNodeMap.GetNamedItem(const name: DOMString): TDOMNode;
- var
- i: Cardinal;
- begin
- if Find(name, i) then
- Result := TDOMNode(FList.List^[i])
- else
- Result := nil;
- end;
- // Note: this *may* raise NOT_SUPPORTED_ERR if the document is e.g. HTML.
- // This isn't checked now.
- function TDOMNamedNodeMap.GetNamedItemNS(const namespaceURI, localName: DOMString): TDOMNode;
- begin
- Result := nil;
- end;
- function TDOMNamedNodeMap.ValidateInsert(arg: TDOMNode): Integer;
- begin
- Result := 0;
- if nfReadOnly in FOwner.FFlags then
- Result := NO_MODIFICATION_ALLOWED_ERR
- else if arg.FOwnerDocument <> FOwner.FOwnerDocument then
- Result := WRONG_DOCUMENT_ERR;
- { Note: Since Entity and Notation maps are always read-only, and the AttributeMap
- overrides this method and does its own check for correct arg.NodeType, there's
- no point in checking NodeType here. }
- end;
- function TDOMNamedNodeMap.SetNamedItem(arg: TDOMNode): TDOMNode;
- var
- i: Cardinal;
- Exists: Boolean;
- res: Integer;
- begin
- res := ValidateInsert(arg);
- if res <> 0 then
- raise EDOMError.Create(res, 'NamedNodeMap.SetNamedItem');
- Exists := Find(arg.NodeName, i);
- if Exists then
- begin
- Result := TDOMNode(FList.List^[i]);
- FList.List^[i] := arg;
- exit;
- end;
- FList.Insert(i, arg);
- Result := nil;
- end;
- function TDOMNamedNodeMap.SetNamedItemNS(arg: TDOMNode): TDOMNode;
- begin
- { Since the map contains only namespaceless nodes (all having empty
- localName and namespaceURI properties), a namespaced arg won't match
- any of them. Therefore, add it using nodeName as key.
- Note: a namespaceless arg is another story, as it will match *any* node
- in the map. This can be considered as a flaw in specs. }
- Result := SetNamedItem(arg);
- end;
- function TDOMNamedNodeMap.Delete(index: LongWord): TDOMNode;
- begin
- Result := TDOMNode(FList.List^[index]);
- FList.Delete(index);
- end;
- function TDOMNamedNodeMap.InternalRemove(const name: DOMString): TDOMNode;
- var
- i: Cardinal;
- begin
- if Find(name, i) then
- Result := Delete(I)
- else
- Result := nil;
- end;
- function TDOMNamedNodeMap.RemoveNamedItem(const name: DOMString): TDOMNode;
- begin
- if nfReadOnly in FOwner.FFlags then
- raise EDOMError.Create(NO_MODIFICATION_ALLOWED_ERR, 'NamedNodeMap.RemoveNamedItem');
- Result := InternalRemove(name);
- if Result = nil then
- raise EDOMNotFound.Create('NamedNodeMap.RemoveNamedItem');
- end;
- function TDOMNamedNodeMap.RemoveNamedItemNS(const namespaceURI, localName: DOMString): TDOMNode;
- begin
- // see comments to SetNamedItemNS. Related tests are written clever enough
- // in the sense they don't expect NO_MODIFICATION_ERR in first place.
- raise EDOMNotFound.Create('NamedNodeMap.RemoveNamedItemNS');
- Result := nil;
- end;
- { TAttributeMap }
- function TAttributeMap.Delete(index: LongWord): TDOMNode;
- begin
- Result := inherited Delete(index);
- if Assigned(Result) then
- begin
- Result.FParentNode := nil;
- if Assigned(TDOMAttr(Result).FNSI.QName) then
- RestoreDefault(TDOMAttr(Result).FNSI.QName);
- end;
- end;
- function TAttributeMap.ValidateInsert(arg: TDOMNode): Integer;
- begin
- Result := inherited ValidateInsert(arg);
- if Result = 0 then
- begin
- if arg.NodeType <> ATTRIBUTE_NODE then
- Result := HIERARCHY_REQUEST_ERR
- else if Assigned(arg.FParentNode) and (arg.FParentNode <> FOwner) then
- Result := INUSE_ATTRIBUTE_ERR;
- end;
- end;
- procedure TAttributeMap.RestoreDefault(aName: PHashItem);
- var
- eldef: TElementDecl;
- attrdef: TAttributeDef;
- begin
- if not Assigned(TDOMElement(FOwner).FNSI.QName) then // safeguard
- Exit;
- eldef := TElementDecl(TDOMElement(FOwner).FNSI.QName^.Data);
- if Assigned(eldef) then
- begin
- // TODO: can be avoided by linking attributes directly to their defs
- attrdef := eldef.GetAttrDef(aName);
- if Assigned(attrdef) and (attrdef.Default in [adDefault, adFixed]) then
- TDOMElement(FOwner).RestoreDefaultAttr(attrdef);
- end;
- end;
- // Since list is kept sorted by nodeName, we must use linear search here.
- // This routine is not called while parsing, so parsing speed is not lowered.
- function TAttributeMap.FindNS(nsIndex: Integer; const aLocalName: DOMString;
- out Index: LongWord): Boolean;
- var
- I: Integer;
- P: DOMPChar;
- begin
- for I := 0 to FList.Count-1 do
- begin
- with TDOMAttr(FList.List^[I]) do
- begin
- if nsIndex = FNSI.NSIndex then
- begin
- P := DOMPChar(FNSI.QName^.Key);
- if FNSI.PrefixLen > 1 then
- Inc(P, FNSI.PrefixLen);
- if CompareDOMStrings(DOMPChar(aLocalName), P, System.Length(aLocalName), System.Length(FNSI.QName^.Key) - FNSI.PrefixLen) = 0 then
- begin
- Index := I;
- Result := True;
- Exit;
- end;
- end;
- end;
- end;
- Result := False;
- end;
- function TAttributeMap.InternalRemoveNS(const nsURI, aLocalName: DOMString): TDOMNode;
- var
- i: Cardinal;
- nsIndex: Integer;
- begin
- Result := nil;
- nsIndex := FOwner.FOwnerDocument.IndexOfNS(nsURI);
- if (nsIndex >= 0) and FindNS(nsIndex, aLocalName, i) then
- Result := Delete(I);
- end;
- function TAttributeMap.getNamedItemNS(const namespaceURI, localName: DOMString): TDOMNode;
- var
- nsIndex: Integer;
- i: LongWord;
- begin
- nsIndex := FOwner.FOwnerDocument.IndexOfNS(namespaceURI);
- if (nsIndex >= 0) and FindNS(nsIndex, localName, i) then
- Result := TDOMNode(FList.List^[i])
- else
- Result := nil;
- end;
- function TAttributeMap.setNamedItem(arg: TDOMNode): TDOMNode;
- begin
- Result := inherited setNamedItem(arg);
- if Assigned(Result) then
- Result.FParentNode := nil;
- arg.FParentNode := FOwner;
- end;
- function TAttributeMap.setNamedItemNS(arg: TDOMNode): TDOMNode;
- var
- i: LongWord;
- res: Integer;
- Exists: Boolean;
- begin
- res := ValidateInsert(arg);
- if res <> 0 then
- raise EDOMError.Create(res, 'NamedNodeMap.SetNamedItemNS');
- Result := nil;
- with TDOMAttr(arg) do
- begin
- // calling LocalName is no good... but it is done once
- if FindNS(FNSI.NSIndex, localName, i) then
- begin
- Result := TDOMNode(FList.List^[i]);
- FList.Delete(i);
- end;
- // Do a non-namespace search in order to keep the list sorted on nodeName
- Exists := Find(FNSI.QName^.Key, i);
- if Exists and (Result = nil) then // case when arg has no namespace
- begin
- Result := TDOMNode(FList.List^[i]);
- FList.List^[i] := arg;
- end
- else
- FList.Insert(i, arg);
- end;
- if Assigned(Result) then
- Result.FParentNode := nil;
- arg.FParentNode := FOwner;
- end;
- function TAttributeMap.removeNamedItemNS(const namespaceURI,
- localName: DOMString): TDOMNode;
- begin
- if nfReadOnly in FOwner.FFlags then
- raise EDOMError.Create(NO_MODIFICATION_ALLOWED_ERR, 'NamedNodeMap.RemoveNamedItemNS');
- Result := InternalRemoveNS(namespaceURI, localName);
- if Result = nil then
- raise EDOMNotFound.Create('NamedNodeMap.RemoveNamedItemNS');
- end;
- // -------------------------------------------------------
- // CharacterData
- // -------------------------------------------------------
- function TDOMCharacterData.GetLength: LongWord;
- begin
- Result := system.Length(FNodeValue);
- end;
- function TDOMCharacterData.GetNodeValue: DOMString;
- begin
- Result := FNodeValue;
- end;
- procedure TDOMCharacterData.SetNodeValue(const AValue: DOMString);
- begin
- Changing;
- FNodeValue := AValue;
- end;
- function TDOMCharacterData.SubstringData(offset, count: LongWord): DOMString;
- begin
- if offset > Length then
- raise EDOMIndexSize.Create('CharacterData.SubstringData');
- Result := Copy(FNodeValue, offset + 1, count);
- end;
- procedure TDOMCharacterData.AppendData(const arg: DOMString);
- begin
- Changing;
- FNodeValue := FNodeValue + arg;
- end;
- procedure TDOMCharacterData.InsertData(offset: LongWord; const arg: DOMString);
- begin
- Changing;
- if offset > Length then
- raise EDOMIndexSize.Create('CharacterData.InsertData');
- Insert(arg, FNodeValue, offset+1);
- end;
- procedure TDOMCharacterData.DeleteData(offset, count: LongWord);
- begin
- Changing;
- if offset > Length then
- raise EDOMIndexSize.Create('CharacterData.DeleteData');
- Delete(FNodeValue, offset+1, count);
- end;
- procedure TDOMCharacterData.ReplaceData(offset, count: LongWord; const arg: DOMString);
- begin
- DeleteData(offset, count);
- InsertData(offset, arg);
- end;
- // -------------------------------------------------------
- // DocumentFragmet
- // -------------------------------------------------------
- function TDOMDocumentFragment.GetNodeType: Integer;
- begin
- Result := DOCUMENT_FRAGMENT_NODE;
- end;
- function TDOMDocumentFragment.GetNodeName: DOMString;
- begin
- Result := '#document-fragment';
- end;
- function TDOMDocumentFragment.CloneNode(deep: Boolean; aCloneOwner: TDOMDocument): TDOMNode;
- begin
- Result := aCloneOwner.CreateDocumentFragment;
- if deep then
- CloneChildren(Result, aCloneOwner);
- end;
- // -------------------------------------------------------
- // Top-level node
- // -------------------------------------------------------
- function TDOMNode_TopLevel.GetXMLVersion: DOMString;
- begin
- Result := xmlVersionStr[FXMLVersion];
- end;
- // -------------------------------------------------------
- // DOMImplementation
- // -------------------------------------------------------
- { Non-negative return value is Pos(':', QName), negative is DOM error code. }
- function CheckQName(const QName: DOMString): Integer;
- var
- I, L: Integer;
- begin
- if not IsXmlName(QName) then
- begin
- Result := -INVALID_CHARACTER_ERR;
- Exit;
- end;
- L := Length(QName);
- Result := Pos(WideChar(':'), QName);
- if Result > 0 then
- begin
- for I := Result+1 to L-1 do // check for second colon (Use IndexWord?)
- if QName[I] = ':' then
- begin
- Result := -NAMESPACE_ERR;
- Exit;
- end;
- // Name validity has already been checked by IsXmlName() call above.
- // So just check that colon isn't first or last char, and that it is follwed by NameStartChar.
- if ((Result = 1) or (Result = L) or not IsXmlName(@QName[Result+1], 1)) then
- begin
- Result := -NAMESPACE_ERR;
- Exit;
- end;
- end;
- end;
- function TDOMImplementation.HasFeature(const feature, version: DOMString):
- Boolean;
- var
- s: string;
- begin
- s := feature; // force Ansi, features do not contain non-ASCII chars
- Result := (SameText(s, 'XML') and ((version = '') or (version = '1.0') or (version = '2.0'))) or
- (SameText(s, 'Core') and ((version = '') or (version = '2.0')));
- end;
- function TDOMImplementation.CreateDocumentType(const QualifiedName, PublicID,
- SystemID: DOMString): TDOMDocumentType;
- var
- res: Integer;
- model: TDTDModel;
- begin
- res := CheckQName(QualifiedName);
- if res < 0 then
- raise EDOMError.Create(-res, 'Implementation.CreateDocumentType');
- model := TDTDModel.Create(nil); // !!nowhere to get nametable from at this time
- model.FName := QualifiedName;
- // DOM does not restrict PublicID without SystemID (unlike XML spec)
- model.FPublicID := PublicID;
- model.FSystemID := SystemID;
- Result := TDOMDocumentType.Create(nil, model);
- model.Release; // now Result remains a sole owner of model
- end;
- function TDOMImplementation.CreateDocument(const NamespaceURI,
- QualifiedName: DOMString; doctype: TDOMDocumentType): TDOMDocument;
- var
- Root: TDOMNode;
- begin
- if Assigned(doctype) and Assigned(doctype.OwnerDocument) then
- raise EDOMWrongDocument.Create('Implementation.CreateDocument');
- Result := TXMLDocument.Create;
- Result.FImplementation := Self;
- try
- if Assigned(doctype) then
- begin
- Doctype.FOwnerDocument := Result;
- Result.AppendChild(doctype);
- end;
- Root := Result.CreateElementNS(NamespaceURI, QualifiedName);
- Result.AppendChild(Root);
- except
- Result.Free;
- raise;
- end;
- end;
- // -------------------------------------------------------
- // Document
- // -------------------------------------------------------
- constructor TDOMDocument.Create;
- begin
- inherited Create(nil);
- FOwnerDocument := Self;
- FMaxPoolSize := (TDOMEntity.InstanceSize + sizeof(Pointer)-1) and not (sizeof(Pointer)-1) + sizeof(Pointer);
- FPools := AllocMem(FMaxPoolSize);
- FNames := THashTable.Create(256, True);
- SetLength(FNamespaces, 3);
- // Namespace #0 should always be an empty string
- FNamespaces[1] := stduri_xml;
- FNamespaces[2] := stduri_xmlns;
- FEmptyNode := TDOMElement.Create(Self);
- FStdUri_xml := FNames.FindOrAdd(stduri_xml);
- FStdUri_xmlns := FNames.FindOrAdd(stduri_xmlns);
- end;
- destructor TDOMDocument.Destroy;
- var
- i: Integer;
- begin
- Include(FFlags, nfDestroying);
- FreeAndNil(FIDList); // set to nil before starting destroying children
- FNodeLists.Free;
- FEmptyNode.Free;
- inherited Destroy;
- for i := 0 to (FMaxPoolSize div sizeof(TNodePool))-1 do
- FPools^[i].Free;
- FreeMem(FPools);
- FNames.Free; // free the nametable after inherited has destroyed the children
- // (because children reference the nametable)
- end;
- function TDOMDocument.CloneNode(deep: Boolean): TDOMNode;
- type
- TDOMDocumentClass = class of TDOMDocument;
- var
- Clone: TDOMDocument;
- node, doctypenode: TDOMNode;
- begin
- Clone := TDOMDocumentClass(ClassType).Create;
- Clone.FInputEncoding := FInputEncoding;
- Clone.FXMLEncoding := FXMLEncoding;
- Clone.FXMLVersion := FXMLVersion;
- Clone.FXMLStandalone := FXMLStandalone;
- Clone.FURI := FURI;
- if deep then
- begin
- node := FirstChild;
- doctypenode := DocType;
- while Assigned(node) do
- begin
- {TODO: now just skip doctype, a better solution is to be found.}
- if node <> doctypenode then
- Clone.InternalAppend(node.CloneNode(True, Clone));
- node := node.NextSibling;
- end;
- end;
- Result := Clone;
- end;
- function TDOMDocument.Alloc(AClass: TDOMNodeClass): TDOMNode;
- var
- pp: TNodePool;
- size: Integer;
- begin
- if nfDestroying in FFlags then
- raise EDOMError.Create(INVALID_ACCESS_ERR, 'Attempt to allocate node memory while destroying');
- size := (AClass.InstanceSize + sizeof(Pointer)-1) and not (sizeof(Pointer)-1);
- if size > FMaxPoolSize then
- begin
- Result := TDOMNode(AClass.NewInstance);
- Exit;
- end;
- pp := FPools^[size div sizeof(TNodePool)];
- if pp = nil then
- begin
- pp := TNodePool.Create(size);
- FPools^[size div sizeof(TNodePool)] := pp;
- end;
- Result := pp.AllocNode(AClass);
- end;
- // This shouldn't be called if document has no IDs,
- // or when it is being destroyed
- // TODO: This could be much faster if removing ID happens
- // upon modification of corresponding attribute value.
- procedure TDOMDocument.RemoveID(Elem: TDOMElement);
- begin
- FIDList.RemoveData(Elem);
- end;
- function TDOMDocument.GetNodeType: Integer;
- begin
- Result := DOCUMENT_NODE;
- end;
- function TDOMDocument.GetNodeName: DOMString;
- begin
- Result := '#document';
- end;
- function TDOMDocument.GetTextContent: DOMString;
- begin
- Result := '';
- end;
- procedure TDOMDocument.SetTextContent(const value: DOMString);
- begin
- // Document ignores setting TextContent
- end;
- function TDOMDocument.GetOwnerDocument: TDOMDocument;
- begin
- Result := nil;
- end;
- function TDOMDocument.InsertBefore(NewChild, RefChild: TDOMNode): TDOMNode;
- var
- nType: Integer;
- begin
- nType := NewChild.NodeType;
- if ((nType = ELEMENT_NODE) and Assigned(DocumentElement)) or
- ((nType = DOCUMENT_TYPE_NODE) and Assigned(DocType)) then
- raise EDOMHierarchyRequest.Create('Document.InsertBefore');
- Result := inherited InsertBefore(NewChild, RefChild);
- end;
- function TDOMDocument.ReplaceChild(NewChild, OldChild: TDOMNode): TDOMNode;
- var
- nType: Integer;
- begin
- nType := NewChild.NodeType;
- if ((nType = ELEMENT_NODE) and (OldChild = DocumentElement)) or // root can be replaced by another element
- ((nType = DOCUMENT_TYPE_NODE) and (OldChild = DocType)) then // and so can be DTD
- begin
- inherited InsertBefore(NewChild, OldChild);
- Result := OldChild;
- if OldChild <> NewChild then
- RemoveChild(OldChild);
- end
- else
- Result := inherited ReplaceChild(NewChild, OldChild);
- end;
- function TDOMDocument.GetDocumentElement: TDOMElement;
- var
- node: TDOMNode;
- begin
- node := FFirstChild;
- while Assigned(node) and (node.NodeType <> ELEMENT_NODE) do
- node := node.NextSibling;
- Result := TDOMElement(node);
- end;
- function TDOMDocument.GetDocType: TDOMDocumentType;
- var
- node: TDOMNode;
- begin
- node := FFirstChild;
- while Assigned(node) and (node.NodeType <> DOCUMENT_TYPE_NODE) do
- node := node.NextSibling;
- Result := TDOMDocumentType(node);
- end;
- function TDOMDocument.CreateElement(const tagName: DOMString): TDOMElement;
- begin
- if not IsXmlName(tagName) then
- raise EDOMError.Create(INVALID_CHARACTER_ERR, 'DOMDocument.CreateElement');
- TDOMNode(Result) := Alloc(TDOMElement);
- Result.Create(Self);
- Result.FNSI.QName := FNames.FindOrAdd(DOMPChar(tagName), Length(tagName));
- Result.AttachDefaultAttrs;
- end;
- function TDOMDocument.CreateElementBuf(Buf: DOMPChar; Length: Integer): TDOMElement;
- begin
- TDOMNode(Result) := Alloc(TDOMElement);
- Result.Create(Self);
- Result.FNSI.QName := FNames.FindOrAdd(Buf, Length);
- end;
- function TDOMDocument.CreateDocumentFragment: TDOMDocumentFragment;
- begin
- TDOMNode(Result) := Alloc(TDOMDocumentFragment);
- Result.Create(Self);
- end;
- function TDOMDocument.CreateTextNode(const data: DOMString): TDOMText;
- begin
- TDOMNode(Result) := Alloc(TDOMText);
- Result.Create(Self);
- Result.FNodeValue := data;
- end;
- function TDOMDocument.CreateTextNodeBuf(Buf: DOMPChar; Length: Integer; IgnWS: Boolean): TDOMText;
- begin
- TDOMNode(Result) := Alloc(TDOMText);
- Result.Create(Self);
- SetString(Result.FNodeValue, Buf, Length);
- if IgnWS then
- Include(Result.FFlags, nfIgnorableWS);
- end;
- function TDOMDocument.CreateComment(const data: DOMString): TDOMComment;
- begin
- TDOMNode(Result) := Alloc(TDOMComment);
- Result.Create(Self);
- Result.FNodeValue := data;
- end;
- function TDOMDocument.CreateCommentBuf(Buf: DOMPChar; Length: Integer): TDOMComment;
- begin
- TDOMNode(Result) := Alloc(TDOMComment);
- Result.Create(Self);
- SetString(Result.FNodeValue, Buf, Length);
- end;
- function TDOMDocument.CreateCDATASection(const data: DOMString):
- TDOMCDATASection;
- begin
- raise EDOMNotSupported.Create('DOMDocument.CreateCDATASection');
- Result:=nil;
- end;
- function TDOMDocument.CreateProcessingInstruction(const target,
- data: DOMString): TDOMProcessingInstruction;
- begin
- raise EDOMNotSupported.Create('DOMDocument.CreateProcessingInstruction');
- Result:=nil;
- end;
- function TDOMDocument.CreateAttribute(const name: DOMString): TDOMAttr;
- begin
- if not IsXmlName(name) then
- raise EDOMError.Create(INVALID_CHARACTER_ERR, 'DOMDocument.CreateAttribute');
- TDOMNode(Result) := Alloc(TDOMAttr);
- Result.Create(Self);
- Result.FNSI.QName := FNames.FindOrAdd(DOMPChar(name), Length(name));
- Include(Result.FFlags, nfSpecified);
- end;
- function TDOMDocument.CreateAttributeBuf(Buf: DOMPChar; Length: Integer): TDOMAttr;
- begin
- TDOMNode(Result) := Alloc(TDOMAttr);
- Result.Create(Self);
- Result.FNSI.QName := FNames.FindOrAdd(buf, Length);
- Include(Result.FFlags, nfSpecified);
- end;
- function TDOMDocument.CreateEntityReference(const name: DOMString):
- TDOMEntityReference;
- begin
- raise EDOMNotSupported.Create('DOMDocument.CreateEntityReference');
- Result:=nil;
- end;
- function TDOMDocument.GetChildNodeList(aNode: TDOMNode): TDOMNodeList;
- begin
- if not (aNode is TDOMNode_WithChildren) then
- aNode := FEmptyNode;
- Result := TDOMNode_WithChildren(aNode).FChildNodes;
- if Result = nil then
- begin
- Result := TDOMNodeList.Create(aNode);
- TDOMNode_WithChildren(aNode).FChildNodes := Result;
- end;
- end;
- function TDOMDocument.GetElementList(aNode: TDOMNode; const nsURI, aLocalName: DOMString;
- UseNS: Boolean): TDOMNodeList;
- var
- L: Integer;
- Key, P: DOMPChar;
- Item: PHashItem;
- begin
- if FNodeLists = nil then
- FNodeLists := THashTable.Create(32, True);
- L := (sizeof(Pointer) div sizeof(WideChar)) + Length(aLocalName);
- if UseNS then
- Inc(L, Length(nsURI)+1);
- GetMem(Key, L*sizeof(WideChar));
- try
- // compose the key for hashing
- P := Key;
- PPointer(P)^ := aNode;
- Inc(PPointer(P));
- Move(DOMPChar(aLocalName)^, P^, Length(aLocalName)*sizeof(WideChar));
- if UseNS then
- begin
- Inc(P, Length(aLocalName));
- P^ := #12; Inc(P); // separator -- diff ('foo','bar') from 'foobar'
- Move(DOMPChar(nsURI)^, P^, Length(nsURI)*sizeof(WideChar));
- end;
- // try finding in the hashtable
- Item := FNodeLists.FindOrAdd(Key, L);
- Result := TDOMNodeList(Item^.Data);
- if Result = nil then
- begin
- if UseNS then
- Result := TDOMElementList.Create(aNode, nsURI, aLocalName)
- else
- Result := TDOMElementList.Create(aNode, aLocalName);
- Item^.Data := Result;
- end;
- finally
- FreeMem(Key);
- end;
- end;
- function TDOMDocument.GetElementsByTagName(const tagname: DOMString): TDOMNodeList;
- begin
- Result := GetElementList(Self, '', tagname, False);
- end;
- function TDOMDocument.GetElementsByTagNameNS(const nsURI, aLocalName: DOMString): TDOMNodeList;
- begin
- Result := GetElementList(Self, nsURI, aLocalName, True);
- end;
- { This is linear hence slow. However:
- - if user code frees each nodelist ASAP, there are only few items in the hashtable
- - if user code does not free nodelists, this is not called at all.
- }
- procedure TDOMDocument.NodeListDestroyed(aList: TDOMNodeList);
- begin
- if (not (nfDestroying in FFlags)) and (FNodeLists <> nil) then
- FNodeLists.RemoveData(aList);
- end;
- function TDOMDocument.ValidateQName(const nsUri, qName: DOMString;
- out nsidx: PHashItem): Integer;
- begin
- nsidx := FNames.FindOrAdd(DOMPChar(nsUri), Length(nsUri));
- Result := CheckQName(qName);
- if Result >= 0 then
- begin
- // QName contains prefix, but no namespace
- if ((nsUri = '') and (Result > 0)) or
- // Bad usage of 'http://www.w3.org/2000/xmlns/'
- ((((Length(QName) = 5) or (Result = 6)) and (Pos(DOMString('xmlns'), QName) = 1)) <> (nsIdx = FStdUri_xmlns)) or
- // Bad usage of 'http://www.w3.org/XML/1998/namespace'
- ((Result = 4) and (Pos(DOMString('xml'), QName) = 1) and (nsIdx <> FStdUri_xml)) then
- Result := -NAMESPACE_ERR;
- end;
- end;
- function TDOMDocument.CreateAttributeNS(const nsURI,
- QualifiedName: DOMString): TDOMAttr;
- var
- PrefIdx: Integer;
- nsidx: PHashItem;
- begin
- PrefIdx := ValidateQName(nsURI, QualifiedName, nsidx);
- if PrefIdx < 0 then
- raise EDOMError.Create(-PrefIdx, 'Document.CreateAttributeNS');
- TDOMNode(Result) := Alloc(TDOMAttr);
- Result.Create(Self);
- Result.FNSI.QName := FNames.FindOrAdd(DOMPChar(QualifiedName), Length(QualifiedName));
- Result.FNSI.NSIndex := Word(IndexOfNS(nsURI, True));
- Result.FNSI.PrefixLen := Word(PrefIdx);
- Include(Result.FFlags, nfLevel2);
- Include(Result.FFlags, nfSpecified);
- end;
- function TDOMDocument.CreateElementNS(const nsURI,
- QualifiedName: DOMString): TDOMElement;
- begin
- result:=CreateElementNS(nsURI, QualifiedName, TDOMElement);
- end;
- function TDOMDocument.CreateElementNS(const nsURI, QualifiedName: DOMString;
- AClass: TDOMElementClass): TDOMElement; overload;
- var
- PrefIdx: Integer;
- nsidx: PHashItem;
- begin
- PrefIdx := ValidateQName(nsURI, QualifiedName, nsidx);
- if PrefIdx < 0 then
- raise EDOMError.Create(-PrefIdx, 'Document.CreateElementNS');
- TDOMNode(Result) := Alloc(AClass);
- Result.Create(Self);
- Result.FNSI.QName := FNames.FindOrAdd(DOMPChar(QualifiedName), Length(QualifiedName));
- Result.FNSI.NSIndex := Word(IndexOfNS(nsURI, True));
- Result.FNSI.PrefixLen := Word(PrefIdx);
- Include(Result.FFlags, nfLevel2);
- Result.AttachDefaultAttrs;
- end;
- function TDOMDocument.GetElementById(const ElementID: DOMString): TDOMElement;
- begin
- Result := nil;
- if Assigned(FIDList) then
- Result := TDOMElement(FIDList.Get(DOMPChar(ElementID), Length(ElementID)));
- end;
- function TDOMDocument.ImportNode(ImportedNode: TDOMNode;
- Deep: Boolean): TDOMNode;
- begin
- Result := ImportedNode.CloneNode(Deep, Self);
- end;
- function TDOMDocument.IndexOfNS(const nsURI: DOMString; AddIfAbsent: Boolean): Integer;
- var
- I: Integer;
- begin
- // TODO: elaborate implementation
- for I := 0 to Length(FNamespaces)-1 do
- if FNamespaces[I] = nsURI then
- begin
- Result := I;
- Exit;
- end;
- if AddIfAbsent then
- begin
- Result := Length(FNamespaces);
- SetLength(FNamespaces, Result+1);
- FNamespaces[Result] := nsURI;
- end
- else
- Result := -1;
- end;
- procedure TDOMDocument.SetXMLVersion(const aValue: DOMString);
- begin
- raise EDOMNotSupported.Create('DOMDocument.SetXMLVersion');
- end;
- procedure TDOMDocument.SetXMLStandalone(aValue: Boolean);
- begin
- raise EDOMNotSupported.Create('DOMDocument.SetXMLStandalone');
- end;
- constructor TXMLDocument.Create;
- begin
- inherited Create;
- FXMLVersion := xmlVersion10;
- end;
- function TXMLDocument.CreateCDATASection(const data: DOMString):
- TDOMCDATASection;
- begin
- TDOMNode(Result) := Alloc(TDOMCDATASection);
- Result.Create(Self);
- Result.FNodeValue := data;
- end;
- function TXMLDocument.CreateProcessingInstruction(const target,
- data: DOMString): TDOMProcessingInstruction;
- begin
- if not IsXmlName(target) then
- raise EDOMError.Create(INVALID_CHARACTER_ERR, 'XMLDocument.CreateProcessingInstruction');
- TDOMNode(Result) := Alloc(TDOMProcessingInstruction);
- Result.Create(Self);
- Result.FTarget := target;
- Result.FNodeValue := data;
- end;
- function TXMLDocument.CreateEntityReference(const name: DOMString):
- TDOMEntityReference;
- var
- dType: TDOMDocumentType;
- ent: TDOMEntity;
- begin
- if not IsXmlName(name) then
- raise EDOMError.Create(INVALID_CHARACTER_ERR, 'XMLDocument.CreateEntityReference');
- TDOMNode(Result) := Alloc(TDOMEntityReference);
- Result.Create(Self);
- Result.FName := name;
- dType := DocType;
- if Assigned(dType) then
- begin
- TDOMNode(ent) := dType.Entities.GetNamedItem(name);
- if Assigned(ent) then
- ent.CloneChildren(Result, Self);
- end;
- Result.SetReadOnly(True);
- end;
- procedure TXMLDocument.SetXMLVersion(const aValue: DOMString);
- begin
- if aValue = '1.0' then
- FXMLVersion := xmlVersion10
- else if aValue = '1.1' then
- FXMLVersion := xmlVersion11
- else
- raise EDOMNotSupported.Create('XMLDocument.SetXMLVersion');
- end;
- procedure TXMLDocument.SetXMLStandalone(aValue: Boolean);
- begin
- FXmlStandalone := aValue;
- end;
- { TDOMNode_NS }
- function TDOMNode_NS.GetNodeName: DOMString;
- begin
- // Because FNSI.QName is not set by the TDOMNode itself, but is set by
- // other classes/functions, it is necessary to check if FNSQ.QName is
- // assigned.
- if assigned(FNSI.QName) then
- Result := FNSI.QName^.Key
- else
- Result := '';
- end;
- function TDOMNode_NS.GetLocalName: DOMString;
- begin
- if nfLevel2 in FFlags then
- Result := Copy(FNSI.QName^.Key, FNSI.PrefixLen+1, MaxInt)
- else
- Result := '';
- end;
- function TDOMNode_NS.GetNamespaceURI: DOMString;
- begin
- Result := FOwnerDocument.FNamespaces[FNSI.NSIndex];
- end;
- function TDOMNode_NS.GetPrefix: DOMString;
- begin
- if FNSI.PrefixLen < 2 then
- Result := ''
- else
- Result := Copy(FNSI.QName^.Key, 1, FNSI.PrefixLen-1);
- end;
- procedure TDOMNode_NS.SetPrefix(const Value: DOMString);
- var
- NewName: DOMString;
- begin
- Changing;
- if not IsXmlName(Value) then
- raise EDOMError.Create(INVALID_CHARACTER_ERR, 'Node.SetPrefix');
- if (Pos(WideChar(':'), Value) > 0) or ((FNSI.NSIndex = 0) and (Value <> '')) or
- ((Value = 'xml') and (FNSI.NSIndex <> 1)) or
- ((ClassType = TDOMAttr) and // BAD!
- ((Value = 'xmlns') and (FNSI.NSIndex <> 2)) or (FNSI.QName^.Key = 'xmlns')) then
- raise EDOMNamespace.Create('Node.SetPrefix');
- // TODO: rehash properly
- NewName := Value + ':' + Copy(FNSI.QName^.Key, FNSI.PrefixLen+1, MaxInt);
- FNSI.QName := FOwnerDocument.FNames.FindOrAdd(DOMPChar(NewName), Length(NewName));
- FNSI.PrefixLen := Length(Value)+1;
- end;
- function TDOMNode_NS.CompareName(const AName: DOMString): Integer;
- begin
- Result := CompareDOMStrings(DOMPChar(AName), DOMPChar(NodeName), Length(AName), Length(NodeName));
- end;
- procedure TDOMNode_NS.SetNSI(const nsUri: DOMString; ColonPos: Integer);
- begin
- FNSI.NSIndex := FOwnerDocument.IndexOfNS(nsURI, True);
- FNSI.PrefixLen := ColonPos;
- Include(FFlags, nfLevel2);
- end;
- // -------------------------------------------------------
- // Attr
- // -------------------------------------------------------
- function TDOMAttr.GetNodeType: Integer;
- begin
- Result := ATTRIBUTE_NODE;
- end;
- function TDOMAttr.GetParentNode: TDOMNode;
- begin
- Result := nil;
- end;
- destructor TDOMAttr.Destroy;
- begin
- if Assigned(FParentNode) and not (nfDestroying in FParentNode.FFlags) then
- // TODO: This may raise NOT_FOUND_ERR in case something's really wrong
- TDOMElement(FParentNode).RemoveAttributeNode(Self);
- FParentNode := nil;
- inherited Destroy;
- end;
- function TDOMAttr.CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode;
- begin
- // Cloned attribute is always specified and carries its children
- if nfLevel2 in FFlags then
- Result := ACloneOwner.CreateAttributeNS(namespaceURI, NodeName)
- else
- Result := ACloneOwner.CreateAttribute(NodeName);
- TDOMAttr(Result).FDataType := FDataType;
- CloneChildren(Result, ACloneOwner);
- end;
- function TDOMAttr.GetNodeValue: DOMString;
- begin
- Result := GetTextContent;
- if FDataType <> dtCdata then
- NormalizeSpaces(Result);
- end;
- procedure TDOMAttr.SetNodeValue(const AValue: DOMString);
- begin
- SetTextContent(AValue);
- Include(FFlags, nfSpecified);
- end;
- function TDOMAttr.GetSpecified: Boolean;
- begin
- Result := nfSpecified in FFlags;
- end;
- function TDOMAttr.GetIsID: Boolean;
- begin
- Result := FDataType = dtID;
- end;
- function TDOMAttr.GetOwnerElement: TDOMElement;
- begin
- Result := TDOMElement(FParentNode);
- end;
- // -------------------------------------------------------
- // Element
- // -------------------------------------------------------
- function TDOMElement.GetNodeType: Integer;
- begin
- Result := ELEMENT_NODE;
- end;
- destructor TDOMElement.Destroy;
- begin
- Include(FFlags, nfDestroying);
- if Assigned(FOwnerDocument.FIDList) then
- FOwnerDocument.RemoveID(Self);
- FAttributes.Free;
- FAttributes := nil;
- inherited Destroy;
- end;
- function TDOMElement.CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode;
- var
- i: Integer;
- Attr, AttrClone: TDOMAttr;
- begin
- if ACloneOwner <> FOwnerDocument then
- begin
- // Importing has to go the hard way...
- if nfLevel2 in FFlags then
- Result := ACloneOwner.CreateElementNS(NamespaceURI, NodeName)
- else
- Result := ACloneOwner.CreateElement(NodeName);
- if Assigned(FAttributes) then
- begin
- for i := 0 to FAttributes.Length - 1 do
- begin
- Attr := TDOMAttr(FAttributes[i]);
- // destroy defaulted attributes (if any), it is safe because caller had not seen them yet
- if Attr.Specified then
- TDOMElement(Result).SetAttributeNode(TDOMAttr(Attr.CloneNode(True, ACloneOwner))).Free;
- end;
- end;
- end
- else // Cloning may cheat a little bit.
- begin
- Result := FOwnerDocument.Alloc(TDOMElement);
- TDOMElement(Result).Create(FOwnerDocument);
- TDOMElement(Result).FNSI := FNSI;
- if nfLevel2 in FFlags then
- Include(Result.FFlags, nfLevel2);
- if Assigned(FAttributes) then
- begin
- // clone all attributes, but preserve nfSpecified flag
- for i := 0 to FAttributes.Length - 1 do
- begin
- Attr := TDOMAttr(FAttributes[i]);
- AttrClone := TDOMAttr(Attr.CloneNode(True, ACloneOwner));
- if not Attr.Specified then
- Exclude(AttrClone.FFlags, nfSpecified);
- TDOMElement(Result).SetAttributeNode(AttrClone);
- end;
- end;
- end;
- if deep then
- CloneChildren(Result, ACloneOwner);
- end;
- procedure TDOMElement.AttachDefaultAttrs;
- var
- eldef: TElementDecl;
- attrdef: TAttributeDef;
- I: Integer;
- begin
- if not Assigned(FNSI.QName) then // safeguard
- Exit;
- eldef := TElementDecl(FNSI.QName^.Data);
- if Assigned(eldef) and eldef.NeedsDefaultPass then
- begin
- for I := 0 to eldef.AttrDefCount-1 do
- begin
- attrdef := eldef.AttrDefs[I];
- if attrdef.Default in [adDefault, adFixed] then
- RestoreDefaultAttr(attrdef);
- end;
- end;
- end;
- function TDOMElement.InternalLookupPrefix(const nsURI: DOMString; Original: TDOMElement): DOMString;
- var
- I: Integer;
- Attr: TDOMAttr;
- begin
- result := '';
- if Self = nil then
- Exit;
- if (nfLevel2 in FFlags) and (namespaceURI = nsURI) and (FNSI.PrefixLen > 0) then
- begin
- Result := Prefix;
- if Original.LookupNamespaceURI(result) = nsURI then
- Exit;
- end;
- if Assigned(FAttributes) then
- begin
- for I := 0 to FAttributes.Length-1 do
- begin
- Attr := TDOMAttr(FAttributes[I]);
- if (Attr.Prefix = 'xmlns') and (Attr.Value = nsURI) then
- begin
- result := Attr.LocalName;
- if Original.LookupNamespaceURI(result) = nsURI then
- Exit;
- end;
- end;
- end;
- result := GetAncestorElement(Self).InternalLookupPrefix(nsURI, Original);
- end;
- function LoadAttribute(doc: TDOMDocument; src: PNodeData): TDOMAttr;
- var
- curr: PNodeData;
- begin
- TDOMNode(result) := doc.Alloc(TDOMAttr);
- result.Create(doc);
- result.FNSI.QName := src^.FQName;
- if not src^.FIsDefault then
- Include(result.FFlags, nfSpecified);
- if Assigned(src^.FTypeInfo) then
- result.FDataType := TAttributeDef(src^.FTypeInfo).DataType;
- if Assigned(src^.FNsUri) then
- result.SetNSI(src^.FNsUri^.Key, src^.FColonPos+1);
- if Assigned(src^.FNext) then
- begin
- curr := src^.FNext;
- while Assigned(curr) do
- begin
- case curr^.FNodeType of
- ntText: result.InternalAppend(doc.CreateTextNode(curr^.FValueStr));
- ntEntityReference: result.InternalAppend(doc.CreateEntityReference(curr^.FQName^.Key));
- end;
- curr := curr^.FNext;
- end;
- end
- else if src^.FValueStr <> '' then
- result.InternalAppend(doc.CreateTextNode(src^.FValueStr));
- end;
- function LoadElement(doc: TDOMDocument; src: PNodeData; attrCount: Integer): TDOMElement;
- var
- i: Integer;
- begin
- TDOMNode(result) := doc.Alloc(TDOMElement);
- result.Create(doc);
- result.FNSI.QName := src^.FQName;
- if Assigned(src^.FNsUri) then
- result.SetNSI(src^.FNsUri^.Key, src^.FColonPos+1);
- for i := 0 to attrCount-1 do
- begin
- Inc(src);
- result.SetAttributeNode(LoadAttribute(doc, src));
- // Attach element to ID map entry if necessary
- if Assigned(src^.FIDEntry) then
- src^.FIDEntry^.Data := Result;
- end;
- end;
- procedure TDOMElement.RestoreDefaultAttr(AttrDef: TAttributeDef);
- var
- Attr: TDOMAttr;
- AttrData: TNodeData;
- nsuri: DOMString;
- begin
- if nfDestroying in FOwnerDocument.FFlags then
- Exit;
- { Copy data and maybe fixup namespace fields }
- AttrData := AttrDef.Data^;
- if AttrDef.IsNamespaceDecl then
- AttrData.FNsUri := FOwnerDocument.FStdUri_xmlns
- else if AttrData.FColonPos > 0 then
- begin
- if (AttrData.FColonPos = 3) and (Pos(DOMString('xml'), AttrData.FQName^.Key) = 1) then
- AttrData.FNsUri := FOwnerDocument.FStdUri_xml
- else
- begin
- nsuri := LookupNamespaceURI(Copy(AttrData.FQName^.Key, 1, AttrData.FColonPos));
- // TODO: what if prefix isn't defined?
- AttrData.FNsUri := FOwnerDocument.FNames.FindOrAdd(nsuri);
- end;
- end;
- Attr := LoadAttribute(FOwnerDocument, @AttrData);
- // TODO: this is cheat, should look at config['namespaces'] instead.
- // revisit when it is implemented.
- if nfLevel2 in FFlags then
- Include(Attr.FFlags, nfLevel2);
- // There should be no matching attribute at this point, so non-namespace method is ok
- SetAttributeNode(Attr);
- end;
- procedure TDOMElement.Normalize;
- var
- I: Integer;
- begin
- if Assigned(FAttributes) then
- for I := 0 to FAttributes.Length - 1 do
- FAttributes[I].Normalize;
- inherited Normalize;
- end;
- function TDOMElement.GetAttributes: TDOMNamedNodeMap;
- begin
- if FAttributes=nil then
- FAttributes := TAttributeMap.Create(Self);
- Result := FAttributes;
- end;
- function TDOMElement.GetAttribute(const name: DOMString): DOMString;
- var
- Attr: TDOMNode;
- begin
- SetLength(Result, 0);
- if Assigned(FAttributes) then
- begin
- Attr := FAttributes.GetNamedItem(name);
- if Assigned(Attr) then
- Result := Attr.NodeValue;
- end;
- end;
- function TDOMElement.GetAttributeNS(const nsURI, aLocalName: DOMString): DOMString;
- var
- Attr: TDOMNode;
- begin
- SetLength(Result, 0);
- if Assigned(FAttributes) then
- begin
- Attr := FAttributes.GetNamedItemNS(nsURI, aLocalName);
- if Assigned(Attr) then
- Result := Attr.NodeValue;
- end;
- end;
- procedure TDOMElement.SetAttribute(const name, value: DOMString);
- var
- I: Cardinal;
- attr: TDOMAttr;
- begin
- Changing;
- if Attributes.Find(name, I) then
- Attr := FAttributes[I] as TDOMAttr
- else
- begin
- Attr := FOwnerDocument.CreateAttribute(name);
- Attr.FParentNode := Self;
- FAttributes.FList.Insert(I, Attr);
- end;
- attr.NodeValue := value;
- end;
- procedure TDOMElement.RemoveAttribute(const name: DOMString);
- begin
- Changing;
- // (note) NamedNodeMap.RemoveNamedItem can raise NOT_FOUND_ERR and we should not.
- if Assigned(FAttributes) then
- FAttributes.InternalRemove(name).Free;
- end;
- procedure TDOMElement.RemoveAttributeNS(const nsURI,
- aLocalName: DOMString);
- begin
- Changing;
- if Assigned(FAttributes) then
- TAttributeMap(FAttributes).InternalRemoveNS(nsURI, aLocalName).Free;
- end;
- procedure TDOMElement.SetAttributeNS(const nsURI, qualifiedName,
- value: DOMString);
- var
- I: Cardinal;
- Attr: TDOMAttr;
- idx, prefIdx: Integer;
- nsidx: PHashItem;
- begin
- Changing;
- idx := FOwnerDocument.IndexOfNS(nsURI, True);
- prefIdx := FOwnerDocument.ValidateQName(nsURI, qualifiedName, nsidx);
- if prefIdx < 0 then
- raise EDOMError.Create(-prefIdx, 'Element.SetAttributeNS');
- if TAttributeMap(Attributes).FindNS(idx, Copy(qualifiedName, prefIdx+1, MaxInt), I) then
- begin
- Attr := TDOMAttr(FAttributes[I]);
- // need to reinsert because the nodeName may change
- FAttributes.FList.Delete(I);
- end
- else
- begin
- TDOMNode(Attr) := FOwnerDocument.Alloc(TDOMAttr);
- Attr.Create(FOwnerDocument);
- Attr.FParentNode := Self;
- Attr.FNSI.NSIndex := Word(idx);
- Include(Attr.FFlags, nfLevel2);
- end;
- // keep list sorted by DOM Level 1 name
- FAttributes.Find(qualifiedName, I);
- FAttributes.FList.Insert(I, Attr);
- // TODO: rehash properly, same issue as with Node.SetPrefix()
- Attr.FNSI.QName := FOwnerDocument.FNames.FindOrAdd(DOMPChar(qualifiedName), Length(qualifiedName));
- Attr.FNSI.PrefixLen := Word(prefIdx);
- attr.NodeValue := value;
- end;
- function TDOMElement.GetAttributeNode(const name: DOMString): TDOMAttr;
- begin
- if Assigned(FAttributes) then
- Result := FAttributes.GetNamedItem(name) as TDOMAttr
- else
- Result := nil;
- end;
- function TDOMElement.GetAttributeNodeNS(const nsURI, aLocalName: DOMString): TDOMAttr;
- begin
- if Assigned(FAttributes) then
- Result := FAttributes.GetNamedItemNS(nsURI, aLocalName) as TDOMAttr
- else
- Result := nil;
- end;
- function TDOMElement.SetAttributeNode(NewAttr: TDOMAttr): TDOMAttr;
- begin
- Result := Attributes.SetNamedItem(NewAttr) as TDOMAttr;
- end;
- function TDOMElement.SetAttributeNodeNS(NewAttr: TDOMAttr): TDOMAttr;
- begin
- Result := Attributes.SetNamedItemNS(NewAttr) as TDOMAttr;
- end;
- function TDOMElement.RemoveAttributeNode(OldAttr: TDOMAttr): TDOMAttr;
- var
- Index: Integer;
- begin
- Changing;
- Result := OldAttr;
- if Assigned(FAttributes) then
- begin
- Index := FAttributes.FList.IndexOf(OldAttr);
- if Index > -1 then
- begin
- FAttributes.Delete(Index);
- Exit;
- end;
- end;
- raise EDOMNotFound.Create('Element.RemoveAttributeNode');
- end;
- function TDOMElement.GetElementsByTagName(const name: DOMString): TDOMNodeList;
- begin
- Result := FOwnerDocument.GetElementList(Self, '', name, False);
- end;
- function TDOMElement.GetElementsByTagNameNS(const nsURI, aLocalName: DOMString): TDOMNodeList;
- begin
- Result := FOwnerDocument.GetElementList(Self, nsURI, aLocalName, True);
- end;
- function TDOMElement.hasAttribute(const name: DOMString): Boolean;
- begin
- Result := Assigned(FAttributes) and
- Assigned(FAttributes.GetNamedItem(name));
- end;
- function TDOMElement.hasAttributeNS(const nsURI, aLocalName: DOMString): Boolean;
- begin
- Result := Assigned(FAttributes) and
- Assigned(FAttributes.getNamedItemNS(nsURI, aLocalName));
- end;
- function TDOMElement.HasAttributes: Boolean;
- begin
- Result := Assigned(FAttributes) and (FAttributes.Length > 0);
- end;
- // -------------------------------------------------------
- // Text
- // -------------------------------------------------------
- function TDOMText.GetNodeType: Integer;
- begin
- Result := TEXT_NODE;
- end;
- function TDOMText.GetNodeName: DOMString;
- begin
- Result := '#text';
- end;
- procedure TDOMText.SetNodeValue(const aValue: DOMString);
- begin
- inherited SetNodeValue(aValue);
- // TODO: may analyze aValue, but this will slow things down...
- Exclude(FFlags, nfIgnorableWS);
- end;
- function TDOMText.CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode;
- begin
- Result := ACloneOwner.CreateTextNode(FNodeValue);
- end;
- function TDOMText.SplitText(offset: LongWord): TDOMText;
- var
- L: LongWord;
- begin
- Changing;
- L := Length;
- if offset > L then
- raise EDOMIndexSize.Create('Text.SplitText');
- Result := FOwnerDocument.CreateTextNodeBuf(@FNodeValue[offset+1], L-offset, False);
- Result.FFlags := FFlags * [nfIgnorableWS];
- FNodeValue := Copy(FNodeValue, 1, offset);
- if Assigned(FParentNode) then
- FParentNode.InsertBefore(Result, FNextSibling);
- end;
- function TDOMText.IsElementContentWhitespace: Boolean;
- begin
- Result := nfIgnorableWS in FFlags;
- end;
- // -------------------------------------------------------
- // Comment
- // -------------------------------------------------------
- function TDOMComment.GetNodeType: Integer;
- begin
- Result := COMMENT_NODE;
- end;
- function TDOMComment.GetNodeName: DOMString;
- begin
- Result := '#comment';
- end;
- function TDOMComment.CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode;
- begin
- Result := ACloneOwner.CreateComment(FNodeValue);
- end;
- // -------------------------------------------------------
- // CDATASection
- // -------------------------------------------------------
- function TDOMCDATASection.GetNodeType: Integer;
- begin
- Result := CDATA_SECTION_NODE;
- end;
- function TDOMCDATASection.GetNodeName: DOMString;
- begin
- Result := '#cdata-section';
- end;
- function TDOMCDATASection.CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode;
- begin
- Result := ACloneOwner.CreateCDATASection(FNodeValue);
- end;
- // -------------------------------------------------------
- // DocumentType
- // -------------------------------------------------------
- function TDOMDocumentType.GetNodeType: Integer;
- begin
- Result := DOCUMENT_TYPE_NODE;
- end;
- function TDOMDocumentType.GetNodeName: DOMString;
- begin
- Result := FModel.FName;
- end;
- function TDOMDocumentType.GetPublicID: DOMString;
- begin
- Result := FModel.FPublicID;
- end;
- function TDOMDocumentType.GetSystemID: DOMString;
- begin
- Result := FModel.FSystemID;
- end;
- function TDOMDocumentType.GetInternalSubset: DOMString;
- begin
- Result := FModel.FInternalSubset;
- end;
- function ConvertEntity(Entry: PHashItem; arg: Pointer): Boolean;
- var
- this: TDOMDocumentType absolute arg;
- node: TDOMEntity;
- begin
- node := TDOMEntity.Create(this.ownerDocument);
- node.FDecl := TEntityDecl(Entry^.Data);
- node.FBaseURI := node.FDecl.FURI;
- node.SetReadOnly(True);
- this.Entities.SetNamedItem(node);
- Result := True;
- end;
- function ConvertNotation(Entry: PHashItem; arg: Pointer): Boolean;
- var
- this: TDOMDocumentType absolute arg;
- node: TDOMNotation;
- begin
- node := TDOMNotation.Create(this.ownerDocument);
- node.FDecl := TNotationDecl(Entry^.Data);
- node.FBaseURI := node.FDecl.FURI;
- node.SetReadOnly(True);
- this.Notations.SetNamedItem(node);
- Result := True;
- end;
- constructor TDOMDocumentType.Create(aOwner: TDOMDocument; aModel: TDTDModel);
- begin
- inherited Create(aOwner);
- FModel := aModel.Reference;
- FModel.Entities.ForEach(@ConvertEntity, Self);
- FModel.Notations.ForEach(@ConvertNotation, Self);
- SetReadOnly(True);
- end;
- destructor TDOMDocumentType.Destroy;
- begin
- FModel.Release;
- FEntities.Free;
- FNotations.Free;
- inherited Destroy;
- end;
- function TDOMDocumentType.GetEntities: TDOMNamedNodeMap;
- begin
- if FEntities = nil then
- FEntities := TDOMNamedNodeMap.Create(Self);
- Result := FEntities;
- end;
- function TDOMDocumentType.GetNotations: TDOMNamedNodeMap;
- begin
- if FNotations = nil then
- FNotations := TDOMNamedNodeMap.Create(Self);
- Result := FNotations;
- end;
- // -------------------------------------------------------
- // Notation
- // -------------------------------------------------------
- function TDOMNotation.GetNodeType: Integer;
- begin
- Result := NOTATION_NODE;
- end;
- function TDOMNotation.GetNodeName: DOMString;
- begin
- Result := FDecl.FName;
- end;
- function TDOMNotation.GetPublicID: DOMString;
- begin
- Result := FDecl.FPublicID;
- end;
- function TDOMNotation.GetSystemID: DOMString;
- begin
- Result := FDecl.FSystemID;
- end;
- function TDOMNotation.CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode;
- begin
- Result := ACloneOwner.Alloc(TDOMNotation);
- TDOMNotation(Result).Create(ACloneOwner);
- TDOMNotation(Result).FDecl := FDecl;
- // notation cannot have children, ignore Deep
- end;
- // -------------------------------------------------------
- // Entity
- // -------------------------------------------------------
- function TDOMEntity.GetNodeType: Integer;
- begin
- Result := ENTITY_NODE;
- end;
- function TDOMEntity.GetNodeName: DOMString;
- begin
- Result := FDecl.FName;
- end;
- function TDOMEntity.GetPublicID: DOMString;
- begin
- Result := FDecl.FPublicID;
- end;
- function TDOMEntity.GetSystemID: DOMString;
- begin
- Result := FDecl.FSystemID;
- end;
- function TDOMEntity.GetNotationName: DOMString;
- begin
- Result := FDecl.FNotationName;
- end;
- function TDOMEntity.CloneNode(deep: Boolean; aCloneOwner: TDOMDocument): TDOMNode;
- begin
- Result := aCloneOwner.Alloc(TDOMEntity);
- TDOMEntity(Result).Create(aCloneOwner);
- TDOMEntity(Result).FDecl := FDecl;
- if deep then
- CloneChildren(Result, aCloneOwner);
- Result.SetReadOnly(True);
- end;
- // -------------------------------------------------------
- // EntityReference
- // -------------------------------------------------------
- function TDOMEntityReference.GetNodeType: Integer;
- begin
- Result := ENTITY_REFERENCE_NODE;
- end;
- function TDOMEntityReference.GetNodeName: DOMString;
- begin
- Result := FName;
- end;
- function TDOMEntityReference.CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode;
- begin
- Result := ACloneOwner.CreateEntityReference(FName);
- end;
- // -------------------------------------------------------
- // ProcessingInstruction
- // -------------------------------------------------------
- function TDOMProcessingInstruction.CloneNode(deep: Boolean;
- ACloneOwner: TDOMDocument): TDOMNode;
- begin
- Result := ACloneOwner.CreateProcessingInstruction(Target, Data);
- end;
- function TDOMProcessingInstruction.GetNodeType: Integer;
- begin
- Result := PROCESSING_INSTRUCTION_NODE;
- end;
- function TDOMProcessingInstruction.GetNodeName: DOMString;
- begin
- Result := FTarget;
- end;
- function TDOMProcessingInstruction.GetNodeValue: DOMString;
- begin
- Result := FNodeValue;
- end;
- procedure TDOMProcessingInstruction.SetNodeValue(const AValue: DOMString);
- begin
- Changing;
- FNodeValue := AValue;
- end;
- { TNodePool }
- constructor TNodePool.Create(AElementSize: Integer; AElementCount: Integer);
- begin
- FElementSize := AElementSize;
- AddExtent(AElementCount);
- end;
- destructor TNodePool.Destroy;
- var
- ext, next: PExtent;
- ptr, ptr_end: PAnsiChar;
- sz: Integer;
- begin
- ext := FCurrExtent;
- ptr := PAnsiChar(FCurrBlock) + FElementSize;
- sz := FCurrExtentSize;
- while Assigned(ext) do
- begin
- // call destructors for everyone still there
- ptr_end := PAnsiChar(ext) + sizeof(TExtent) + (sz - 1) * FElementSize;
- while ptr <= ptr_end do
- begin
- if TDOMNode(ptr).FPool = Self then
- TObject(ptr).Destroy;
- Inc(ptr, FElementSize);
- end;
- // dispose the extent and pass to the next one
- next := ext^.Next;
- FreeMem(ext);
- ext := next;
- sz := sz div 2;
- ptr := PAnsiChar(ext) + sizeof(TExtent);
- end;
- inherited Destroy;
- end;
- procedure TNodePool.AddExtent(AElemCount: Integer);
- var
- ext: PExtent;
- begin
- Assert((FCurrExtent = nil) or
- (PAnsiChar(FCurrBlock) < PAnsiChar(FCurrExtent) + sizeof(TExtent)));
- Assert(AElemCount > 0);
- GetMem(ext, sizeof(TExtent) + AElemCount * FElementSize);
- ext^.Next := FCurrExtent;
- // point to the beginning of the last block of extent
- FCurrBlock := TDOMNode(PAnsiChar(ext) + sizeof(TExtent) + (AElemCount - 1) * FElementSize);
- FCurrExtent := ext;
- FCurrExtentSize := AElemCount;
- end;
- function TNodePool.AllocNode(AClass: TDOMNodeClass): TDOMNode;
- begin
- if Assigned(FFirstFree) then
- begin
- Result := FFirstFree; // remove from free list
- FFirstFree := TDOMNode(Result.FPool);
- end
- else
- begin
- if PAnsiChar(FCurrBlock) < PAnsiChar(FCurrExtent) + sizeof(TExtent) then
- AddExtent(FCurrExtentSize * 2);
- Result := FCurrBlock;
- Dec(PAnsiChar(FCurrBlock), FElementSize);
- end;
- AClass.InitInstance(Result);
- Result.FPool := Self; // mark as used
- end;
- procedure TNodePool.FreeNode(ANode: TDOMNode);
- begin
- ANode.FPool := FFirstFree;
- FFirstFree := ANode;
- end;
- end.
|