Class TTreeNode

Unit

Declaration

type TTreeNode = class(TObject)

Description

This class representates an element of the html file

It is stored in an unusual tree representation: All elements form a linked list and the next element is the first children, or if there is none, the next node on the same level, or if there is none, the closing tag of the current parent.
E.g. an XML file like <foo><bar>x</bar></foo> is stored as a quadro-linked list:

  /---------------------------------\
  |         |  -----------          |                                   link to parent (for faster access, it would work without it)
 \|/        | \|/        |          |
  '            '
<foo> <---> <bar>  <---> x <--->  </bar>  <--->  </foo>                 double linked list of tags (previous link again for faster access, a single linked list would work as well)
  .           .                     .               .
 /|\         /|\                   /|\             /|\
  |           -----------------------               |                   single linked of corresponding node
  ---------------------------------------------------

There are functions (getNextSibling, getFirstChild, findNext, ...) to access it like a regular tree, but it is easier and faster to work directly with the list.
Some invariants: (SO: set of opening tags in sequence)
∀a \in SO: a < a.reverse
∀a,b \in SO: a < b < a.reverse => a < b.reverse < a.reverse

Attributes should be accessed with the getAttribute or getAttributeTry method. Or you can enumerate them all for attrib in attributes, if attributes is not nil.
#)

Hierarchy

Overview

Fields

Public typ: TTreeNodeType;
Public value: string;
Public hash: TNodeNameHash;
Public attributes: TTreeAttribute;
Public next: TTreeNode;
Public previous: TTreeNode;
Public parent: TTreeNode;
Public root: TTreeNode;
Public reverse: TTreeNode;
Public namespace: TNamespace;
Public offset: TTreeNodeIntOffset;

Methods

Public procedure freeAll();
Public function findNext(withTyp: TTreeNodeType; const withText:string; findOptions: TTreeNodeFindOptions=[]; sequenceEnd: TTreeNode = nil):TTreeNode;
Public function findChild(withTyp: TTreeNodeType; const withText:string; findOptions: TTreeNodeFindOptions=[]): TTreeNode;
Public function getEnumeratorAttributes: TTreeAttributeEnumerator;
Public function getEnumeratorChildren: TTreeNodeEnumerator;
Public function getEnumeratorDescendants: TTreeNodeEnumerator;
Public function getEnumeratorAncestors: TTreeNodeEnumerator;
Public function getEnumeratorPreceding: TTreeNodeEnumerator;
Public function getEnumeratorFollowing: TTreeNodeEnumerator;
Public function getEnumeratorPrecedingSiblings: TTreeNodeEnumerator;
Public function getEnumeratorFollowingSiblings: TTreeNodeEnumerator;
Public function deepNodeText(separator: string=''):string;
Public function outerXML(insertLineBreaks: boolean = false):string;
Public function innerXML(insertLineBreaks: boolean = false):string;
Public function outerHTML(insertLineBreaks: boolean = false):string;
Public function innerHTML(insertLineBreaks: boolean = false):string;
Public function innerText():string;
Public class function innerTextRangeInternal(from, till_excluding: TTreeNode): string; static;
Public function getValue(): string;
Public function getValueTry(out valueout:string): boolean;
Public function getStringValue(): string;
Public function hasAttribute(const a: string; const cmpFunction: TStringComparisonFunc = nil): boolean;
Public function getAttribute(const a: string):string; overload;
Public function getAttribute(const a: string; const cmpFunction: TStringComparisonFunc):string; overload;
Public function getAttribute(const a: string; const def: string; const cmpFunction: TStringComparisonFunc = nil):string; overload;
Public function getAttributeTry(const a: string; out valueout: string; const cmpFunction: TStringComparisonFunc = nil):boolean;
Public function getAttributeTry(a: string; out valueout: TTreeAttribute; cmpFunction: TStringComparisonFunc = nil):boolean;
Public function getPreviousSibling(): TTreeNode;
Public function getNextSibling(): TTreeNode;
Public function hasChildren(): boolean; inline;
Public function getFirstChild(): TTreeNode;
Public function getParent(): TTreeNode;
Public function getPrevious(): TTreeNode;
Public function getRootHighest(): TTreeNode;
Public function getRootElement(): TTreeNode;
Public function getDocument(): TTreeDocument;
Public function getChildrenCount(types: TTreeNodeTypes): integer;
Public function hasDocumentRoot(): boolean;
Public function getNodeName(): string;
Public function getNamespacePrefix(): string;
Public function getNamespaceURL(): string;
Public function getNamespaceURL(prefixOverride: string; cmpFunction: TStringComparisonFunc = nil): string;
Public procedure getOwnNamespaces(var list: TNamespaceList);
Public procedure getAllNamespaces(var list: TNamespaceList; first: boolean = true);
Public function isNamespaceUsed(const n: TNamespace): boolean;
Public function isDeepEqual(cmpTo: TTreeNode; ignoredTypes: TTreeNodeTypes = [tetComment, tetProcessingInstruction]; cmpFunction: TStringComparisonFunc = nil): boolean;
Public procedure insert(el: TTreeNode);
Public procedure insertSurrounding(before, after: TTreeNode);
Public procedure insertSurrounding(basetag: TTreeNode);
Public procedure addAttribute(a: TTreeAttribute);
Public function addAttribute(const aname, avalue: string): TTreeAttribute;
Public procedure addNamespaceDeclaration(n: TNamespace; overrides: boolean );
Public procedure addNamespaceDeclaration(const prefix, url: string; overrides: boolean );
Public procedure addChild(child: TTreeNode);
Public procedure removeAttribute(a: TTreeAttribute);
Public function cloneShallowNoAttributes(targetDocument: TTreeDocument; newRoot: TTreeNode; var newBaseOffset: TTreeNodeIntOffset): TTreeNode;
Public function clone(targetDocument: TTreeDocument; newRoot: TTreeNode; var newBaseOffset: TTreeNodeIntOffset): TTreeNode; virtual;
Public function clone(targetDocument: TTreeDocument): TTreeNode;
Public function toString(): string; reintroduce;
Public constructor Create;
Public procedure FreeInstance; override;
Public destructor destroy(); override;
Public function caseInsensitiveCompare(const a,b: string): boolean;
Public function caseSensitiveCompare(const a,b: string): boolean;
Public class function compareInDocumentOrder(const a,b: TTreeNode): integer; static;

