Have ideas to improve npm?Join in the discussion! »

    hast-util-from-selector

    1.0.2 • Public • Published

    hast-util-from-selector

    Build Coverage Downloads Size Sponsors Backers Chat

    hast utility to create nodes from an advanced CSS selector.

    Install

    npm:

    npm install hast-util-from-selector

    Use

    var from = require('hast-util-from-selector')
     
    console.log(from('p svg[viewbox=0 0 10 10] circle[cx=10][cy=10][r=10]'))

    Yields:

    {
      type: 'element',
      tagName: 'p',
      properties: {},
      children: [
        {
          type: 'element',
          tagName: 'svg',
          properties: {viewBox: '0 0 10 10'},
          children: [
            {
              type: 'element',
              tagName: 'circle',
              properties: {cx: '10', cy: '10', r: '10'},
              children: []
            }
          ]
        }
      ]
    }

    API

    fromSelector([selector][, options])

    Create one or more element nodes from a CSS selector.

    Parameters
    • selector (string, optional) — CSS selector
    • space (string, optional) — Treated as options.space
    • options.space (enum, 'svg' or 'html', default: 'html') — Which space first element in the selector is in. When an svg is created in HTML, the space is switched automatically to SVG
    Returns

    Element.

    Support

    • * (universal selector, creates a div in HTML, g in SVG)
    • p (type selector)
    • .class (class selector)
    • #id (id selector)
    • [attr] (attribute existence, creates a boolean)
    • [attr=value] (attribute equality)
    • article p (descendant combinator)
    • article > p (child combinator)
    • section h1 + p (next-sibling combinator, not at root)
    • section h1 ~ p (subsequent-sibling combinator, not at root)

    Security

    Use of from-selector can open you up to a cross-site scripting (XSS) attack as values are injected into the syntax tree.

    Either do not use user input in from-selector or use hast-util-santize.

    Related

    Contribute

    See contributing.md in syntax-tree/.github for ways to get started. See support.md for ways to get help.

    This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.

    License

    MIT © Titus Wormer

    Install

    npm i hast-util-from-selector

    DownloadsWeekly Downloads

    24

    Version

    1.0.2

    License

    MIT

    Unpacked Size

    9.6 kB

    Total Files

    4

    Last publish

    Collaborators

    • avatar
    • avatar