class TemplateInspector

This class helps the RulesGenerator by analyzing a given template in order to answer questions such as "can this tag be a child/descendant of that other tag?" and others related to the HTML5 content model.

We use the HTML5 specs to determine which children or descendants should be allowed or denied based on HTML5 content models. While it does not exactly match HTML5 content models, it gets pretty close. We also use HTML5 "optional end tag" rules to create closeParent rules.

Currently, this method does not evaluate elements created with correctly, or attributes created with and may never will due to the increased complexity it would entail. Additionally, it does not evaluate the scope of . For instance, it will treat as if it was

Constants

XMLNS_XSL

XSL namespace

Properties

protected string[] $allowChildBitfields
protected bool $allowsChildElements
protected bool $allowsText
protected array[] $branches
protected string $contentBitfield
protected string $defaultBranchBitfield
protected string $denyDescendantBitfield
protected DOMDocument $dom
protected bool $hasElements
protected bool $hasRootText
protected bool $isBlock
protected bool $isEmpty
protected bool $isFormattingElement
protected bool $isPassthrough
protected bool $isTransparent
protected bool $isVoid
protected array $leafNodes
protected bool $preservesNewLines
protected array $rootBitfields
protected array $rootNodes
protected DOMXPath $xpath

Methods

__construct(string $template)

Constructor

bool
allowsChild(TemplateInspector $child)

Return whether this template allows a given child

bool
allowsDescendant(TemplateInspector $descendant)

Return whether this template allows a given descendant

bool
allowsChildElements()

Return whether this template allows elements as children

bool
allowsText()

Return whether this template allows text nodes as children

bool
closesParent(TemplateInspector $parent)

Return whether this template automatically closes given parent template

mixed
evaluate(string $expr, DOMElement|null $node = null)

Evaluate an XPath expression

bool
isBlock()

Return whether this template should be considered a block-level element

bool
isFormattingElement()

Return whether this template adds to the list of active formatting elements

bool
isEmpty()

Return whether this template uses the "empty" content model

bool
isPassthrough()

Return whether this template lets content through via an xsl:apply-templates element

bool
isTransparent()

Return whether this template uses the "transparent" content model

bool
isVoid()

Return whether all branches have an ancestor that is a void element

bool
preservesNewLines()

Return whether this template preserves the whitespace in its descendants

analyseContent()

Analyses the content of the whole template and set $this->contentBitfield accordingly

analyseRootNodes()

Records the HTML elements (and their bitfield) rendered at the root of the template

analyseBranches()

Analyses each branch that leads to an tag

bool
anyBranchHasProperty(string $methodName)

Test whether any branch of this template has an element that has given property

void
computeBitfields()

Compute the allowChildBitfields and denyDescendantBitfield properties

void
computeAllowsChildElements()

Compute the allowsChildElements property

void
computeAllowsText()

Compute the allowsText property

void
computeFormattingElement()

Compute the isFormattingElement property

void
computeIsEmpty()

Compute the isEmpty property

void
computeIsTransparent()

Compute the isTransparent property

void
computeIsVoid()

Compute the isVoid property

void
computePreservesNewLines()

Compute the preservesNewLines property

bool
elementIsBlock(DOMElement $element)

Test whether given element is a block-level element

string
getStyle(DOMElement $node, bool $deep = false)

Retrieve and return the inline style assigned to given element

bool
isFormattingSpan(DOMElement $node)

Test whether given node is a span element used for formatting

void
storeLeafNodes()

Store the names of every leaf node

static bool
match(string $bitfield1, string $bitfield2)

Test whether two bitfields have any bits in common

Details

at line 147
__construct(string $template)

Constructor

Parameters

string $template

Template content

at line 165
bool allowsChild(TemplateInspector $child)

Return whether this template allows a given child

Parameters

TemplateInspector $child

Return Value

bool

at line 193
bool allowsDescendant(TemplateInspector $descendant)

Return whether this template allows a given descendant

Parameters

TemplateInspector $descendant

Return Value

bool

at line 210
bool allowsChildElements()

