Skip to content

Commit

Permalink
perf: drop alter* props in favor of domVisitors
Browse files Browse the repository at this point in the history
The new prop is an object with 3 optional callbacks, `onElement`,
`onDocument` and `onText` which you can use to intercept and tamper
nodes during parsing. Take advantage of
[domutils](https://github.com/fb55/domutils) library to delete, insert
and manipulate those nodes.

The new TRE engine uses a custom parser to support these callbacks.
Therefore, a supplementary tree traversal after parsing is not required
anymore, removing a O(n) computational cost.

BREAKING CHANGE: `alterDOMNode`, `alterDOMData` and `alterDOMChildren`
have been dropped in favor of `domVisitors`. The latter is an object
with 3 optional callbacks, `onElement`, `onDocument` and `onText` which
you can use to intercept and tamper nodes during parsing. Take advantage
of [domutils](https://github.com/fb55/domutils) library to delete,
insert and manipulate those nodes.
  • Loading branch information
jsamr committed Jun 4, 2021
1 parent 9ec6403 commit ffb1f58
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/render-html/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type {
AnchorTagName,
AttribTagNames,
DefaultHTMLElementModels,
DomVisitorCallbacks,
EditsTagNames,
EmbeddedTagNames,
GroupingTagNames,
Expand Down
17 changes: 9 additions & 8 deletions packages/render-html/src/shared-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ import type {
import type {
MixedStyleRecord,
DOMNode,
DOMText,
DOMElement,
TNode,
TBlock,
TText,
TPhrasing,
DocumentContext as TREDocumentContext,
TDocument,
DOMDocument
DomVisitorCallbacks
} from '@native-html/transient-render-engine';
import type { CounterStyleRenderer } from '@jsamr/counter-style';
import type { ComponentType, ReactElement, ReactNode } from 'react';
Expand Down Expand Up @@ -358,13 +356,16 @@ export interface TransientRenderEngineConfig {
*/
ignoreDomNode?: (node: DOMNode) => boolean;
/**
* Change specific DOM nodes children.
* An object which callbacks will be invoked when a DOM element or text node
* has been parsed and its children attached. This is great to tamper the dom,
* remove children, insert nodes, change text nodes data... etc.
*
* @param elementNode - The DOM element to check.
* @returns An array of DOM nodes if the children should be altered, `false`
* or `void` otherwise.
* @remark Each callback is applied during DOM parsing, thus with very little
* overhead. However, it means that one node next siblings won't be available
* since it has not yet been parsed. If you need some siblings logic, apply
* this logic to the children of this node.
*/
alterDOMChildren?: (elementNode: DOMElement) => DOMNode[] | false | void;
domVisitors?: DomVisitorCallbacks;
/**
* Change specific DOM elements.
*
Expand Down

0 comments on commit ffb1f58

Please sign in to comment.