Skip to content
This repository has been archived by the owner on May 24, 2024. It is now read-only.

vivaxy/WXML

Repository files navigation

WXML

🌇WXML parser and serializer.

Build Status NPM Version NPM Downloads MIT License Standard Version Codecov DOI

Install

yarn add @vivaxy/wxml or npm i @vivaxy/wxml --save

Usage

import * as wxml from '@vivaxy/wxml';
const parsed = wxml.parse('<view></view>');
wxml.traverse(parsed, function visitor(node, parent) {
  const type = node.type;
  const parentNode = node.parentNode;

  if (type === wxml.NODE_TYPES.ELEMENT) {
    // handle element node
    const tagName = node.tagName;
    const attributes = node.attributes; // an object represents the attributes
    const childNodes = node.childNodes;
    const selfClosing = node.selfClosing; // if a node is self closing, like `<tag />`
  } else if (type === wxml.NODE_TYPES.TEXT) {
    // handle text node
    const textContent = node.textContent;
  } else if (type === wxml.NODE_TYPES.COMMENT) {
    // handle comment node
    const comment = node.comment;
  }
});
const serialized = wxml.serialize(parsed);

API

parse

(input: string) => AST

traverse

(node: Node, visitor: (node: Node, parent: Node) => void) => void

serialize

(node: Node) => string