DOM Tampering
This library offers rich integration with the โhtmlparser2
ecosystem. Thanks to the new โTRE, you can process DOM nodes at parse time. It implies that the operation doesn't require a tree traversal contrary to legacy versions, and thus adds up very little overhead.
caution
One important downside is that a DOM node next sibling won't have been parsed yet. However, the children and parent of this node are guaranteed to be attached to this node.
#
Altering the DOM TreeThe API offers one prop, โdomVisitors
, which is a record of 3 optional fields:
โ
onDocument
Triggered when the root DOM โ
Document
has been parsed, at the very end of the parsing phase.โ
onElement
Triggered when a DOM โ
Element
has been parsed along with its children.โ
onText
Triggered when a DOM โ
Text
node has been parsed along with its content.
Those callback should not return anything. Instead, you should change the node or its children in place, or just read its content for inspection purposes.
tip
You are invited to use โdomutils
library to handle DOM querying and manipulation (see example below). This library is already a direct dependency of โhtmlparser2
.
#
Example: Removing Elements#
Example: Altering Data#
Example: Inserting Elements#
Ignoring nodesTwo props can be used to ignore nodes. โignoredDomTags
is an array of lowercase tags to exclude, and โignoreDomNode
is a function taking every parsed DOM node and offering you to reject it by returning true
. Both props are processed at parse time.
#
Example: Ignoring Nodes Conditionallycaution
When โignoreDomNode
is invoked, the passed node has not been attached to his parent yet. But the parent is given as a second argument.
#
Root SelectionThis library provides โselectDomRoot
prop to select a subtree to render. See example below:
note
When โselectDomRoot
returns a falsy value, the initial root will be selected.
#
PrerenderingIn some scenarios, you might want to inspect the DOM before rendering, and even perform asynchronous operations based on your findings. Use cases might involve, for example:
Fetching data from a Web API;
Pre-caching media assets.
To do so, we will take advantage of the โcomposite rendering architecture and the dom source feature:
Let's note a few important details in this example:
โ
TRenderEngineProvider
accepts all โRenderHTML
component props pertaining to the โTransient Render Engine layer such as โcustomHTMLElementModels
, โclassesStyles
(all styling props) and DOM related such as โdomVisitors
, โselectDomRoot
...โ
RenderHTMLConfigProvider
accepts all โRenderHTML
component props pertaining to the โRendering layer such as โrenderers
, โrenderersProps
, โcomputeEmbeddedMaxWidth
, ...โ
RenderHTMLSource
accepts allRenderHTML
component props pertaining to the document such as โsource
, โonTTreeChange
, โcontentWidth
...The general recommendation for this three-layers rendering architecture is that the engine and configuration should be put near the top of your App to factor the cost of instantiating the engine. This is especially usefull for apps which will render hundreds to thousands of small snippets such as chat apps.