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 Tree#
The API offers one prop, โdomVisitors, which is a record of 3 optional fields:
โ
onDocumentTriggered when the root DOM โ
Documenthas been parsed, at the very end of the parsing phase.โ
onElementTriggered when a DOM โ
Elementhas been parsed along with its children.โ
onTextTriggered when a DOM โ
Textnode 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#
- JSX Source
- HTML Snippet
- TRT Snapshot
Example: Altering Data#
- JSX Source
- HTML Snippet
- TRT Snapshot
Example: Inserting Elements#
- JSX Source
- HTML Snippet
- TRT Snapshot
Ignoring nodes#
Two 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 Conditionally#
- JSX Source
- HTML Snippet
- TRT Snapshot
caution
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 Selection#
This library provides โselectDomRoot prop to select a subtree to render. See example below:
- JSX Source
- HTML Snippet
- TRT Snapshot
note
When โselectDomRoot returns a falsy value, the initial root will be selected.
Prerendering#
In 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:
โ
TRenderEngineProvideraccepts all โRenderHTMLcomponent props pertaining to the โTransient Render Engine layer such as โcustomHTMLElementModels, โclassesStyles(all styling props) and DOM related such as โdomVisitors, โselectDomRoot...โ
RenderHTMLConfigProvideraccepts all โRenderHTMLcomponent props pertaining to the โRendering layer such as โrenderers, โrenderersProps, โcomputeEmbeddedMaxWidth, ...โ
RenderHTMLSourceaccepts allRenderHTMLcomponent 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.