Socket
Socket
Sign inDemoInstall

trumpet

Package Overview
Dependencies
1
Maintainers
1
Versions
48
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

trumpet


Version published
Maintainers
1
Created

Readme

Source

trumpet

Parse and transform streaming html using css selectors.

example

select

var transit = require('trumpet');
var tr = transit();

tr.select('.b span', function (node) {
    node.html(function (html) {
        console.log(node.name + ': ' + html);
    });
});

var fs = require('fs');
fs.createReadStream(__dirname + '/select.html').pipe(tr);
<html>
  <head>
    <title>beep</title>
  </head>
  <body>
    <div class="a">¡¡¡</div>
    <div class="b">
      <span>tacos</span>
      <span>y</span>
      <span>burritos</span>
    </div>
    <div class="a">!!!</div>
  </body>
</html>

output:

$ node example/select.js 
span: tacos
span: y
span: burritos

methods

var trumpet = require('trumpet')

var tr = trumpet(fn)

Create a new trumpet stream. This stream is readable and writable. Pipe an html stream into tr and get back a transformed html stream.

Optionally supply a function fn(node) that will get raw parsing events.

node is an object with these keys:

  • type - 'open', 'close', or 'text'
  • source - source string of the present parse event from the input stream
  • name - name of the html element (div, span, ...)
  • attributes - attributes from the html element
  • write(s) - write text to the output stream
  • parser - the raw sax parser object

These nodes are different from the higher-level nodes you get with .select().

tr.select(selector, fn)

Fire fn(node) for every element in the html stream that matches the css selector.

The nodes are described in the nodes section of this document.

nodes

node.name

The name of the html element node, such as 'div' or 'span'.

node.attributes

An object with all the html attributes.

For example,

<img src="/beep.png" width="32" height="32">

has an attribute object of:

{ src : 'beep.png', width : '32', height : '32' }

node.html(cb)

Get the inner text and html for the element, which may not have arrived yet.

cb(text) fires when the inner contents are ready.

install

With npm do:

npm install trumpet

license

MIT/X11

Keywords

FAQs

Last updated on 11 Mar 2012

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc