Match & splice a tokenized HTML stream with css selectors. Like html-select but maintained.
npm install html-select2!Last version





> Match & splice a tokenized HTML stream with css selectors. Like html-select but maintained.
``bash`
$ npm install html-select2 --save
Given a tokenized stream from
html-tokenize, this program will
print the dt tags matching the selector 'ul > li dt':
` js
const select = require('html-select2')
const tokenize = require('html-tokenize')
const fs = require('fs')
const path = require('path')
const stream = select('ul > li dt', function (e) {
console.log(' MATCH ')
e.createReadStream().on('data', function (row) {
console.log([row[0], row[1].toString()])
})
})
fs
.createReadStream(path.join(__dirname, '/page.html'))
.pipe(tokenize())
.pipe(stream)
stream.resume()
`
The s.resume() is necessary to put the stream into flow mode since we aren'ts
doing anything with the output of .
Now this html input:
` html
hello there!
This presentation contains these examples:
require() in the browser
gives this output:
`
MATCH
[ 'open', '' ]
[ 'text', 'browserify' ]
[ 'close', ' ' ]
MATCH
[ 'open', '' ]
[ 'text', 'streams' ]
[ 'close', ' ' ]
MATCH
[ 'open', '' ]
[ 'text', 'ndarray' ]
[ 'close', ' ' ]
MATCH
[ 'open', '' ]
[ 'text', 'music' ]
[ 'close', ' ' ]
MATCH
[ 'open', '' ]
[ 'text', 'voxeljs' ]
[ 'close', ' ' ]
MATCH
[ 'open', '' ]
[ 'text', 'trumpet' ]
[ 'close', ' ' ]
`$3
Using the same html file from the previous example,
this script converts everything inside
dt elements to uppercase:` js
const select = require('html-select2')
const tokenize = require('html-tokenize')
const through = require('through2')
const fs = require('fs')
const path = require('path')const stream = select('dt', function (e) {
const tr = through.obj(function (row, buf, next) {
this.push([ row[0], String(row[1]).toUpperCase() ])
next()
})
tr.pipe(e.createStream()).pipe(tr)
})
fs.createReadStream(path.join(__dirname, '/page.html'))
.pipe(tokenize())
.pipe(stream)
.pipe(through.obj(function (row, buf, next) {
this.push(row[1])
next()
}))
.pipe(process.stdout)
`Running the transform program yields this html output:
` html
presentation examples
hello there!
This presentation contains these examples:
- BROWSERIFY
- node-style
require() in the browser
- STREAMS
- shuffle data around with backpressure
- NDARRAY
- n-dimensional matricies on top of typed arrays
- MUSIC
- make music with code
- VOXELJS
- make minecraft-style games in webgl
- TRUMPET
- transform html with css selectors and streams
`API
` js
const select = require('html-select2')
`$3
Create a new html selector transform stream
sel.sel expects tokenized html objects
as input and writes tokenized html objects as output.If
selector and cb are given, sel.select(selector, cb) is called
automatically.$3
Register a callback
cb(elem) to fire whenever the css selector string
matches.$3
Create a readable object mode stream at the selector. The readable stream
contains all the matching tokenized html objects including the element that
matched and its closing tag.
If
opts.inner is true, only read the inner content. Otherwrite read the outer
content.$3
Create a writable object mode stream at the selector. The writable stream writes
into the document stream at the selector, replacing the existing content.
If
opts.inner is true, only write to the inner content. Otherwrite write to
the outer content.$3
Create a duplex object mode stream at the selector. The writable side will write
into the document stream at the selector, replacing the existing content. The
readable side contains the existing content.
If
opts.inner is true, only read and write to the inner content. Otherwrite
read and write to the outer content.$3
Set an attribute named by
key to value.If
value is true, the attribute will appear without an equal sign in the
markup.$3
Remove an attribute named by
key.$3
Return an object with a single attribute value named by
key`.Return an object with all attributes.
#### elem.name
The string name of the tag.
#### elem.on('close', function () {})
When a matched element is closed for reading and writing, this event fires.
Internally html-select2 uses cssauron.
html-select2 © Kiko Beats, released under the MIT License.
Authored and maintained by Kiko Beats with help from contributors.
> kikobeats.com · GitHub Kiko Beats · Twitter @Kikobeats