| var $ = require("../cheerio"), | ||
| selectors = $.fn.filters; | ||
| selectors["root"] = function(){ | ||
| return [$.root] || []; | ||
| } |
| var cheerio = require("../"), | ||
| should = require("should"); | ||
| describe('$(":root")', function() { | ||
| it('should return the root element', function() { | ||
| var $ = cheerio.load("<div><span>foo</span><span>bar</span></div>"); | ||
| $(":root").append("<div id = 'test'></div>"); | ||
| $.html().should.equal('<div><span>foo</span><span>bar</span></div><div id = "test"></div>'); | ||
| }); | ||
| }); |
+10
-0
@@ -0,1 +1,11 @@ | ||
| 0.6.1 / 2012-02-12 | ||
| ================== | ||
| * Added .first(), .last(), and .clone() commands. | ||
| * Option to parse using whitespace added to `.load`. | ||
| * Many bug fixes to make cheerio more aligned with jQuery. | ||
| * Added $(':root') to select the highest level element. | ||
| Many thanks to the contributors that made this release happen: @ironchefpython and @siddMahen | ||
| 0.6.0 / 2012-02-07 | ||
@@ -2,0 +12,0 @@ ================== |
@@ -150,3 +150,3 @@ var _ = require('underscore'), | ||
| var replaceWith = exports.replaceWith = function(content) { | ||
| var elems = parse.eval(content); | ||
| var elems = (content.cheerio) ? content : parse.eval(content); | ||
@@ -159,3 +159,3 @@ this.each(function() { | ||
| siblings.splice.apply(siblings, [index, 1].concat(elems)); | ||
| siblings.splice.apply(siblings, [index, 1].concat(elems.toArray())); | ||
@@ -177,4 +177,4 @@ updateDOM(siblings, this.parent); | ||
| var html = exports.html = function(str) { | ||
| if(!str || typeof(str) === 'object') { | ||
| if(!this[0] || !this[0].children) return ''; | ||
| if(str === undefined || typeof(str) === 'object') { | ||
| if(!this[0] || !this[0].children) return null; | ||
| return $.html(this[0].children); | ||
@@ -222,2 +222,12 @@ } | ||
| var clone = exports.clone = function() { | ||
| var result = []; | ||
| for (var i = 0; i < this.length; i++) { | ||
| // TODO: This could be more efficient | ||
| result = result.concat(parse.eval($.html(this[i]))); | ||
| } | ||
| return $(result); | ||
| }; | ||
| module.exports = $.fn.extend(exports); |
@@ -16,7 +16,7 @@ var _ = require("underscore"), | ||
| else | ||
| return null; | ||
| return $(); | ||
| }; | ||
| var next = exports.next = function(elem) { | ||
| if(!this[0]) return null; | ||
| if(!this[0]) return $(); | ||
@@ -28,6 +28,7 @@ var nextSibling = this[0].next; | ||
| } | ||
| return $(); | ||
| }; | ||
| var prev = exports.prev = function(elem) { | ||
| if(!this[0]) return null; | ||
| if(!this[0]) return $(); | ||
@@ -39,6 +40,7 @@ var prevSibling = this[0].prev; | ||
| } | ||
| return $(); | ||
| }; | ||
| var siblings = exports.siblings = function(elem) { | ||
| if(!this[0]) return null; | ||
| if(!this[0]) return $(); | ||
@@ -57,3 +59,3 @@ var self = this, | ||
| var children = exports.children = function(selector) { | ||
| if(!this[0] || !this[0].children) return null; | ||
| if(!this[0] || !this[0].children) return $(); | ||
@@ -74,2 +76,10 @@ var children = _.filter(this[0].children, function(elem) { | ||
| var first = exports.first = function() { | ||
| return this[0] ? $(this[0]) : $(); | ||
| }; | ||
| var last = exports.last = function() { | ||
| return this[0] ? $(this[this.length - 1]) : $(); | ||
| }; | ||
| module.exports = $.fn.extend(exports); |
+3
-3
@@ -229,4 +229,4 @@ /* | ||
| var load = exports.load = function(html) { | ||
| var root = parse(html); | ||
| var load = exports.load = function(html, opts) { | ||
| var root = parse(html, opts); | ||
@@ -267,2 +267,2 @@ function fn(selector, context, r) { | ||
| module.exports = $.extend(exports); | ||
| module.exports = $.extend(exports); |
+4
-1
| /* | ||
| Module dependencies | ||
| */ | ||
| var path = require('path'), | ||
| soupselect = require('cheerio-soupselect'), | ||
| filters = soupselect.filters, | ||
| _ = require('underscore'), | ||
@@ -75,2 +77,3 @@ parse = require('./parse'); | ||
| }, | ||
| filters : filters, | ||
| selector : '', | ||
@@ -97,5 +100,5 @@ sort : [].splice, | ||
| */ | ||
| var api = 'core utils attributes traversing manipulation'; | ||
| var api = 'selectors core utils attributes traversing manipulation'; | ||
| api.split(' ').forEach(function(plugin) { | ||
| require('./api/' + plugin); | ||
| }); |
+6
-7
@@ -12,4 +12,4 @@ /* | ||
| */ | ||
| var parser = exports = module.exports = function(content) { | ||
| var dom = eval(content), | ||
| var parser = exports = module.exports = function(content, opts) { | ||
| var dom = eval(content, opts), | ||
| root = { | ||
@@ -29,9 +29,8 @@ type : 'root', | ||
| var eval = exports.eval = function(content) { | ||
| var handler = new htmlparser.DefaultHandler({ | ||
| ignoreWhitespace: true | ||
| }), | ||
| var eval = exports.eval = function(content, opts) { | ||
| opts = opts || { ignoreWhitespace: true }; | ||
| var handler = new htmlparser.DefaultHandler(opts), | ||
| parser = new htmlparser.Parser(handler); | ||
| parser.includeLocation = false; | ||
| parser.parseComplete(content); | ||
@@ -38,0 +37,0 @@ |
+1
-1
@@ -43,3 +43,3 @@ /* | ||
| output = output || []; | ||
| dom = isArray(dom) ? dom : [dom]; | ||
| dom = (isArray(dom) || dom.cheerio) ? dom : [dom]; | ||
@@ -46,0 +46,0 @@ var len = dom.length, |
+1
-1
@@ -6,3 +6,3 @@ { | ||
| "keywords": ["htmlparser", "jquery", "selector", "scraper"], | ||
| "version": "0.6.0", | ||
| "version": "0.6.1", | ||
| "repository": { | ||
@@ -9,0 +9,0 @@ "type": "git", |
+21
-0
@@ -68,2 +68,6 @@ # cheerio | ||
| By default, cheerio's parser will ignore whitespace, you can override this default by setting the second parameter: | ||
| $ = cheerio.load('<ul id = "fruits">...</ul>', { ignoreWhitespace: false }); | ||
| Optionally, you can also load in the HTML by passing the string as the context: | ||
@@ -207,3 +211,14 @@ | ||
| #### .first() | ||
| Will select the first element of a cheerio object | ||
| $('#fruits').children().first().text() | ||
| => Apple | ||
| #### .last() | ||
| Will select the last element of a cheerio object | ||
| $('#fruits').children().last().text() | ||
| => Pear | ||
| ### Manipulation | ||
@@ -336,2 +351,8 @@ Methods for modifying the DOM structure. | ||
| => [ {...}, {...}, {...} ] | ||
| #### .clone() #### | ||
| Clone the cheerio object. | ||
| var moreFruit = $('#fruits').clone() | ||
| ### Utilities | ||
@@ -338,0 +359,0 @@ |
@@ -60,3 +60,3 @@ $ = require('../') | ||
| # console.log $test.children() | ||
| describe '.next', -> | ||
@@ -94,3 +94,46 @@ | ||
| describe '.first', -> | ||
| it '() : should return the first item', -> | ||
| src = $("<span>foo</span><span>bar</span><span>baz</span>") | ||
| elem = src.first() | ||
| elem.length.should.equal 1 | ||
| elem.html().should.equal 'foo' | ||
| it '() : should return an empty object for an empty object', -> | ||
| src = $(); | ||
| first = src.first(); | ||
| first.length.should.equal 0 | ||
| should.not.exist first.html() | ||
| describe '.last', -> | ||
| it '() : should return the last element', -> | ||
| src = $("<span>foo</span><span>bar</span><span>baz</span>"); | ||
| elem = src.last(); | ||
| elem.length.should.equal 1 | ||
| elem.html().should.equal 'baz' | ||
| it '() : should return an empty object for an empty object', -> | ||
| src = $(); | ||
| last = src.last(); | ||
| last.length.should.equal 0 | ||
| should.not.exist last.html() | ||
| describe '.first & .last', -> | ||
| it '() : should return same object if only one object', -> | ||
| src = $("<span>bar</span>"); | ||
| first = src.first(); | ||
| last = src.last(); | ||
| first.html().should.equal last.html() | ||
| first.length.should.equal 1 | ||
| first.html().should.equal 'bar' | ||
| last.length.should.equal 1 | ||
| last.html().should.equal 'bar' | ||
+37
-6
@@ -1,2 +0,2 @@ | ||
| var cheerio = require('../'), | ||
| var $ = require('../'), | ||
| should = require('should'); | ||
@@ -13,11 +13,42 @@ | ||
| it('(html) should return innerHTML of element', function() { | ||
| var html = "<div><span>foo</span><span>bar</span></div>", | ||
| $ = cheerio.load(html), | ||
| span = $('div').children().get(1); | ||
| it('() : should return innerHTML; $.html(obj) should return outerHTML', function() { | ||
| var div = $('div', '<div><span>foo</span><span>bar</span></div>'), | ||
| span = div.children().get(1); | ||
| $(span).html().should.equal('bar'); | ||
| $.html(span).should.equal('<span>bar</span>') | ||
| $.html(span).should.equal('<span>bar</span>'); | ||
| }); | ||
| it('(<obj>) : should accept an object, an array, or a cheerio object', function() { | ||
| var span = $("<span>foo</span>"); | ||
| $.html(span.get(0)).should.equal('<span>foo</span>'); | ||
| $.html(span.get()).should.equal('<span>foo</span>'); | ||
| $.html(span).should.equal('<span>foo</span>'); | ||
| }); | ||
| it('(<value>) : should be able to set to an empty string', function() { | ||
| var elem = $("<span>foo</span>"); | ||
| elem.html(''); | ||
| $.html(elem).should.equal('<span></span>'); | ||
| }); | ||
| it('() : of empty cheerio object should return null', function() { | ||
| should.not.exist($().html()); | ||
| }); | ||
| }); | ||
| describe('.clone', function() { | ||
| it('() : should return a copy', function() { | ||
| var src = $("<div><span>foo</span><span>bar</span><span>baz</span></div>").children(), | ||
| elem = src.clone(); | ||
| elem.length.should.equal(3); | ||
| elem.parent().length.should.equal(0); | ||
| }); | ||
| }); | ||
| }); |
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
AI-detected potential code anomaly
Supply chain riskAI has identified unusual behaviors that may pose a security risk.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
AI-detected potential code anomaly
Supply chain riskAI has identified unusual behaviors that may pose a security risk.
Found 2 instances in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
75381
5.96%27
8%951
5.78%449
4.91%3
-25%5
25%