New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

cornet

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cornet - npm Package Compare versions

Comparing version

to
0.1.0

15

example.js

@@ -17,2 +17,15 @@ var Parser = require("htmlparser2").WritableStream,

});
});
});
//does the same
var i = 0;
cornet.select(".repo_list h3", function(elem){
console.log("repo %d: %s", ++i, $(elem).text().trim());
});
//sometimes, you only want to get a single element
var onTitle = cornet.select("title", function(title){
console.log("Page title:", $(title).text().trim());
cornet.removeLister("element", onTitle);
});

9

index.js

@@ -24,12 +24,13 @@ var DomHandler = require("domhandler").Handler,

}
this.on("element", function(elem){
function onElem(elem){
if(selector(elem)) cb(elem);
});
}
this.on("element", onElem);
return onElem;
};
Handler.prototype.remove = function(selector){
this.select(selector, DomUtils.removeElement);
return this.select(selector, DomUtils.removeElement);
};
module.exports = Handler;
{
"name": "cornet",
"author": "Felix Boehm <me@feedic.com>",
"version": "0.0.0",
"version": "0.1.0",
"description": "transform streaming html using css selectors",

@@ -6,0 +6,0 @@ "main": "index.js",

@@ -10,5 +10,11 @@ ##About

* The ammount of usable CSS selectors is increased dramatically thanks to [`fb55/CSSselect`](https://github.com/fb55/CSSselect).
* `cornet` uses [`fb55/htmlparser2`](https://github.com/fb55/CSSselect), which is probably the fastest HTML parser currently available for node. And it's much less strict than the `sax` module used by `trumpet`.
* `cornet` works as a handler for [`fb55/htmlparser2`](https://github.com/fb55/node-htmlparser), which is probably the fastest HTML parser currently available for node. And it's much less strict than the `sax` module used by `trumpet`.
* By using the great [`MatthewMueller/cheerio`](https://github.com/MatthewMueller/cheerio) module, you can do everything with your document that would be possible with jQuery.
_Please note that callbacks are fired as soon as an element was retrieved. That means that no content past the element will be available, so cheerio won't find anything, and, as the element is at this time the last child of it's parent, selectors like `:nth-last-child` won't work as expected._
##Install
npm install cornet
##Example

@@ -31,5 +37,37 @@

$(elem).find("h3").each(function(i){
console.log("repo %d: %s", i, $(this).text());
console.log("repo %d: %s", i + 1, $(this).text().trim());
});
});
```
//does the same
var i = 0;
cornet.select(".repo_list h3", function(elem){
console.log("repo %d: %s", ++i, $(elem).text().trim());
});
//sometimes, you only want to get a single element
var onTitle = cornet.select("title", function(title){
console.log("Page title:", $(title).text().trim());
cornet.removeLister("element", onTitle);
});
```
##API
####`cornet(options)`
The constructor. `options` are the same you can pass to [`fb55/DomHandler`](https://github.com/fb55/DomHandler).
It's an `EventEmitter` that emits two events:
* `element` is emitted whenever an element was added to the DOM.
* `dom` is emitted when the DOM is complete.
####`cornet#select(selector | fn, cb)`
Calls the callback when the selector is matched or a passed function returns `true` (or any value that evaluates to true).
Internally, listenes for any `element` event and checks then if the selector is matched.
Returns the listening function, so you can remove it afterwards (as shown in the example above).
####`cornet#remove(selector | fn)`
Removes all elements that match the selector. Also returns the listener.