Properties

Public property defaultProperty[name: string]: string read getAttribute;

Description

Fields

Public typ: TTreeNodeType;

open, close, text or comment node

Public value: string;

tag name for open/close nodes, text for text/comment nodes

Public hash: TNodeNameHash;

nodeNameHash(value)

Public attributes: TTreeAttribute;

nil if there are no attributes

Public next: TTreeNode;

next element as in the file (first child if there are childs, else next on lowest level), so elements form a linked list

Public previous: TTreeNode;

previous element (self.next.previous = self)

Public parent: TTreeNode;
 
Public root: TTreeNode;
 
Public reverse: TTreeNode;

element paired by open/closing, or corresponding attributes

Public namespace: TNamespace;

Currently local namespace prefix. (use getNamespacePrefix and getNamespaceURL to access it)

Public offset: TTreeNodeIntOffset;

count of characters in the document before this element (so document_pchar + offset begins with value)

Methods

Public procedure freeAll();

deletes the tree

Public function findNext(withTyp: TTreeNodeType; const withText:string; findOptions: TTreeNodeFindOptions=[]; sequenceEnd: TTreeNode = nil):TTreeNode;

Returns the element with the given type and text which occurs before sequenceEnd.
This function is nil-safe, so if you call TTreeNode(nil).findNext(...) it will return nil

Public function findChild(withTyp: TTreeNodeType; const withText:string; findOptions: TTreeNodeFindOptions=[]): TTreeNode;

Find a matching direct child (equivalent to findNext with certain parameters, but easier to use)
A direct child of X is a node Y with Y.parent = X.
The options tefoNoChildren, tefoNoGrandChildren have of course no effect. (former is set to false, latter to true)

Public function getEnumeratorAttributes: TTreeAttributeEnumerator;
 
Public function getEnumeratorChildren: TTreeNodeEnumerator;
 
Public function getEnumeratorDescendants: TTreeNodeEnumerator;
 
Public function getEnumeratorAncestors: TTreeNodeEnumerator;
 
Public function getEnumeratorPreceding: TTreeNodeEnumerator;
 
Public function getEnumeratorFollowing: TTreeNodeEnumerator;
 
Public function getEnumeratorPrecedingSiblings: TTreeNodeEnumerator;
 
Public function getEnumeratorFollowingSiblings: TTreeNodeEnumerator;
 
Public function deepNodeText(separator: string=''):string;
 
Public function outerXML(insertLineBreaks: boolean = false):string;
 
Public function innerXML(insertLineBreaks: boolean = false):string;
 
Public function outerHTML(insertLineBreaks: boolean = false):string;
 
Public function innerHTML(insertLineBreaks: boolean = false):string;
 
Public function innerText():string;

Returns a human readable text for an HTML node. The exact output might change in future version (e.g. more/less line breaks)

Public class function innerTextRangeInternal(from, till_excluding: TTreeNode): string; static;
 
Public function getValue(): string;

get the value of this element

Public function getValueTry(out valueout:string): boolean;

get the value of this element if the element exists

Public function getStringValue(): string;
 
Public function hasAttribute(const a: string; const cmpFunction: TStringComparisonFunc = nil): boolean;

returns if an attribute with that name exists. cmpFunction controls is used to compare the attribute name the searched string. (can be used to switch between case/in/sensitive)

Public function getAttribute(const a: string):string; overload;

get the value of an attribute of this element or '' if this attribute doesn't exist cmpFunction controls is used to compare the attribute name the searched string. (can be used to switch between case/in/sensitive)

Public function getAttribute(const a: string; const cmpFunction: TStringComparisonFunc):string; overload;

get the value of an attribute of this element or '' if this attribute doesn't exist cmpFunction controls is used to compare the attribute name the searched string. (can be used to switch between case/in/sensitive)