Return whether this template allows elements as children

Return Value

bool

at line 220
bool allowsText()

Return whether this template allows text nodes as children

Return Value

bool

at line 231
bool closesParent(TemplateInspector $parent)

Return whether this template automatically closes given parent template

Parameters

TemplateInspector $parent

Return Value

bool

at line 255
mixed evaluate(string $expr, DOMElement|null $node = null)

Evaluate an XPath expression

Parameters

string $expr

XPath expression

DOMElement|null $node

Context node

Return Value

mixed

at line 265
bool isBlock()

Return whether this template should be considered a block-level element

Return Value

bool

at line 275
bool isFormattingElement()

Return whether this template adds to the list of active formatting elements

Return Value

bool

at line 285
bool isEmpty()

Return whether this template uses the "empty" content model

Return Value

bool

at line 295
bool isPassthrough()

Return whether this template lets content through via an xsl:apply-templates element

Return Value

bool

at line 305
bool isTransparent()

Return whether this template uses the "transparent" content model

Return Value

bool

at line 315
bool isVoid()

Return whether all branches have an ancestor that is a void element

Return Value

bool

at line 325
bool preservesNewLines()

Return whether this template preserves the whitespace in its descendants

Return Value

bool

at line 333
protected analyseContent()

Analyses the content of the whole template and set $this->contentBitfield accordingly

at line 350
protected analyseRootNodes()

Records the HTML elements (and their bitfield) rendered at the root of the template

at line 389
protected analyseBranches()

Analyses each branch that leads to an tag

at line 415
protected bool anyBranchHasProperty(string $methodName)

Test whether any branch of this template has an element that has given property

Parameters

string $methodName

Return Value

bool

at line 436
protected void computeBitfields()

Compute the allowChildBitfields and denyDescendantBitfield properties

Return Value

void

at line 481
protected void computeAllowsChildElements()

Compute the allowsChildElements property

A template allows child Elements if it has at least one xsl:apply-templates and none of its ancestors have the text-only ("to") property

Return Value

void

at line 493
protected void computeAllowsText()

Compute the allowsText property

A template is said to allow text if none of the leaf elements disallow text

Return Value

void

at line 515
protected void computeFormattingElement()

Compute the isFormattingElement property

A template is said to be a formatting element if all (non-zero) of its branches are entirely composed of formatting elements

Return Value

void

at line 540
protected void computeIsEmpty()

Compute the isEmpty property

A template is said to be empty if it has no xsl:apply-templates elements or any there is a empty element ancestor to an xsl:apply-templates element

Return Value

void

at line 553
protected void computeIsTransparent()

Compute the isTransparent property

A template is said to be transparent if it has at least one branch and no non-transparent elements in its path

Return Value

void

at line 578
protected void computeIsVoid()

Compute the isVoid property

A template is said to be void if it has no xsl:apply-templates elements or any there is a void element ancestor to an xsl:apply-templates element

Return Value

void

at line 588
protected void computePreservesNewLines()

Compute the preservesNewLines property

Return Value

void

at line 614
protected bool elementIsBlock(DOMElement $element)

Test whether given element is a block-level element

Parameters

DOMElement $element

Return Value

bool

at line 636
protected string getStyle(DOMElement $node, bool $deep = false)

Retrieve and return the inline style assigned to given element

Parameters

DOMElement $node

Context node

bool $deep

Whether to retrieve the content of all xsl:attribute descendants

Return Value

string

at line 664
protected bool isFormattingSpan(DOMElement $node)

Test whether given node is a span element used for formatting

Will return TRUE if the node is a span element with a class attribute and/or a style attribute and no other attributes

Parameters

DOMElement $node

Return Value

bool

at line 694
protected void storeLeafNodes()

Store the names of every leaf node

A leaf node is defined as the closest non-XSL ancestor to an xsl:apply-templates element

Return Value

void

at line 709
static protected bool match(string $bitfield1, string $bitfield2)

Test whether two bitfields have any bits in common

Parameters

string $bitfield1
string $bitfield2

Return Value

bool