Skip to content

feat: add TText renderer to override all text nodes, including anonymous text#46

Open
keerbee-dev wants to merge 1 commit into
native-html:mainfrom
keerbee-dev:main
Open

feat: add TText renderer to override all text nodes, including anonymous text#46
keerbee-dev wants to merge 1 commit into
native-html:mainfrom
keerbee-dev:main

Conversation

@keerbee-dev

Copy link
Copy Markdown

Summary

This PR adds support for a new special key TText in the renderers prop to override the rendering of all text nodes, including anonymous text (text without an enclosing HTML tag).

The Problem

Currently, custom renderers are resolved only by tagName. Anonymous text has tagName === null, so it cannot be intercepted. Developers must override every possible tag (p, span, div, …) and still miss anonymous text.

Example

<div>
  This is anonymous text   <!-- ❌ Cannot be overridden -->
  <span>Tagged text</span> <!-- ✅ Can be overridden via "span" key -->
</div>

The Fix

Modify RenderRegistry.getRendererConfigForTNode to check for TText when tnode.type === 'text' and no custom renderer was found by tagName.

Implementation

getRendererConfigForTNode<T extends TNode>(tnode: T): RendererConfig<T> {
  let custom = this.getCustomRendererForTNode(tnode);

  if (!custom && tnode.type === 'text') {
    const textRenderer = this.customRenderers['TText'];
    if (textRenderer) {
      custom = textRenderer as any;
    }
  }

  return {
    Custom: custom,
    Default: this.getDefaultRendererForTNode(tnode)
  };
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant