Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

htmldom

Package Overview
Dependencies
Maintainers
1
Versions
66
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

htmldom - npm Package Compare versions

Comparing version 0.1.7 to 0.1.8

selector/private.js

2

package.json
{
"name": "htmldom",
"version": "0.1.7",
"version": "0.1.8",
"description": "Simplified html handle in nodejs",

@@ -5,0 +5,0 @@ "main": "htmldom.js",

@@ -172,3 +172,5 @@ # htmldom — Simplified html handle in nodejs

* find(selector)
The api is deprecated. The ``$`` is faster then ``find``.
```
$('div').find('.item > a')
```
* filter(selector)

@@ -178,3 +180,3 @@ ```

$('').filter(function(index) {
return $(this[index]).attr('data-id') == 1;
return $(this[index], this.document).attr('data-id') == 1;
});

@@ -181,0 +183,0 @@ ```

@@ -5,2 +5,3 @@ var css = require('./css');

var style = require('./style');
var _private = require('./private');

@@ -16,3 +17,3 @@ function $(selector, doc) {

if (util.isString(selector)) {
var result = this._oneByOne(css.split(selector));
var result = this.oneByOne(css.split(selector));

@@ -38,71 +39,12 @@ for (i = 0; i < result.length; i++) {

$.prototype._oneByOne = function(selector) {
var result = this._search(selector.shift());
_private($.prototype);
while (selector.length) {
var item = selector.shift();
switch (item.operator) {
case '>':
result = this._matchDirectParent(item, result);
break;
case '+':
result = this._matchNextWithBrother(item, result);
break;
case '~':
result = this._matchPrecededByBrother(item, result);
break;
default:
result = this._matchParent(item, result);
}
}
return result;
};
$.prototype._search = function(selector) {
var result = [];
function recurse(item) {
if (item.type !== 'tag') return;
if (css.match(item, selector)) {
result.push(item);
}
for (var i = 0, l = item.children.length; i < l; i++) {
recurse(item.children[i]);
}
}
for (var i = 0, l = this.document.length; i < l; i++) {
recurse(this.document[i]);
}
return result;
};
/**
* @example
* $('div a')
* $('div').find('.item > a')
*/
$.prototype._matchParent = function(selector, nodes) {
var result = [];
$.prototype.find = function(selector) {
var ctx = this.newContext();
var result = $(selector, ctx);
for (var i = 0, l = nodes.length; i < l; i++) {
var searchNode = nodes[i]._searchNode || nodes[i];
var match = false;
while (searchNode = searchNode.parent) {
if (css.match(searchNode, selector)) {
result.push(nodes[i]);
nodes[i]._searchNode = searchNode;
match = true;
break;
}
}
if (!match) {
delete nodes[i]._searchNode;
}
}
this.resetContext(ctx);
return result;

@@ -112,111 +54,2 @@ };

/**
* @example
* $('div > a')
*/
$.prototype._matchDirectParent = function(selector, nodes) {
var result = [];
for (var i = 0, l = nodes.length; i < l; i++) {
var searchNode = nodes[i]._searchNode || nodes[i];
searchNode = searchNode.parent;
if (searchNode && css.match(searchNode, selector)) {
result.push(nodes[i]);
nodes[i]._searchNode = searchNode;
} else {
delete nodes[i]._searchNode;
}
}
return result;
};
/**
* @example
* $('div + p')
*/
$.prototype._matchNextWithBrother = function(selector, nodes) {
var result = [];
function preceded(brother, node) {
for (var i = 0; i < brother.length; i++) {
if (brother[i] === node) {
break;
}
}
while (i--) {
var item = brother[i];
if (item.type === 'tag') {
return item;
}
}
}
for (var i = 0, l = nodes.length; i < l; i++) {
var searchNode = nodes[i]._searchNode || nodes[i];
var brother;
if (searchNode.parent) {
brother = searchNode.parent.children;
} else {
brother = this.document;
}
var pre = preceded(brother, searchNode);
if (pre && css.match(pre, selector)) {
result.push(nodes[i]);
nodes[i]._searchNode = pre;
} else {
delete nodes[i]._searchNode;
}
}
return result;
};
/**
* @example
* $('div ~ p')
*/
$.prototype._matchPrecededByBrother = function(selector, nodes) {
var result = [];
function preceded(brother, node) {
for (var i = 0; i < brother.length; i++) {
if (brother[i] === node) {
break;
}
}
return brother.slice(0, i);
}
for (var i = 0, l = nodes.length; i < l; i++) {
var searchNode = nodes[i]._searchNode || nodes[i];
var brother;
if (searchNode.parent) {
brother = searchNode.parent.children;
} else {
brother = this.document;
}
var pres = preceded(brother, searchNode);
if (pres.length) {
for (var j = 0; j < pres.length; j++) {
if (css.match(pres[j], selector)) {
result.push(nodes[i]);
nodes[i]._searchNode = pres[i];
break;
}
}
} else {
delete nodes[i]._searchNode;
}
}
return result;
};
/**
* $('').parent();

@@ -468,11 +301,2 @@ * $('').parent('.cls')

$.prototype._createdom = function(html, callback) {
var HtmlDom = require('../htmldom');
for (var i = 0; i < this.length; i++) {
var htmldom = new HtmlDom(html).dom;
callback(this[i], htmldom);
}
};
/**

@@ -493,3 +317,3 @@ * @example

} else {
this._createdom(content, function(item, children) {
this.createdom(content, function(item, children) {
children.forEach(function(child) {

@@ -509,3 +333,3 @@ child.parent = item;

$.prototype.append = function(content) {
this._createdom(content, function(item, children) {
this.createdom(content, function(item, children) {
children.forEach(function(child) {

@@ -521,6 +345,6 @@ child.parent = item;

/**
* $('')
* $('').prepend('<h2>')
*/
$.prototype.prepend = function(content) {
this._createdom(content, function(item, children) {
this.createdom(content, function(item, children) {
length = children.length;

@@ -527,0 +351,0 @@ while (length--) {

@@ -11,5 +11,5 @@ var fs = require('fs');

it('tag', function() {
assert.equal($('div h3').length, 2);
assert.equal($('div h3').html(), '<a class="link"></a>');
assert.equal($('div a').length, 1);
assert.equal($('div').find('h3').length, 2);
assert.equal($('div').find('h3').html(), '<a class="link"></a>');
assert.equal($('div').find('a').length, 1);
});

@@ -28,3 +28,4 @@

assert.equal($('ul').length, 4);
assert.equal($('ul li').length, 5);
assert.equal($('ul').find('li').length, 5);
assert.equal($('ul').find('ul > li').length, 2);
});

@@ -37,4 +38,5 @@

assert.equal($('div').length, 3);
assert.equal($('div div').length, 2);
assert.equal($('div div div').length, 1);
assert.equal($('div').find('div').length, 2);
assert.equal($('div').find('div div').length, 1);
assert.equal($('div').find('div').find('div').length, 1);
});

@@ -47,5 +49,5 @@

assert.equal($('a').length, 2);
assert.equal($('div a').length, 2);
assert.equal($('div div a').length, 1);
$('div').find('div').append('<a>').append('<a>');
assert.equal($('div').find('div > a').length, 3);
});
});
SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc