Appearance
BaseImmutableHtmlNode
Base interface for HTML-specific node operations.
typescript
interface BaseImmutableHtmlNode extends BaseImmutableNode<ImmutableHtmlNode>
Description
BaseImmutableHtmlNode
extends the base immutable node interface with HTML-specific type casting methods. It provides the foundation for all HTML nodes (both element and text nodes) and enables safe type conversion between different HTML node types.
Import
typescript
import { BaseImmutableHtmlNode } from '@stripoinc/ui-editor-extensions';
Inheritance
Extends: BaseImmutableNode<ImmutableHtmlNode>
Methods
asText()
Casts the current node to an ImmutableHtmlTextNode
.
typescript
asText(): ImmutableHtmlTextNode
Returns: ImmutableHtmlTextNode
- The current node as a text node
Description: This method performs a type cast to interpret the current node as a text node. Use this when you know the node is a text node and need to access text-specific methods.
Example:
typescript
const node: BaseImmutableHtmlNode = getNode();
if (node.getType() === 'text') {
const textNode = node.asText();
const content = textNode.getTextContent();
}
asElement()
Casts the current node to an ImmutableHtmlElementNode
.
typescript
asElement(): ImmutableHtmlElementNode
Returns: ImmutableHtmlElementNode
- The current node as an element node
Description: This method performs a type cast to interpret the current node as an element node. Use this when you know the node is an element and need to access element-specific methods.
Example:
typescript
const node: BaseImmutableHtmlNode = getNode();
if (node.getType() === 'element') {
const element = node.asElement();
const tagName = element.getTagName();
const classes = element.getClassList();
}
Type Casting Pattern
Safe Type Casting
Always check the node type before casting:
typescript
function processHtmlNode(node: BaseImmutableHtmlNode) {
const nodeType = node.getType();
switch(nodeType) {
case 'element':
const element = node.asElement();
// Access element-specific methods
console.log(`Tag: ${element.getTagName()}`);
break;
case 'text':
const textNode = node.asText();
// Access text-specific methods
console.log(`Text: ${textNode.getTextContent()}`);
break;
default:
console.log(`Unknown node type: ${nodeType}`);
}
}