all da files
This commit is contained in:
149
node_modules/cheerio/lib/api/attributes.js
generated
vendored
Normal file
149
node_modules/cheerio/lib/api/attributes.js
generated
vendored
Normal file
@@ -0,0 +1,149 @@
|
||||
var _ = require('underscore'),
|
||||
utils = require('../utils'),
|
||||
isTag = utils.isTag,
|
||||
decode = utils.decode,
|
||||
encode = utils.encode,
|
||||
rspace = /\s+/,
|
||||
|
||||
// Attributes that are booleans
|
||||
rboolean = /^(?:autofocus|autoplay|async|checked|controls|defer|disabled|hidden|loop|multiple|open|readonly|required|scoped|selected)$/i;
|
||||
|
||||
|
||||
var setAttr = function(el, name, value) {
|
||||
if (typeof name === 'object') return _.extend(el.attribs, name);
|
||||
|
||||
if (value === null) {
|
||||
removeAttribute(el, name);
|
||||
} else {
|
||||
el.attribs[name] = encode(value);
|
||||
}
|
||||
|
||||
return el.attribs;
|
||||
};
|
||||
|
||||
var attr = exports.attr = function(name, value) {
|
||||
var elem = this[0];
|
||||
|
||||
if (!elem || !isTag(elem))
|
||||
return undefined;
|
||||
|
||||
if (!elem.attribs) {
|
||||
elem.attribs = {};
|
||||
}
|
||||
|
||||
// Return the entire attribs object if no attribute specified
|
||||
if (!name) {
|
||||
for (var a in elem.attribs) {
|
||||
elem.attribs[a] = decode(elem.attribs[a]);
|
||||
}
|
||||
return elem.attribs;
|
||||
}
|
||||
|
||||
// Set the value (with attr map support)
|
||||
if (typeof name === 'object' || value !== undefined) {
|
||||
this.each(function(i, el) {
|
||||
el.attribs = setAttr(el, name, value);
|
||||
});
|
||||
return this;
|
||||
} else if (Object.hasOwnProperty.call(elem.attribs, name)) {
|
||||
// Get the (decoded) attribute
|
||||
return decode(elem.attribs[name]);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Remove an attribute
|
||||
*/
|
||||
|
||||
var removeAttribute = function(elem, name) {
|
||||
if (!isTag(elem.type) || !elem.attribs || !Object.hasOwnProperty.call(elem.attribs, name))
|
||||
return;
|
||||
|
||||
if (rboolean.test(elem.attribs[name]))
|
||||
elem.attribs[name] = false;
|
||||
else
|
||||
delete elem.attribs[name];
|
||||
};
|
||||
|
||||
|
||||
var removeAttr = exports.removeAttr = function(name) {
|
||||
this.each(function(i, elem) {
|
||||
removeAttribute(elem, name);
|
||||
});
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
var hasClass = exports.hasClass = function(className) {
|
||||
return _.any(this, function(elem) {
|
||||
var attrs = elem.attribs;
|
||||
return attrs && _.contains((attrs['class'] || '').split(rspace), className);
|
||||
});
|
||||
};
|
||||
|
||||
var addClass = exports.addClass = function(value) {
|
||||
// Support functions
|
||||
if (_.isFunction(value)) {
|
||||
this.each(function(i) {
|
||||
var className = this.attr('class') || '';
|
||||
this.addClass(value.call(this, i, className));
|
||||
});
|
||||
}
|
||||
|
||||
// Return if no value or not a string or function
|
||||
if (!value || !_.isString(value)) return this;
|
||||
|
||||
var classNames = value.split(rspace),
|
||||
numElements = this.length,
|
||||
numClasses,
|
||||
setClass,
|
||||
$elem;
|
||||
|
||||
|
||||
for (var i = 0; i < numElements; i++) {
|
||||
$elem = this.make(this[i]);
|
||||
// If selected element isnt a tag, move on
|
||||
if (!isTag(this[i])) continue;
|
||||
|
||||
// If we don't already have classes
|
||||
if (!$elem.attr('class')) {
|
||||
$elem.attr('class', classNames.join(' ').trim());
|
||||
} else {
|
||||
setClass = ' ' + $elem.attr('class') + ' ';
|
||||
numClasses = classNames.length;
|
||||
|
||||
// Check if class already exists
|
||||
for (var j = 0; j < numClasses; j++) {
|
||||
if (!~setClass.indexOf(' ' + classNames[j] + ' '))
|
||||
setClass += classNames[j] + ' ';
|
||||
}
|
||||
|
||||
$elem.attr('class', setClass.trim());
|
||||
}
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
var removeClass = exports.removeClass = function(value) {
|
||||
var split = function(className) {
|
||||
return className ? className.trim().split(rspace) : [];
|
||||
};
|
||||
|
||||
var classes = split(value);
|
||||
|
||||
// Handle if value is a function
|
||||
if (_.isFunction(value)) {
|
||||
return this.each(function(i, el) {
|
||||
this.removeClass(value.call(this, i, el.attribs['class'] || ''));
|
||||
});
|
||||
}
|
||||
|
||||
return this.each(function(i, el) {
|
||||
if (!isTag(el)) return;
|
||||
el.attribs['class'] = (!value) ? '' : _.reject(
|
||||
split(el.attribs['class']),
|
||||
function(name) { return _.contains(classes, name); }
|
||||
).join(' ');
|
||||
});
|
||||
};
|
192
node_modules/cheerio/lib/api/manipulation.js
generated
vendored
Normal file
192
node_modules/cheerio/lib/api/manipulation.js
generated
vendored
Normal file
@@ -0,0 +1,192 @@
|
||||
var _ = require('underscore'),
|
||||
parse = require('../parse'),
|
||||
$ = require('../static'),
|
||||
updateDOM = parse.update,
|
||||
evaluate = parse.evaluate,
|
||||
encode = require('../utils').encode,
|
||||
slice = Array.prototype.slice;
|
||||
|
||||
/*
|
||||
Creates an array of cheerio objects,
|
||||
parsing strings if necessary
|
||||
*/
|
||||
var makeCheerioArray = function(elems) {
|
||||
return _.reduce(elems, function(dom, elem) {
|
||||
return dom.concat(elem.cheerio ? elem.toArray() : evaluate(elem));
|
||||
}, []);
|
||||
};
|
||||
|
||||
var _insert = function(concatenator) {
|
||||
return function() {
|
||||
var elems = slice.call(arguments),
|
||||
dom = makeCheerioArray(elems);
|
||||
|
||||
return this.each(function(i, el) {
|
||||
if (_.isFunction(elems[0])) return el; // not yet supported
|
||||
updateDOM(concatenator(dom, el.children || (el.children = [])), el);
|
||||
});
|
||||
};
|
||||
};
|
||||
|
||||
var append = exports.append = _insert(function(dom, children) {
|
||||
return children.concat(dom);
|
||||
});
|
||||
|
||||
var prepend = exports.prepend = _insert(function(dom, children) {
|
||||
return dom.concat(children);
|
||||
});
|
||||
|
||||
var after = exports.after = function() {
|
||||
var elems = slice.call(arguments),
|
||||
dom = makeCheerioArray(elems);
|
||||
|
||||
this.each(function(i, el) {
|
||||
var siblings = el.parent.children,
|
||||
index = siblings.indexOf(el);
|
||||
|
||||
// If not found, move on
|
||||
if (!~index) return;
|
||||
|
||||
// Add element after `this` element
|
||||
siblings.splice.apply(siblings, [++index, 0].concat(dom));
|
||||
|
||||
// Update next, prev, and parent pointers
|
||||
updateDOM(siblings, el.parent);
|
||||
el.parent.children = siblings;
|
||||
|
||||
});
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
var before = exports.before = function() {
|
||||
var elems = slice.call(arguments),
|
||||
dom = makeCheerioArray(elems);
|
||||
|
||||
this.each(function(i, el) {
|
||||
var siblings = el.parent.children,
|
||||
index = siblings.indexOf(el);
|
||||
|
||||
// If not found, move on
|
||||
if (!~index) return;
|
||||
|
||||
// Add element before `el` element
|
||||
siblings.splice.apply(siblings, [index, 0].concat(dom));
|
||||
|
||||
// Update next, prev, and parent pointers
|
||||
updateDOM(siblings, el.parent);
|
||||
el.parent.children = siblings;
|
||||
|
||||
});
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/*
|
||||
remove([selector])
|
||||
*/
|
||||
var remove = exports.remove = function(selector) {
|
||||
var elems = this;
|
||||
|
||||
// Filter if we have selector
|
||||
if (selector)
|
||||
elems = elems.filter(selector);
|
||||
|
||||
elems.each(function(i, el) {
|
||||
var siblings = el.parent.children,
|
||||
index = siblings.indexOf(el);
|
||||
|
||||
if (!~index) return;
|
||||
|
||||
siblings.splice(index, 1);
|
||||
|
||||
// Update next, prev, and parent pointers
|
||||
updateDOM(siblings, el.parent);
|
||||
el.parent.children = siblings;
|
||||
});
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
var replaceWith = exports.replaceWith = function(content) {
|
||||
content = content.cheerio ? content.toArray() : evaluate(content);
|
||||
|
||||
this.each(function(i, el) {
|
||||
var siblings = el.parent.children,
|
||||
index = siblings.indexOf(el);
|
||||
|
||||
if (!~index) return;
|
||||
|
||||
siblings.splice.apply(siblings, [index, 1].concat(content));
|
||||
|
||||
updateDOM(siblings, el.parent);
|
||||
el.parent.children = siblings;
|
||||
});
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
var empty = exports.empty = function() {
|
||||
this.each(function(i, el) {
|
||||
el.children = [];
|
||||
});
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Set/Get the HTML
|
||||
*/
|
||||
var html = exports.html = function(str) {
|
||||
if (str === undefined) {
|
||||
if (!this[0] || !this[0].children) return null;
|
||||
return $.html(this[0].children);
|
||||
}
|
||||
|
||||
str = str.cheerio ? str.toArray() : evaluate(str);
|
||||
|
||||
this.each(function(i, el) {
|
||||
el.children = str;
|
||||
updateDOM(el.children, el);
|
||||
});
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
var toString = exports.toString = function() {
|
||||
return $.html(this);
|
||||
};
|
||||
|
||||
var text = exports.text = function(str) {
|
||||
// If `str` blank or an object
|
||||
if (!str || typeof str === 'object') {
|
||||
return $.text(this);
|
||||
} else if (_.isFunction(str)) {
|
||||
// Function support
|
||||
return this.each(function(i, el) {
|
||||
return this.text(str.call(el, i, this.text()));
|
||||
});
|
||||
}
|
||||
|
||||
var elem = {
|
||||
data: encode(str),
|
||||
type: 'text',
|
||||
parent: null,
|
||||
prev: null,
|
||||
next: null,
|
||||
children: []
|
||||
};
|
||||
|
||||
// Append text node to each selected elements
|
||||
this.each(function(i, el) {
|
||||
el.children = elem;
|
||||
updateDOM(el.children, el);
|
||||
});
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
var clone = exports.clone = function() {
|
||||
// Turn it into HTML, then recreate it,
|
||||
// Seems to be the easiest way to reconnect everything correctly
|
||||
return this.constructor($.html(this));
|
||||
};
|
116
node_modules/cheerio/lib/api/traversing.js
generated
vendored
Normal file
116
node_modules/cheerio/lib/api/traversing.js
generated
vendored
Normal file
@@ -0,0 +1,116 @@
|
||||
var _ = require('underscore'),
|
||||
select = require('cheerio-select'),
|
||||
utils = require('../utils'),
|
||||
isTag = utils.isTag;
|
||||
|
||||
var find = exports.find = function(selector) {
|
||||
if (!selector) return this;
|
||||
try {
|
||||
var elem = select(selector, [].slice.call(this.children()));
|
||||
return this.make(elem);
|
||||
} catch(e) {
|
||||
return this.make([]);
|
||||
}
|
||||
};
|
||||
|
||||
var parent = exports.parent = function(elem) {
|
||||
if (this[0] && this[0].parent)
|
||||
return this.make(this[0].parent);
|
||||
else
|
||||
return this;
|
||||
};
|
||||
|
||||
var next = exports.next = function(elem) {
|
||||
if (!this[0]) return this;
|
||||
|
||||
var nextSibling = this[0].next;
|
||||
while (nextSibling) {
|
||||
if (isTag(nextSibling)) return this.make(nextSibling);
|
||||
nextSibling = nextSibling.next;
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
var prev = exports.prev = function(elem) {
|
||||
if (!this[0]) return this;
|
||||
|
||||
var prevSibling = this[0].prev;
|
||||
while (prevSibling) {
|
||||
if (isTag(prevSibling)) return this.make(prevSibling);
|
||||
prevSibling = prevSibling.prev;
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
||||
var siblings = exports.siblings = function(elem) {
|
||||
if (!this[0]) return this;
|
||||
var self = this,
|
||||
siblings = (this.parent()) ? this.parent().children()
|
||||
: this.siblingsAndMe();
|
||||
|
||||
siblings = _.filter(siblings, function(elem) {
|
||||
return (elem !== self[0] && isTag(elem));
|
||||
});
|
||||
|
||||
return this.make(siblings);
|
||||
};
|
||||
|
||||
var children = exports.children = function(selector) {
|
||||
|
||||
var elems = _.reduce(this, function(memo, elem) {
|
||||
return memo.concat(_.filter(elem.children, isTag));
|
||||
}, []);
|
||||
|
||||
if (selector === undefined) return this.make(elems);
|
||||
else if (_.isNumber(selector)) return this.make(elems[selector]);
|
||||
|
||||
return this.make(elems).filter(selector);
|
||||
};
|
||||
|
||||
var each = exports.each = function(fn) {
|
||||
var length = this.length,
|
||||
el, i;
|
||||
|
||||
for (i = 0; i < length; ++i) {
|
||||
el = this[i];
|
||||
if (fn.call(this.make(el), i, el) === false) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
var map = exports.map = function(fn) {
|
||||
return _.map(this, function(el, i) {
|
||||
return fn.call(this.make(el), i, el);
|
||||
}, this);
|
||||
};
|
||||
|
||||
var filter = exports.filter = function(match) {
|
||||
var make = _.bind(this.make, this);
|
||||
return make(_.filter(this, _.isString(match) ?
|
||||
function(el) { return select(match, el)[0] === el; }
|
||||
: function(el, i) { return match.call(make(el), i, el); }
|
||||
));
|
||||
};
|
||||
|
||||
var first = exports.first = function() {
|
||||
return this[0] ? this.make(this[0]) : this;
|
||||
};
|
||||
|
||||
var last = exports.last = function() {
|
||||
return this[0] ? this.make(this[this.length - 1]) : this;
|
||||
};
|
||||
|
||||
// Reduce the set of matched elements to the one at the specified index.
|
||||
var eq = exports.eq = function(i) {
|
||||
i = +i;
|
||||
if (i < 0) i = this.length + i;
|
||||
return this[i] ? this.make(this[i]) : this.make([]);
|
||||
};
|
||||
|
||||
var slice = exports.slice = function() {
|
||||
return this.make([].slice.apply(this, arguments));
|
||||
};
|
143
node_modules/cheerio/lib/cheerio.js
generated
vendored
Normal file
143
node_modules/cheerio/lib/cheerio.js
generated
vendored
Normal file
@@ -0,0 +1,143 @@
|
||||
/*
|
||||
Module dependencies
|
||||
*/
|
||||
|
||||
var path = require('path'),
|
||||
select = require('cheerio-select'),
|
||||
parse = require('./parse'),
|
||||
evaluate = parse.evaluate,
|
||||
updateDOM = parse.update,
|
||||
_ = require('underscore');
|
||||
|
||||
/*
|
||||
* The API
|
||||
*/
|
||||
|
||||
var api = ['attributes', 'traversing', 'manipulation'];
|
||||
|
||||
/*
|
||||
* A simple way to check for HTML strings or ID strings
|
||||
*/
|
||||
|
||||
var quickExpr = /^(?:[^#<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/;
|
||||
|
||||
/**
|
||||
* Static Methods
|
||||
*/
|
||||
|
||||
var $ = require('./static');
|
||||
|
||||
/*
|
||||
* Instance of cheerio
|
||||
*/
|
||||
|
||||
var Cheerio = module.exports = function(selector, context, root) {
|
||||
if (!(this instanceof Cheerio)) return new Cheerio(selector, context, root);
|
||||
|
||||
// $(), $(null), $(undefined), $(false)
|
||||
if (!selector) return this;
|
||||
|
||||
if (root) {
|
||||
if (typeof root === 'string') root = parse(root);
|
||||
this._root = this.make(root, this);
|
||||
}
|
||||
|
||||
// $($)
|
||||
if (selector.cheerio) return selector;
|
||||
|
||||
// $(dom)
|
||||
if (selector.name || Array.isArray(selector))
|
||||
return this.make(selector, this);
|
||||
|
||||
// $(<html>)
|
||||
if (typeof selector === 'string' && isHtml(selector)) {
|
||||
return this.make(parse(selector).children);
|
||||
}
|
||||
|
||||
// If we don't have a context, maybe we have a root, from loading
|
||||
if (!context) {
|
||||
context = this._root;
|
||||
} else if (typeof context === 'string') {
|
||||
if (isHtml(context)) {
|
||||
// $('li', '<ul>...</ul>')
|
||||
context = parse(context);
|
||||
context = this.make(context, this);
|
||||
} else {
|
||||
// $('li', 'ul')
|
||||
selector = [context, selector].join(' ');
|
||||
context = this._root;
|
||||
}
|
||||
}
|
||||
|
||||
// If we still don't have a context, return
|
||||
if (!context) return this;
|
||||
|
||||
// #id, .class, tag
|
||||
return context.parent().find(selector);
|
||||
};
|
||||
|
||||
/**
|
||||
* Inherit from `static`
|
||||
*/
|
||||
|
||||
Cheerio.__proto__ = require('./static');
|
||||
|
||||
/*
|
||||
* Set a signature of the object
|
||||
*/
|
||||
|
||||
Cheerio.prototype.cheerio = '[cheerio object]';
|
||||
|
||||
/*
|
||||
* Cheerio default options
|
||||
*/
|
||||
|
||||
Cheerio.prototype.options = {
|
||||
ignoreWhitespace: false,
|
||||
xmlMode: false,
|
||||
lowerCaseTags: false
|
||||
};
|
||||
|
||||
/*
|
||||
* Make cheerio an array-like object
|
||||
*/
|
||||
|
||||
Cheerio.prototype.length = 0;
|
||||
Cheerio.prototype.sort = [].splice;
|
||||
|
||||
/*
|
||||
* Check if string is HTML
|
||||
*/
|
||||
var isHtml = function(str) {
|
||||
// Faster than running regex, if str starts with `<` and ends with `>`, assume it's HTML
|
||||
if (str.charAt(0) === '<' && str.charAt(str.length - 1) === '>' && str.length >= 3) return true;
|
||||
|
||||
// Run the regex
|
||||
var match = quickExpr.exec(str);
|
||||
return !!(match && match[1]);
|
||||
};
|
||||
|
||||
/*
|
||||
* Make a cheerio object
|
||||
*/
|
||||
|
||||
Cheerio.prototype.make = function(dom, context) {
|
||||
if (dom.cheerio) return dom;
|
||||
dom = (Array.isArray(dom)) ? dom : [dom];
|
||||
return _.extend(context || new Cheerio(), dom, { length: dom.length });
|
||||
};
|
||||
|
||||
/**
|
||||
* Turn a cheerio object into an array
|
||||
*/
|
||||
|
||||
Cheerio.prototype.toArray = function() {
|
||||
return [].slice.call(this, 0);
|
||||
};
|
||||
|
||||
/**
|
||||
* Plug in the API
|
||||
*/
|
||||
api.forEach(function(mod) {
|
||||
_.extend(Cheerio.prototype, require('./api/' + mod));
|
||||
});
|
97
node_modules/cheerio/lib/parse.js
generated
vendored
Normal file
97
node_modules/cheerio/lib/parse.js
generated
vendored
Normal file
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
Module Dependencies
|
||||
*/
|
||||
var htmlparser = require('htmlparser2'),
|
||||
_ = require('underscore'),
|
||||
isTag = require('./utils').isTag;
|
||||
|
||||
/*
|
||||
Parser
|
||||
*/
|
||||
exports = module.exports = function(content, options) {
|
||||
var dom = evaluate(content, options);
|
||||
|
||||
// Generic root element
|
||||
var root = {
|
||||
type: 'root',
|
||||
name: 'root',
|
||||
parent: null,
|
||||
prev: null,
|
||||
next: null,
|
||||
children: []
|
||||
};
|
||||
|
||||
// Update the dom using the root
|
||||
update(dom, root);
|
||||
|
||||
return root;
|
||||
};
|
||||
|
||||
var evaluate = exports.evaluate = function(content, options) {
|
||||
// options = options || $.fn.options;
|
||||
|
||||
var handler = new htmlparser.DomHandler(options),
|
||||
parser = new htmlparser.Parser(handler, options);
|
||||
|
||||
parser.write(content);
|
||||
parser.done();
|
||||
|
||||
return connect(handler.dom);
|
||||
};
|
||||
|
||||
var connect = exports.connect = function(dom, parent) {
|
||||
parent = parent || null;
|
||||
|
||||
var prevElem = null;
|
||||
|
||||
_.each(dom, function(elem) {
|
||||
// If tag and no attributes, add empty object
|
||||
if (isTag(elem.type) && elem.attribs === undefined)
|
||||
elem.attribs = {};
|
||||
|
||||
// Set parent
|
||||
elem.parent = parent;
|
||||
|
||||
// Previous Sibling
|
||||
elem.prev = prevElem;
|
||||
|
||||
// Next sibling
|
||||
elem.next = null;
|
||||
if (prevElem) prevElem.next = elem;
|
||||
|
||||
// Run through the children
|
||||
if (elem.children)
|
||||
connect(elem.children, elem);
|
||||
else if (isTag(elem.type))
|
||||
elem.children = [];
|
||||
|
||||
// Get ready for next element
|
||||
prevElem = elem;
|
||||
});
|
||||
|
||||
return dom;
|
||||
};
|
||||
|
||||
/*
|
||||
Update the dom structure, for one changed layer
|
||||
|
||||
* Much faster than reconnecting
|
||||
*/
|
||||
var update = exports.update = function(arr, parent) {
|
||||
// normalize
|
||||
if (!Array.isArray(arr)) arr = [arr];
|
||||
|
||||
// Update neighbors
|
||||
for (var i = 0; i < arr.length; i++) {
|
||||
arr[i].prev = arr[i - 1] || null;
|
||||
arr[i].next = arr[i + 1] || null;
|
||||
arr[i].parent = parent || null;
|
||||
}
|
||||
|
||||
// Update parent
|
||||
parent.children = arr;
|
||||
|
||||
return parent;
|
||||
};
|
||||
|
||||
// module.exports = $.extend(exports);
|
121
node_modules/cheerio/lib/render.js
generated
vendored
Normal file
121
node_modules/cheerio/lib/render.js
generated
vendored
Normal file
@@ -0,0 +1,121 @@
|
||||
/*
|
||||
Module dependencies
|
||||
*/
|
||||
var _ = require('underscore');
|
||||
|
||||
/*
|
||||
Boolean Attributes
|
||||
*/
|
||||
var rboolean = /^(?:autofocus|autoplay|async|checked|controls|defer|disabled|hidden|loop|multiple|open|readonly|required|scoped|selected)$/i;
|
||||
|
||||
/*
|
||||
Format attributes
|
||||
*/
|
||||
var formatAttrs = function(attributes) {
|
||||
if (!attributes) return '';
|
||||
|
||||
var output = [],
|
||||
value;
|
||||
|
||||
// Loop through the attributes
|
||||
for (var key in attributes) {
|
||||
value = attributes[key];
|
||||
if (!value && (rboolean.test(key) || key === '/')) {
|
||||
output.push(key);
|
||||
} else {
|
||||
output.push(key + '="' + value + '"');
|
||||
}
|
||||
}
|
||||
|
||||
return output.join(' ');
|
||||
};
|
||||
|
||||
/*
|
||||
Self-enclosing tags (stolen from node-htmlparser)
|
||||
*/
|
||||
var singleTag = {
|
||||
area: 1,
|
||||
base: 1,
|
||||
basefont: 1,
|
||||
br: 1,
|
||||
col: 1,
|
||||
frame: 1,
|
||||
hr: 1,
|
||||
img: 1,
|
||||
input: 1,
|
||||
isindex: 1,
|
||||
link: 1,
|
||||
meta: 1,
|
||||
param: 1,
|
||||
embed: 1,
|
||||
include: 1,
|
||||
'yield': 1
|
||||
};
|
||||
|
||||
/*
|
||||
Tag types from htmlparser
|
||||
*/
|
||||
var tagType = {
|
||||
tag: 1,
|
||||
script: 1,
|
||||
link: 1,
|
||||
style: 1,
|
||||
template: 1
|
||||
};
|
||||
|
||||
var render = module.exports = function(dom, opts) {
|
||||
if (!Array.isArray(dom) && !dom.cheerio) dom = [dom];
|
||||
opts = opts || {};
|
||||
|
||||
var output = [],
|
||||
xmlMode = opts.xmlMode || false,
|
||||
ignoreWhitespace = opts.ignoreWhitespace || false;
|
||||
|
||||
_.each(dom, function(elem) {
|
||||
var pushVal;
|
||||
|
||||
if (tagType[elem.type])
|
||||
pushVal = renderTag(elem);
|
||||
else if (elem.type === 'directive')
|
||||
pushVal = renderDirective(elem);
|
||||
else if (elem.type === 'comment')
|
||||
pushVal = renderComment(elem);
|
||||
else
|
||||
pushVal = renderText(elem);
|
||||
|
||||
// Push rendered DOM node
|
||||
output.push(pushVal);
|
||||
|
||||
if (elem.children)
|
||||
output.push(render(elem.children, opts));
|
||||
|
||||
if ((!singleTag[elem.name] || xmlMode) && tagType[elem.type])
|
||||
output.push('</' + elem.name + '>');
|
||||
});
|
||||
|
||||
return output.join('');
|
||||
};
|
||||
|
||||
var renderTag = function(elem) {
|
||||
var tag = '<' + elem.name;
|
||||
|
||||
if (elem.attribs && _.size(elem.attribs)) {
|
||||
tag += ' ' + formatAttrs(elem.attribs);
|
||||
}
|
||||
|
||||
return tag + '>';
|
||||
};
|
||||
|
||||
var renderDirective = function(elem) {
|
||||
return '<' + elem.data + '>';
|
||||
};
|
||||
|
||||
var renderText = function(elem) {
|
||||
return elem.data;
|
||||
};
|
||||
|
||||
var renderComment = function(elem) {
|
||||
return '<!--' + elem.data + '-->';
|
||||
};
|
||||
|
||||
// module.exports = $.extend(exports);
|
95
node_modules/cheerio/lib/static.js
generated
vendored
Normal file
95
node_modules/cheerio/lib/static.js
generated
vendored
Normal file
@@ -0,0 +1,95 @@
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
|
||||
var select = require('cheerio-select'),
|
||||
parse = require('./parse'),
|
||||
render = require('./render'),
|
||||
decode = require('./utils').decode;
|
||||
|
||||
/**
|
||||
* $.load(str)
|
||||
*/
|
||||
|
||||
var load = exports.load = function(str, options) {
|
||||
var Cheerio = require('./cheerio'),
|
||||
root = parse(str, options);
|
||||
|
||||
var initialize = function(selector, context, r) {
|
||||
return new Cheerio(selector, context, r || root);
|
||||
};
|
||||
|
||||
// Add in the static methods
|
||||
initialize.__proto__ = exports;
|
||||
|
||||
// Add in the root
|
||||
initialize._root = root;
|
||||
|
||||
return initialize;
|
||||
};
|
||||
|
||||
/**
|
||||
* $.html([selector | dom])
|
||||
*/
|
||||
|
||||
var html = exports.html = function(dom) {
|
||||
if (dom) {
|
||||
dom = (typeof dom === 'string') ? select(dom, this._root) : dom;
|
||||
return render(dom);
|
||||
} else if (this._root && this._root.children) {
|
||||
return render(this._root.children);
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* $.text(dom)
|
||||
*/
|
||||
|
||||
var text = exports.text = function(elems) {
|
||||
if (!elems) return '';
|
||||
|
||||
var ret = '',
|
||||
len = elems.length,
|
||||
elem;
|
||||
|
||||
for (var i = 0; i < len; i ++) {
|
||||
elem = elems[i];
|
||||
if (elem.type === 'text') ret += decode(elem.data);
|
||||
else if (elem.children && elem.type !== 'comment') {
|
||||
ret += text(elem.children);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
};
|
||||
|
||||
/**
|
||||
* $.root()
|
||||
*/
|
||||
var root = exports.root = function() {
|
||||
return this(this._root);
|
||||
};
|
||||
|
||||
/**
|
||||
* $.contains()
|
||||
*/
|
||||
var contains = exports.contains = function(container, contained) {
|
||||
|
||||
// According to the jQuery API, an element does not "contain" itself
|
||||
if (contained === container) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Step up the descendents, stopping when the root element is reached
|
||||
// (signaled by `.parent` returning a reference to the same object)
|
||||
while (contained && contained !== contained.parent) {
|
||||
contained = contained.parent;
|
||||
if (contained === container) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
30
node_modules/cheerio/lib/utils.js
generated
vendored
Normal file
30
node_modules/cheerio/lib/utils.js
generated
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
/**
|
||||
* Module Dependencies
|
||||
*/
|
||||
var entities = require('entities');
|
||||
|
||||
/**
|
||||
* HTML Tags
|
||||
*/
|
||||
|
||||
var tags = { tag: true, script: true, style: true };
|
||||
|
||||
/**
|
||||
* Check if the DOM element is a tag
|
||||
*
|
||||
* isTag(type) includes <script> and <style> tags
|
||||
*/
|
||||
|
||||
exports.isTag = function(type) {
|
||||
if (type.type) type = type.type;
|
||||
return tags[type] || false;
|
||||
};
|
||||
|
||||
/**
|
||||
* Expose encode and decode methods from FB55's node-entities library
|
||||
*
|
||||
* 0 = XML, 1 = HTML4 and 2 = HTML5
|
||||
*/
|
||||
|
||||
exports.encode = function(str) { return entities.encode(String(str), 0); };
|
||||
exports.decode = function(str) { return entities.decode(str, 2); };
|
Reference in New Issue
Block a user