Giter Club home page Giter Club logo

dom-parse-filter's Introduction

DOM Parse Filter Challenge

Transform and Filter DOM

  • Output match Node type
  • Unsupported DOM nodes should be filtered out
  • Supported types are stated in Type enum
enum Type {
  html = "html",
  head = "head",
  meta = "meta",
  script = "script",
  link = "link",
  body = "body",
  div = "div",
  span = "span",
  p = "p",
  button = "button",
  a = "a",
  form = "form",
  input = "input",
  img = "img",
  text = "text",
}

interface Node {
  type: Type;
  attributes: Record<string, string>;
  children: Node[];
  text?: string;
}

Solution

  • recursively iterate DOM nodes
  • leaf nodes are determined by looking at children.length
const getAttributes = (node: HTMLElement | Element) => {
  const attributeNames = node.getAttributeNames();
  const attributes: Record<string, string> = {};
  attributeNames.forEach((name: string) => {
    const attributeValue = node.getAttribute(name);
    if (attributeValue != null) {
      attributes[name] = attributeValue;
    }
  });
  return attributes;
};

const isSupportedElement = (tagName: string) => {
  return Type[tagName.toLowerCase() as keyof typeof Type];
};

function recursiveTransform(node: HTMLElement | Element): Node {
  if (isSupportedElement(node.tagName) == null) {
    //@ts-expect-error: keep typing in sync with HTMLElement
    return;
  }

  if (node.children.length > 0) {
    const children = [];
    for (const child of node.children) {
      if (isSupportedElement(child.tagName) != null) {
        children.push(recursiveTransform(child));
      }
    }
    return {
      type: Type[node.tagName.toLowerCase() as keyof typeof Type],
      children,
      attributes: getAttributes(node),
    };
  } else {
    return {
      type: Type[node.tagName.toLowerCase() as keyof typeof Type],
      attributes: getAttributes(node),
      text: node.textContent ?? "",
      children: [],
    };
  }
}

Steps to run example

yarn
yarn run dev
  • Above assumes node and yarn are pre-installed
  • Output is displayed in the dev consoles

Screenshots

Screenshots can be found at screenshots folder

Screenshot 1

dom-parse-filter's People

Contributors

bigappleinsider avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.