/* eslint-disable @typescript-eslint/no-unused-vars */
import React from 'react';
import ListItemCode from '../components/ListItemCode';
import Page from '../Page';
import useToolkit from '../toolkit/useToolkit';
import RefHtmlparser2 from '../components/RefHtmlparser2';
import removeOlChildrenConfig from './cards/removeOlChildrenConfig';
import replaceDataConfig from './cards/replaceDataConfig';
import insertingElementConfig from './cards/insertingElementConfig';
import ignoreDomNodeConfig from './cards/ignoreDomNodeConfig';
import selectDomRootConfig from './cards/selectDomRootConfig';
const prerenderingSrc = `import React, { useState, useEffect, useMemo } from 'react';
import {
TRenderEngineProvider,
RenderHTMLConfigProvider,
RenderHTMLSource,
useAmbientTRenderEngine
} from 'react-native-render-html';
import { findAll } from 'domutils';
function isImgElement(node) {
return node.name === 'img';
}
function RenderSource({ html }) {
const [isDomReady, setIsDomReady] = useState(false);
// Let's use the TRE provided from the root of our app
// via TRenderEngineProvider to build the DOM
const engine = useAmbientTRenderEngine();
const dom = useMemo(() => engine.parseDocument(html), [html, engine]);
// Use effect to inspect the DOM
useEffect(function inspectDom(){
// Do any pre-rendering logic here
const images = findAll(isImgElement, dom, true);
// Do stuff with images such as preloading
// ...
// When preloading is done, set isDomReady to true!
setIsDomReady(true);
},[dom]);
return isDomReady ?
: null;
}
const html = \`
hello world!
\`
export default function App() {
return (
);
}`;
export default function PageGuideDomTampering() {
const {
Acronym,
Admonition,
Bold,
DList,
DListItem,
DListTitle,
Header,
Paragraph,
Chapter,
SourceDisplay,
RefLibrary,
RefRNSymbol,
RefHtmlElement,
RefCssProperty,
RefRenderHtmlProp,
RefHtmlAttr,
RefTRE,
RefDOM,
RefRenderHTMLExport,
RefDoc,
RenderHtmlCard,
Section,
InlineCode,
Hyperlink,
List,
ListItem,
SvgFigure
} = useToolkit();
return (
This library offers rich integration with the {' '}
ecosystem. Thanks to the new , 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.
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.
The API offers one prop, ,
which is a record of 3 optional fields:
Triggered when the root DOM has been
parsed, at the very end of the parsing phase.
Triggered when a DOM has been parsed along
with its children.
Triggered when a DOM 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.
You are invited to use{' '}
{' '}
library to handle DOM querying and manipulation (see example below).
This library is already a direct dependency of .
Two props can be used to ignore nodes.{' '}
is an array of lowercase
tags to exclude, and is a
function taking every parsed DOM node and offering you to reject it by
returning true. Both props are processed at
parse time.
When is invoked, the
passed node has not been attached to his parent yet. But the parent
is given as a second argument.
This library provides prop
to select a subtree to render. See example below:
When returns a falsy value,
the initial root will be selected.
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:
accepts all{' '}
component props pertaining
to the layer such as{' '}
,{' '}
(all styling props) and DOM
related such as ,{' '}
...
accepts all{' '}
component props pertaining
to the layer such as{' '}
,{' '}
,{' '}
, ...
accepts all{' '}
RenderHTML component props pertaining to the
document such as ,{' '}
,{' '}
...
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 usefuld for apps which will render hundreds to
thousands of small snippets such as chat apps.
);
}