Public function getAttribute(const a: string; const def: string; const cmpFunction: TStringComparisonFunc = nil):string; overload;

get the value of an attribute of this element or '' if this attribute doesn't exist cmpFunction controls is used to compare the attribute name the searched string. (can be used to switch between case/in/sensitive)

Public function getAttributeTry(const a: string; out valueout: string; const cmpFunction: TStringComparisonFunc = nil):boolean;

get the value of an attribute of this element and returns false if it doesn't exist cmpFunction controls is used to compare the attribute name the searched string. (can be used to switch between case/in/sensitive)

Public function getAttributeTry(a: string; out valueout: TTreeAttribute; cmpFunction: TStringComparisonFunc = nil):boolean;

get the value of an attribute of this element and returns false if it doesn't exist cmpFunction controls is used to compare the attribute name the searched string. (can be used to switch between case/in/sensitive)

Public function getPreviousSibling(): TTreeNode;

Get the previous element on the same level or nil if there is none

Public function getNextSibling(): TTreeNode;

Get the next element on the same level or nil if there is none

Public function hasChildren(): boolean; inline;
 
Public function getFirstChild(): TTreeNode;

Get the first child, or nil if there is none

Public function getParent(): TTreeNode;

Searchs the parent

Public function getPrevious(): TTreeNode;

Searchs the previous

Public function getRootHighest(): TTreeNode;

Returns the highest node ancestor

Public function getRootElement(): TTreeNode;

Returns the highest element node ancestor

Public function getDocument(): TTreeDocument;

Returns the document node containing this node. Raises an exception if there is no associated document

Public function getChildrenCount(types: TTreeNodeTypes): integer;
 
Public function hasDocumentRoot(): boolean;
 
Public function getNodeName(): string;

Returns the name as namespaceprefix:name if a namespace exists, or name otherwise. Only attributes, elements and PIs have names.

Public function getNamespacePrefix(): string;

Returns the namespace prefix. (i.e. 'a' for 'a:b', '' for 'b')

Public function getNamespaceURL(): string;

Returns the namespace url. (very slow, it searches the parents for a matching xmlns attribute) cmpFunction controls is used to compare the xmlns: attribute name the searched string. (can be used to switch between case/in/sensitive)

Public function getNamespaceURL(prefixOverride: string; cmpFunction: TStringComparisonFunc = nil): string;

Returns the url of a namespace prefix, defined in this element or one of his parents cmpFunction controls is used to compare the xmlns: attribute name the searched string. (can be used to switch between case/in/sensitive)

Public procedure getOwnNamespaces(var list: TNamespaceList);

Returns all namespaces declared or used in this element and its attributes

Public procedure getAllNamespaces(var list: TNamespaceList; first: boolean = true);

Returns all namespaces declared/used by this element and all ancestors

Public function isNamespaceUsed(const n: TNamespace): boolean;

Tests if a namespace is used by this element or any child (same prefix + url)

Public function isDeepEqual(cmpTo: TTreeNode; ignoredTypes: TTreeNodeTypes = [tetComment, tetProcessingInstruction]; cmpFunction: TStringComparisonFunc = nil): boolean;
 
Public procedure insert(el: TTreeNode);

inserts el after the current element (does only change next+previous, not reverse+parent)

Public procedure insertSurrounding(before, after: TTreeNode);

Surrounds self by before and after, i.e. inserts "before" directly before the element and "after" directly after its closing tag (slow)

Public procedure insertSurrounding(basetag: TTreeNode);

inserts basetag before the current tag, and creates a matching closing tag after the closing tag of self (slow)

Public procedure addAttribute(a: TTreeAttribute);
 
Public function addAttribute(const aname, avalue: string): TTreeAttribute;
 
Public procedure addNamespaceDeclaration(n: TNamespace; overrides: boolean );
 
Public procedure addNamespaceDeclaration(const prefix, url: string; overrides: boolean );
 
Public procedure addChild(child: TTreeNode);
 
Public procedure removeAttribute(a: TTreeAttribute);
 
Public function cloneShallowNoAttributes(targetDocument: TTreeDocument; newRoot: TTreeNode; var newBaseOffset: TTreeNodeIntOffset): TTreeNode;
 
Public function clone(targetDocument: TTreeDocument; newRoot: TTreeNode; var newBaseOffset: TTreeNodeIntOffset): TTreeNode; virtual;
 
Public function clone(targetDocument: TTreeDocument): TTreeNode;
 
Public function toString(): string; reintroduce;

converts the element to a string (not recursive)

Public constructor Create;
 
Public procedure FreeInstance; override;
 
Public destructor destroy(); override;
 
Public function caseInsensitiveCompare(const a,b: string): boolean;

returns true if a=b case insensitive. Can be passed to getAttribute

Public function caseSensitiveCompare(const a,b: string): boolean;

returns true if a=b case sensitive. Can be passed to getAttribute

Public class function compareInDocumentOrder(const a,b: TTreeNode): integer; static;
 

Properties

Public property defaultProperty[name: string]: string read getAttribute;
 

Generated by PasDoc 0.16.0.