Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Switched to parsing with ignoreWhitespace set

* Also removed some extra whitespace floating around.
  • Loading branch information...
commit bd00f7e74c16429921ee1862e239aa9cc612a515 1 parent e6a3cb6
@siddMahen siddMahen authored
View
6 Makefile
@@ -1,10 +1,10 @@
build:
- @coffee -o lib/ src/
+ @coffee -o lib/ src/
-test:
+test:
@./node_modules/.bin/mocha
mate:
@mate src/ test/
-.PHONY: test build mate
+.PHONY: test build mate
View
2  index.js
@@ -16,4 +16,4 @@ exports.utils = require(base + 'utils');
/*
Attach other modules on here, this will allow testing to be done in mocha without recompiling
-*/
+*/
View
11 lib/api/attributes.js
@@ -1,17 +1,24 @@
(function() {
var $, addClass, attr, hasClass, rclass, removeAttr, removeClass, rspace, _;
+
_ = require("underscore");
+
$ = require("../cheerio");
+
rclass = /[\n\t\r]/g;
+
rspace = /\s+/;
+
attr = exports.attr = function(name, value) {
return $.access(this, name, value, true, $.attr);
};
+
removeAttr = exports.removeAttr = function(name) {
return this.each(function() {
return $.removeAttr(this, name);
});
};
+
hasClass = exports.hasClass = function(selector) {
var className, elem, _i, _len;
className = " " + selector + " ";
@@ -23,6 +30,7 @@
}
return false;
};
+
addClass = exports.addClass = function(value) {
var $elem, className, classNames, elem, setClass, _i, _j, _len, _len2;
if (_.isFunction(value)) {
@@ -56,6 +64,7 @@
}
return this;
};
+
removeClass = exports.removeClass = function(value) {
var $elem, className, classNames, elem, ret, _i, _j, _len, _len2;
if (_.isFunction(value)) {
@@ -87,5 +96,7 @@
}
return this;
};
+
module.exports = $.fn.extend(exports);
+
}).call(this);
View
13 lib/api/core.js
@@ -1,13 +1,18 @@
(function() {
var $, get, pushStack, siblingsAndMe, size, toArray, underscore;
+
underscore = require('underscore');
+
$ = require('../cheerio');
+
size = exports.size = function() {
return this.length;
};
+
toArray = exports.toArray = function() {
return Array.prototype.slice.call(this, 0);
};
+
get = exports.get = function(num) {
if (num === void 0) {
return this.toArray();
@@ -19,6 +24,7 @@
}
}
};
+
pushStack = exports.pushStack = function(elems, name, selector) {
var ret;
ret = this.constructor();
@@ -32,12 +38,11 @@
if (name === "find") {
ret.selector = this.selector + (this.selector ? " " : "") + selector;
} else {
- if (name) {
- ret.selector = this.selector + "." + name + "(" + selector + ")";
- }
+ if (name) ret.selector = this.selector + "." + name + "(" + selector + ")";
}
return ret;
};
+
siblingsAndMe = exports.siblingsAndMe = function() {
var element, raw, siblings;
siblings = [];
@@ -53,5 +58,7 @@
}
return siblings;
};
+
module.exports = $.fn.extend(exports);
+
}).call(this);
View
43 lib/api/manipulation.js
@@ -1,16 +1,19 @@
(function() {
- var $, after, append, before, empty, html, parse, prepend, remove, removeChild, replaceWith, text, _;
- var __slice = Array.prototype.slice;
+ var $, after, append, before, empty, html, parse, prepend, remove, removeChild, replaceWith, text, _,
+ __slice = Array.prototype.slice;
+
_ = require('underscore');
+
$ = require('../cheerio');
+
parse = require('../parse');
+
removeChild = function(parent, elem) {
return $.each(parent.children, function(i, child) {
- if (elem === child) {
- return parent.children.splice(i, 1);
- }
+ if (elem === child) return parent.children.splice(i, 1);
});
};
+
append = exports.append = function() {
var dom, elem, elems, _i, _len;
elems = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
@@ -25,15 +28,14 @@
}
this.each(function() {
if (_.isFunction(elems[0])) {} else {
- if (!this.children) {
- this.children = [];
- }
+ if (!this.children) this.children = [];
this.children = this.children.concat(dom);
return $.updateDOM(this.children, this);
}
});
return this;
};
+
prepend = exports.prepend = function() {
var dom, elem, elems, _i, _len;
elems = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
@@ -48,15 +50,14 @@
}
this.each(function() {
if (_.isFunction(elems[0])) {} else {
- if (!this.children) {
- this.children = [];
- }
+ if (!this.children) this.children = [];
this.children = dom.concat(this.children);
return $.updateDOM(this.children, this);
}
});
return this;
};
+
after = exports.after = function() {
var dom, elem, elems, _i, _len;
elems = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
@@ -73,14 +74,13 @@
var index, siblings;
siblings = this.parent.children;
index = siblings.indexOf(this);
- if (index >= 0) {
- siblings.splice.apply(siblings, [index + 1, 0].concat(dom));
- }
+ if (index >= 0) siblings.splice.apply(siblings, [index + 1, 0].concat(dom));
$.updateDOM(siblings, this.parent);
return this.parent.children = siblings;
});
return this;
};
+
before = exports.before = function() {
var dom, elem, elems, _i, _len;
elems = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
@@ -97,20 +97,17 @@
var index, siblings;
siblings = this.parent.children;
index = siblings.indexOf(this);
- if (index >= 0) {
- siblings.splice.apply(siblings, [index, 0].concat(dom));
- }
+ if (index >= 0) siblings.splice.apply(siblings, [index, 0].concat(dom));
$.updateDOM(siblings, this.parent);
return this.parent.children = siblings;
});
return this;
};
+
remove = exports.remove = function(selector) {
var elems;
elems = this;
- if (selector) {
- elems = this.find(selector);
- }
+ if (selector) elems = this.find(selector);
elems.each(function() {
var index, siblings;
siblings = this.parent.children;
@@ -121,6 +118,7 @@
});
return this;
};
+
replaceWith = exports.replaceWith = function(content) {
var elems;
elems = parse.eval(content);
@@ -133,11 +131,13 @@
return this.parent.children = siblings;
});
};
+
empty = exports.empty = function() {
return this.each(function() {
return this.children = [];
});
};
+
html = exports.html = function(htmlString) {
var htmlElement;
if (typeof htmlString !== "object" && htmlString !== void 0) {
@@ -150,6 +150,7 @@
return $.html(this[0]);
}
};
+
text = exports.text = function(textString) {
var textElement;
if (_.isFunction(textString)) {
@@ -178,5 +179,7 @@
return $.text(this);
}
};
+
module.exports = $.fn.extend(exports);
+
}).call(this);
View
45 lib/api/traversing.js
@@ -1,22 +1,25 @@
(function() {
var $, children, each, find, next, parent, prev, siblings, soupselect, _;
- var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
+
_ = require("underscore");
+
soupselect = require("cheerio-soupselect");
+
$ = require("../cheerio");
+
/*
Stupidly simple traversal
-
+
TODO : Make it more jQuery-like
*/
+
find = exports.find = function(selector) {
var elem;
- if (!selector) {
- return this;
- }
+ if (!selector) return this;
elem = soupselect.select(this.toArray(), selector);
return $(elem);
};
+
parent = exports.parent = function(elem) {
if (this[0] && this[0].parent) {
return $(this[0].parent);
@@ -24,46 +27,43 @@
return null;
}
};
+
next = exports.next = function(elem) {
var nextSibling;
- if (!this[0]) {
- return null;
- }
+ if (!this[0]) return null;
nextSibling = this[0].next;
while (nextSibling) {
- if ($.isTag(nextSibling)) {
- return $(nextSibling);
- }
+ if ($.isTag(nextSibling)) return $(nextSibling);
nextSibling = nextSibling.next;
}
return null;
};
+
prev = exports.prev = function(elem) {
var prevSibling;
- if (!this[0]) {
- return null;
- }
+ if (!this[0]) return null;
prevSibling = this[0].prev;
while (prevSibling) {
- if ($.isTag(prevSibling)) {
- return $(prevSibling);
- }
+ if ($.isTag(prevSibling)) return $(prevSibling);
prevSibling = prevSibling.prev;
}
return null;
};
+
siblings = exports.siblings = function(elem) {
- var sibs;
+ var sibs,
+ _this = this;
if (this.parent()) {
sibs = this.parent().children();
} else {
sibs = this.siblingsAndMe();
}
- siblings = _.filter(sibs, __bind(function(elem) {
- return elem !== this[0] && $.isTag(elem);
- }, this));
+ siblings = _.filter(sibs, function(elem) {
+ return elem !== _this[0] && $.isTag(elem);
+ });
return $(siblings);
};
+
children = exports.children = function(selector) {
if (this[0] && this[0].children) {
children = _.filter(this[0].children, function(elem) {
@@ -85,8 +85,11 @@
return null;
}
};
+
each = exports.each = function(callback, args) {
return $.each(this, callback, args);
};
+
module.exports = $.fn.extend(exports);
+
}).call(this);
View
87 lib/api/utils.js
@@ -1,13 +1,20 @@
(function() {
var $, access, attr, class2type, dom, each, html, inArray, indexOf, isArray, isTag, load, makeArray, merge, parse, push, rboolean, removeAttr, render, tags, text, toString, type, updateDOM, _;
+
_ = require("underscore");
+
$ = require("../cheerio");
+
parse = require("../parse");
+
render = require("../render");
+
class2type = {};
+
_.each("Boolean Number String Function Array Date Regex Object".split(" "), function(name, i) {
return class2type["[object " + name + "]"] = name.toLowerCase();
});
+
/*
Node Types
directive : 10
@@ -17,25 +24,30 @@
text : 3
tag : 1
*/
+
rboolean = /^(?:autofocus|autoplay|async|checked|controls|defer|disabled|hidden|loop|multiple|open|readonly|required|scoped|selected)$/i;
+
toString = Object.prototype.toString;
+
push = Array.prototype.push;
+
indexOf = Array.prototype.indexOf;
+
tags = {
'tag': 1,
'script': 1,
'style': 1
};
+
isTag = exports.isTag = function(type) {
- if (type.type) {
- type = type.type;
- }
+ if (type.type) type = type.type;
if (tags[type]) {
return true;
} else {
return false;
}
};
+
updateDOM = exports.updateDOM = function(arr, parent) {
var elem, i, _len;
arr = $(arr).get();
@@ -45,12 +57,11 @@
arr[i].next = arr[i + 1] || null;
arr[i].parent = parent || null;
}
- if (!parent.children) {
- parent.children = [];
- }
+ if (!parent.children) parent.children = [];
parent.children = arr;
return parent;
};
+
type = exports.type = function(obj) {
if (obj === null) {
return String(obj);
@@ -58,9 +69,11 @@
return class2type[toString.call(obj)] || "object";
}
};
+
isArray = exports.isArray = function(array) {
return _(this).isArray();
};
+
merge = exports.merge = function(first, second) {
var i, j, l;
i = first.length;
@@ -79,6 +92,7 @@
first.length = i;
return first;
};
+
makeArray = exports.makeArray = function(array, results) {
var ret;
ret = results || [];
@@ -92,12 +106,12 @@
}
return ret;
};
+
inArray = exports.inArray = function(elem, array) {
- if (!array) {
- return -1;
- }
+ if (!array) return -1;
return indexOf.call(array, elem);
};
+
each = exports.each = function(object, callback, args) {
var i, isObj, length, name;
length = object.length;
@@ -106,34 +120,27 @@
if (args) {
if (isObj) {
for (name in object) {
- if (callback.apply(object[name], args) === false) {
- break;
- }
+ if (callback.apply(object[name], args) === false) break;
}
} else {
while (i < length) {
- if (callback.apply(object[i++], args) === false) {
- break;
- }
+ if (callback.apply(object[i++], args) === false) break;
}
}
} else {
if (isObj) {
for (name in object) {
- if (callback.call(object[name], name, object[name]) === false) {
- break;
- }
+ if (callback.call(object[name], name, object[name]) === false) break;
}
} else {
while (i < length) {
- if (callback.call(object[i], i, object[i++]) === false) {
- break;
- }
+ if (callback.call(object[i], i, object[i++]) === false) break;
}
}
}
return object;
};
+
access = exports.access = function(elems, key, value, exec, fn, pass) {
var i, k, length;
length = elems.length;
@@ -152,23 +159,14 @@
}
return elems;
}
- if (length) {
- return fn(elems[0], key);
- } else {
- return;
- }
+ return (length ? fn(elems[0], key) : void 0);
};
+
attr = exports.attr = function(elem, name, value, pass) {
type = elem.type;
- if (!elem || !$.isTag(elem)) {
- return;
- }
- if (!elem.attribs) {
- elem.attribs = {};
- }
- if (!name) {
- return elem.attribs;
- }
+ if (!elem || !$.isTag(elem)) return;
+ if (!elem.attribs) elem.attribs = {};
+ if (!name) return elem.attribs;
if (value !== void 0) {
if (value === null) {
return $.removeAttr(elem, name);
@@ -179,6 +177,7 @@
return elem.attribs[name];
}
};
+
removeAttr = exports.removeAttr = function(elem, name) {
if (isTag(elem.type) && elem.attribs) {
if (elem.attribs[name]) {
@@ -190,12 +189,11 @@
}
}
};
+
text = exports.text = function(elems) {
var elem, ret, _i, _len;
ret = "";
- if (!elems) {
- return ret;
- }
+ if (!elems) return ret;
for (_i = 0, _len = elems.length; _i < _len; _i++) {
elem = elems[_i];
if (elem.type === "text") {
@@ -206,6 +204,7 @@
}
return ret;
};
+
load = exports.load = function(html) {
var fn, root;
root = parse(html);
@@ -213,13 +212,12 @@
'root': root
});
fn = function(selector, context, r) {
- if (r) {
- root = parse(r);
- }
+ if (r) root = parse(r);
return $(selector, context, root);
};
return _(fn).extend($);
};
+
html = exports.html = function(dom) {
if (dom !== void 0 && dom.type) {
return render(dom);
@@ -229,16 +227,17 @@
return "";
}
};
+
dom = exports.dom = function(dom) {
if (dom !== void 0) {
- if (dom.type) {
- return dom;
- }
+ if (dom.type) return dom;
} else if (this.root && this.root.children) {
return this.root.children;
} else {
return "";
}
};
+
module.exports = $.extend(exports);
+
}).call(this);
View
24 lib/cheerio.js
@@ -1,9 +1,14 @@
(function() {
var api, cheerio, parse, path, plugin, soupselect, _, _i, _len;
+
path = require("path");
+
soupselect = require("cheerio-soupselect");
+
_ = require("underscore");
+
parse = require("./parse");
+
cheerio = (function() {
var quickExpr, trimLeft, trimRight;
cheerio = function(selector, context, root) {
@@ -17,16 +22,12 @@
constructor: cheerio,
init: function(selector, context, root) {
var elems, match;
- if (!selector) {
- return this;
- }
+ if (!selector) return this;
if (root) {
cheerio.extend({
'root': root
});
- if (_.isString(context)) {
- selector = "" + context + " " + selector;
- }
+ if (_.isString(context)) selector = "" + context + " " + selector;
context = root;
}
if (typeof selector === "string") {
@@ -47,13 +48,11 @@
}
/*
Refactor
- */
+ */
if (!context || context.cheerio) {
return this.constructor(context || root).find(selector);
} else {
- if (_.isString(context)) {
- context = parse(context);
- }
+ if (_.isString(context)) context = parse(context);
return this.constructor(context).find(selector);
}
}
@@ -70,13 +69,18 @@
};
return cheerio;
})();
+
module.exports = cheerio;
+
/*
Plug in the API
*/
+
api = ['core', 'utils', 'attributes', 'traversing', 'manipulation'];
+
for (_i = 0, _len = api.length; _i < _len; _i++) {
plugin = api[_i];
require("./api/" + plugin);
}
+
}).call(this);
View
24 lib/parse.js
@@ -1,9 +1,12 @@
(function() {
var connect, eval, exports, htmlparser, isTag, parser;
+
htmlparser = require("htmlparser2");
+
/*
parser
*/
+
parser = exports = module.exports = function(content) {
var dom, root;
dom = eval(content);
@@ -18,14 +21,18 @@
root.children = connect(dom, root);
return root;
};
+
eval = exports.eval = function(content) {
var handler;
- handler = new htmlparser.DefaultHandler();
+ handler = new htmlparser.DefaultHandler({
+ ignoreWhitespace: true
+ });
parser = new htmlparser.Parser(handler);
parser.includeLocation = false;
parser.parseComplete(content);
return handler.dom;
};
+
isTag = function(type) {
if (type === 'tag' || type === 'script' || type === 'style') {
return true;
@@ -33,18 +40,15 @@
return false;
}
};
+
connect = exports.connect = function(dom, parent) {
var elem, i, lastElem, prev, prevIndex, _len;
- if (parent == null) {
- parent = null;
- }
+ if (parent == null) parent = null;
prevIndex = -1;
lastElem = null;
for (i = 0, _len = dom.length; i < _len; i++) {
elem = dom[i];
- if (isTag(dom[i].type) && dom[i].attribs === void 0) {
- dom[i].attribs = {};
- }
+ if (isTag(dom[i].type) && dom[i].attribs === void 0) dom[i].attribs = {};
dom[i].parent = parent;
prev = dom[prevIndex];
if (prev) {
@@ -53,9 +57,7 @@
dom[i].prev = null;
}
dom[i].next = null;
- if (lastElem) {
- lastElem.next = dom[i];
- }
+ if (lastElem) lastElem.next = dom[i];
if (dom[i].children) {
connect(dom[i].children, dom[i]);
} else if (isTag(dom[i].type)) {
@@ -66,5 +68,7 @@
}
return dom;
};
+
module.exports = exports;
+
}).call(this);
View
57 lib/parser.js
@@ -1,57 +0,0 @@
-(function() {
- var createTree, eval, fs, htmlparser, parse, splice;
- htmlparser = require("htmlparser2");
- fs = require("fs");
- splice = [].splice;
- eval = exports.eval = function(content) {
- var handler, parser;
- handler = new htmlparser.DefaultHandler();
- parser = new htmlparser.Parser(handler);
- parser.includeLocation = false;
- parser.parseComplete(content);
- return handler.dom;
- };
- parse = exports.parse = function(content) {
- var dom, root;
- dom = eval(content);
- root = {
- type: 'root',
- name: 'root',
- parent: null,
- prev: null,
- next: null,
- children: []
- };
- root.children = createTree(dom, root);
- return root;
- };
- createTree = exports.createTree = function(dom, parent) {
- var elem, i, lastElem, prev, prevIndex, _len;
- if (parent == null) {
- parent = null;
- }
- prevIndex = -1;
- lastElem = null;
- for (i = 0, _len = dom.length; i < _len; i++) {
- elem = dom[i];
- dom[i].parent = parent;
- prev = dom[prevIndex];
- if (prev) {
- dom[i].prev = prev;
- } else {
- dom[i].prev = null;
- }
- dom[i].next = null;
- if (lastElem) {
- lastElem.next = dom[i];
- }
- if (dom[i].children) {
- createTree(dom[i].children, dom[i]);
- }
- prevIndex = i;
- lastElem = dom[i];
- }
- return dom;
- };
- module.exports = exports;
-}).call(this);
View
23 lib/render.js
@@ -1,7 +1,10 @@
(function() {
var exports, render, renderComment, renderDirective, renderTag, renderText, singleTag, tagType, utils, _;
+
_ = require("underscore");
+
utils = require("./utils");
+
singleTag = {
area: 1,
base: 1,
@@ -20,6 +23,7 @@
include: 1,
yield: 1
};
+
tagType = {
tag: 1,
script: 1,
@@ -27,14 +31,11 @@
style: 1,
template: 1
};
+
render = exports = module.exports = function(dom, output) {
var elem, str, _i, _len;
- if (output == null) {
- output = [];
- }
- if (!_.isArray(dom)) {
- dom = [dom];
- }
+ if (output == null) output = [];
+ if (!_.isArray(dom)) dom = [dom];
for (_i = 0, _len = dom.length; _i < _len; _i++) {
elem = dom[_i];
str = elem.name;
@@ -47,15 +48,14 @@
} else {
output.push(renderText(elem));
}
- if (elem.children) {
- output.push(render(elem.children));
- }
+ if (elem.children) output.push(render(elem.children));
if (!singleTag[elem.name] && tagType[elem.type]) {
output.push("</" + elem.name + ">");
}
}
return output.join("");
};
+
renderTag = exports.renderTag = function(elem) {
var tag;
tag = "<" + elem.name;
@@ -69,14 +69,19 @@
}
return tag;
};
+
renderDirective = function(elem) {
return "<" + elem.data + ">";
};
+
renderText = function(elem) {
return elem.data;
};
+
renderComment = function(elem) {
return '<!--' + elem.data + '-->';
};
+
module.exports = exports;
+
}).call(this);
View
89 lib/renderer.js
@@ -1,89 +0,0 @@
-(function() {
- var render, renderComment, renderDirective, renderTag, renderText, singleTag, tagType, utils, _;
- _ = require("underscore");
- utils = require("./utils");
- 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
- };
- tagType = {
- tag: 1,
- script: 1,
- link: 1,
- style: 1,
- template: 1
- };
- render = exports.render = function(dom, output) {
- var data, elem, str, _i, _len;
- if (output == null) {
- output = [];
- }
- if (!_.isArray(dom)) {
- dom = [dom];
- }
- for (_i = 0, _len = dom.length; _i < _len; _i++) {
- elem = dom[_i];
- str = elem.name;
- if (elem.raw === null) {
- continue;
- }
- data = elem.data;
- if (data[0] === '%' && data[data.length - 1] === '%') {
- elem.type = "template";
- }
- if (tagType[elem.type]) {
- output.push(renderTag(elem));
- } else if (elem.type === "directive") {
- output.push(renderDirective(elem));
- } else if (elem.type === "comment") {
- output.push(renderComment(elem));
- } else {
- output.push(renderText(elem));
- }
- if (elem.children) {
- output.push(render(elem.children));
- }
- if (!singleTag[elem.name] && tagType[elem.type]) {
- output.push("</" + elem.name + ">");
- }
- }
- return output.join("");
- };
- renderTag = function(elem) {
- var tag;
- tag = "<" + elem.name;
- if (elem.attribs && _.size(elem.attribs) > 0) {
- tag += " " + utils.formatAttributes(elem.attribs);
- }
- if (!singleTag[elem.name]) {
- tag += ">";
- } else {
- tag += "/>";
- }
- return tag;
- };
- renderDirective = function(elem) {
- return "<" + elem.raw + ">";
- };
- renderText = function(elem) {
- return elem.raw;
- };
- renderComment = function(elem) {
- return '<!--' + elem.raw + '-->';
- };
- module.exports = exports;
-}).call(this);
View
38 lib/utils.js
@@ -1,10 +1,9 @@
(function() {
var formatAttributes, print, print_r;
+
formatAttributes = exports.formatAttributes = function(attributes) {
var key, output, value;
- if (!attributes) {
- return "";
- }
+ if (!attributes) return "";
output = [];
for (key in attributes) {
value = attributes[key];
@@ -16,27 +15,22 @@
}
return output.join(" ");
};
+
module.exports = exports;
+
String.prototype.repeat = function(times) {
return new Array(times + 1).join(this);
};
+
print = exports.print = function(dom, attr, depth, out) {
var attrs, elem, passback, prop, str, type, val, _i, _j, _len, _len2;
- if (attr == null) {
- attr = null;
- }
- if (depth == null) {
- depth = 0;
- }
- if (out == null) {
- out = [];
- }
+ if (attr == null) attr = null;
+ if (depth == null) depth = 0;
+ if (out == null) out = [];
for (_i = 0, _len = dom.length; _i < _len; _i++) {
elem = dom[_i];
type = elem.type;
- if (type === "tag" || type === "script" || type === "style") {
- continue;
- }
+ if (type === "tag" || type === "script" || type === "style") continue;
str = ''.repeat(depth);
if (depth > 0) {
str += str + " " + elem.name;
@@ -55,22 +49,17 @@
break;
}
}
- if (passback) {
- str += " ( " + attr + " : " + val + " )";
- }
+ if (passback) str += " ( " + attr + " : " + val + " )";
}
out.push(str);
- if (elem.children) {
- print(elem.children, attr, depth + 1, out);
- }
+ if (elem.children) print(elem.children, attr, depth + 1, out);
}
return out.join("\n");
};
+
print_r = exports.print_r = function(arr, attr) {
var attributes, out;
- if (attr == null) {
- attr = "name";
- }
+ if (attr == null) attr = "name";
out = "[ ";
attributes = arr.map(function(elem) {
return elem[attr];
@@ -79,4 +68,5 @@
out += " ]";
return out;
};
+
}).call(this);
View
32 src/api/attributes.coffee
@@ -16,7 +16,7 @@ hasClass = exports.hasClass = (selector) ->
for elem in this
if $.isTag(elem) and elem.attribs and (" " + elem.attribs["class"] + " ").replace(rclass, " ").indexOf(className) > -1
return true
-
+
return false
addClass = exports.addClass = (value) ->
@@ -25,41 +25,41 @@ addClass = exports.addClass = (value) ->
$this = $(this)
className = $this.attr('class') or ""
$this.addClass value.call(this, i, className)
-
+
if value and _.isString value
classNames = value.split rspace
-
+
for elem in this
$elem = $(elem)
if $.isTag(elem)
-
+
if !$elem.attr("class")
$elem.attr('class', classNames.join(' ').trim())
-
+
else
setClass = " " + $elem.attr("class") + " "
-
+
for className in classNames
-
+
if !~setClass.indexOf(" " + className + " ")
setClass += className + " "
-
+
$elem.attr('class', setClass.trim())
-
-
+
+
return this
removeClass = exports.removeClass = (value) ->
-
+
if _.isFunction value
return this.each (j) ->
$this = $(this)
className = $this.attr('class') or ""
$this.removeClass value.call(this, j, className)
-
+
if (value and _.isString value) or value is undefined
classNames = (value || "").split rspace
-
+
for elem in this
$elem = $(elem)
if $.isTag(elem) and $elem.attr('class')
@@ -67,11 +67,11 @@ removeClass = exports.removeClass = (value) ->
ret = (" " + $elem.attr('class') + " ").replace( rclass, " " )
for className in classNames
ret = ret.replace( " " + className + " ", " ")
-
+
$elem.attr('class', ret.trim())
- else
+ else
$elem.attr('class', '');
-
+
return this
module.exports = $.fn.extend exports
View
22 src/api/core.coffee
@@ -3,7 +3,7 @@ $ = require '../cheerio'
size = exports.size = () ->
this.length
-
+
toArray = exports.toArray = () ->
return Array.prototype.slice.call this, 0
@@ -11,11 +11,11 @@ toArray = exports.toArray = () ->
# Get the whole matched element set as a clean array
get = exports.get = (num) ->
if num is undefined
- this.toArray()
+ this.toArray()
else
if num < 0
- this[this.length + num]
- else
+ this[this.length + num]
+ else
this[num]
# Take an array of elements and push it onto the stack
@@ -26,15 +26,15 @@ pushStack = exports.pushStack = (elems, name, selector) ->
push.apply ret, elems
else
$.merge ret, elems
-
+
ret.prevObject = this
ret.context = this.context
-
+
if name == "find"
ret.selector = this.selector + (if this.selector then " " else "") + selector
- else
+ else
ret.selector = this.selector + "." + name + "(" + selector + ")" if name
-
+
return ret
@@ -51,7 +51,7 @@ siblingsAndMe = exports.siblingsAndMe = () ->
while element.next
element = element.next
siblings.push element
-
- return siblings
-module.exports = $.fn.extend exports
+ return siblings
+
+module.exports = $.fn.extend exports
View
38 src/api/manipulation.coffee
@@ -18,7 +18,7 @@ append = exports.append = (elems...) ->
this.each ->
if _.isFunction elems[0]
# Not yet supported
- else
+ else
if !this.children
this.children = []
this.children = this.children.concat dom
@@ -33,7 +33,7 @@ prepend = exports.prepend = (elems...) ->
dom = dom.concat elem.toArray()
else
dom = dom.concat parse.eval(elem)
-
+
this.each ->
if _.isFunction elems[0]
# Not yet supported
@@ -60,11 +60,11 @@ after = exports.after = (elems...) ->
if index >= 0
siblings.splice.apply(siblings, [index + 1, 0].concat(dom))
-
+
# Update siblings
$.updateDOM siblings, this.parent
this.parent.children = siblings
-
+
return this
before = exports.before = (elems...) ->
@@ -74,7 +74,7 @@ before = exports.before = (elems...) ->
dom = dom.concat elem.toArray()
else
dom = dom.concat parse.eval(elem)
-
+
this.each ->
siblings = this.parent.children
index = siblings.indexOf(this)
@@ -86,21 +86,21 @@ before = exports.before = (elems...) ->
$.updateDOM siblings, this.parent
this.parent.children = siblings
- return this
+ return this
remove = exports.remove = (selector) ->
elems = this
if selector
elems = this.find(selector)
-
+
elems.each ->
siblings = this.parent.children
index = siblings.indexOf(this)
siblings.splice index, 1
-
+
$.updateDOM siblings, this.parent
this.parent.children = siblings
-
+
return this
replaceWith = exports.replaceWith = (content) ->
@@ -109,10 +109,10 @@ replaceWith = exports.replaceWith = (content) ->
this.each ->
siblings = this.parent.children
index = siblings.indexOf(this)
-
+
# siblings.slice(index, 1, elem1, elem2, elem3, ...)
siblings.splice.apply(siblings, [index, 1].concat(elems))
-
+
$.updateDOM siblings, this.parent
this.parent.children = siblings
@@ -130,17 +130,17 @@ html = exports.html = (htmlString) ->
return this
else
return $.html this[0]
-
+
text = exports.text = (textString) ->
if _.isFunction textString
this.each (i) ->
self = $(this)
self.text textString.call this, i, self.text()
-
+
# Set the text
if typeof textString isnt "object" and textString isnt undefined
-
- textElement =
+
+ textElement =
raw : textString
data : textString
type : "text"
@@ -148,12 +148,12 @@ text = exports.text = (textString) ->
prev : null
next : null
children : []
-
+
this.each (i) ->
this.children = textElement
$.updateDOM this.children, this
return this
- else
+ else
return $.text this
-
-module.exports = $.fn.extend exports
+
+module.exports = $.fn.extend exports
View
26 src/api/traversing.coffee
@@ -4,14 +4,14 @@ soupselect = require "cheerio-soupselect"
$ = require "../cheerio"
###
Stupidly simple traversal
-
+
TODO : Make it more jQuery-like
###
find = exports.find = (selector) ->
if !selector
return this
-
+
elem = soupselect.select this.toArray(), selector
return $(elem)
@@ -20,22 +20,22 @@ parent = exports.parent = (elem) ->
next = exports.next = (elem) ->
return null if not this[0]
-
+
nextSibling = this[0].next
while nextSibling
return $(nextSibling) if $.isTag nextSibling
nextSibling = nextSibling.next
-
+
return null
-
+
prev = exports.prev = (elem) ->
return null if not this[0]
-
+
prevSibling = this[0].prev
while prevSibling
return $(prevSibling) if $.isTag prevSibling
prevSibling = prevSibling.prev
-
+
return null
siblings = exports.siblings = (elem) ->
@@ -43,10 +43,10 @@ siblings = exports.siblings = (elem) ->
sibs = this.parent().children()
else
sibs = this.siblingsAndMe()
-
+
siblings = _.filter sibs, (elem) =>
elem isnt this[0] and $.isTag elem
-
+
return $(siblings)
children = exports.children = (selector) ->
@@ -58,12 +58,12 @@ children = exports.children = (selector) ->
if _.isNumber selector
if children[selector]
return $(children[selector])
- else
+ else
return null
else
return $(children).find selector
-
-
+
+
return $(children)
else
return null
@@ -74,4 +74,4 @@ children = exports.children = (selector) ->
each = exports.each = (callback, args) ->
return $.each this, callback, args
-module.exports = $.fn.extend exports
+module.exports = $.fn.extend exports
View
78 src/api/utils.coffee
@@ -6,11 +6,11 @@ render = require "../render"
# [[Class]] -> type pairs
class2type = {}
-
+
# Populate class2type map
_.each "Boolean Number String Function Array Date Regex Object".split(" "), (name, i) ->
class2type[ "[object #{name}]" ] = name.toLowerCase()
-
+
###
Node Types
directive : 10
@@ -23,13 +23,13 @@ Node Types
# Some regexs
rboolean = /^(?:autofocus|autoplay|async|checked|controls|defer|disabled|hidden|loop|multiple|open|readonly|required|scoped|selected)$/i
-
+
# Save a reference to some core methods
toString = Object.prototype.toString
push = Array.prototype.push
indexOf = Array.prototype.indexOf
-tags =
+tags =
'tag' : 1
'script' : 1
'style' : 1
@@ -37,11 +37,11 @@ tags =
isTag = exports.isTag = (type) ->
if type.type
type = type.type
-
+
return if tags[type] then true else false
updateDOM = exports.updateDOM = (arr, parent) ->
- # normalize
+ # normalize
arr = $(arr).get()
for elem, i in arr
@@ -51,9 +51,9 @@ updateDOM = exports.updateDOM = (arr, parent) ->
if !parent.children
parent.children = []
-
+
parent.children = arr
-
+
return parent
type = exports.type = ( obj ) ->
@@ -61,24 +61,24 @@ type = exports.type = ( obj ) ->
isArray = exports.isArray = (array) ->
return _(this).isArray()
-
+
merge = exports.merge = (first, second) ->
i = first.length
j = 0
-
+
if typeof second.length == "number"
l = second.length
while j < l
first[i++] = second[j]
j++
-
+
else
while second[j] != undefined
first[i++] = second[j++]
-
+
first.length = i
-
+
return first
makeArray = exports.makeArray = (array, results) ->
@@ -89,13 +89,13 @@ makeArray = exports.makeArray = (array, results) ->
push.call ret, array
else
$.merge ret, array
-
+
return ret
-
+
inArray = exports.inArray = (elem, array) ->
if !array
return -1
-
+
return indexOf.call(array, elem)
# Args is for internal usage only
@@ -110,7 +110,7 @@ each = exports.each = (object, callback, args) ->
else
while i < length
break if callback.apply(object[i++], args) is false
-
+
# A special fast, case for the most common use of each
else
if isObj
@@ -132,7 +132,7 @@ access = exports.access = (elems, key, value, exec, fn, pass) ->
for k of key
access elems, k, key[k], exec, fn, value
return elems
-
+
# Setting one attribute
if value isnt undefined
exec = not pass and exec and _.isFunction(value)
@@ -142,36 +142,36 @@ access = exports.access = (elems, key, value, exec, fn, pass) ->
fn elems[i], key, (if exec then value.call(elems[i], i, fn(elems[i], key)) else value), pass
i++
return elems
-
+
# Getting an attribute
return (if length then fn(elems[0], key) else undefined)
attr = exports.attr = (elem, name, value, pass) ->
type = elem.type
-
+
if (!elem or !$.isTag(elem))
- return undefined
-
+ return undefined
+
if !elem.attribs
elem.attribs = {}
-
+
# Return the entire attribs object if no attribute specified
if !name
return elem.attribs
-
+
if value isnt undefined
-
+
if value is null
$.removeAttr elem, name
-
+
# Set the attribute
else
elem.attribs[name] = "" + value
-
+
else
# Get the attribute
return elem.attribs[name]
-
+
removeAttr = exports.removeAttr = (elem, name) ->
if isTag(elem.type) and elem.attribs
@@ -180,19 +180,19 @@ removeAttr = exports.removeAttr = (elem, name) ->
elem.attribs[name] = false
else
delete elem.attribs[name]
-
+
text = exports.text = (elems) ->
ret = ""
-
+
if !elems
return ret
-
+
for elem in elems
if elem.type is "text"
ret += elem.data
else if elem.children && elem.type isnt "comment"
ret += text elem.children
-
+
return ret
# setRoot = (html) ->
@@ -200,23 +200,23 @@ text = exports.text = (elems) ->
# root = parser.parse html
# else if html.length
# root = html
-#
+#
# $.extend
# 'root' : root
-#
+#
# return root
-
+
load = exports.load = (html) ->
root = parse html
$.extend
'root' : root
-
+
fn = (selector, context, r) ->
if r
root = parse r
-
+
$ selector, context, root
return _(fn).extend $
@@ -230,7 +230,7 @@ html = exports.html = (dom) ->
return ""
dom = exports.dom = (dom) ->
- if dom isnt undefined
+ if dom isnt undefined
if dom.type
return dom
else if this.root and this.root.children
@@ -238,4 +238,4 @@ dom = exports.dom = (dom) ->
else
return ""
-module.exports = $.extend exports
+module.exports = $.extend exports
View
20 src/cheerio.coffee
@@ -8,13 +8,13 @@ parse = require "./parse"
cheerio = do ->
cheerio = (selector, context, root) ->
return new cheerio.fn.init selector, context, root
-
+
# A simple way to check for HTML strings or ID strings
# Prioritize #id over <tag> to avoid XSS via location.hash (#9521)
quickExpr = /^(?:[^#<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/
-
+
# Used for trimming whitespace
- trimLeft = /^\s+/
+ trimLeft = /^\s+/
trimRight = /\s+$/
cheerio.fn = cheerio.prototype =
@@ -24,7 +24,7 @@ cheerio = do ->
# Handle $(""), $(null), or $(undefined)
if not selector
return this
-
+
if root
cheerio.extend
'root' : root
@@ -50,30 +50,30 @@ cheerio = do ->
elems = soupselect.select context, selector
this.selector = selector
return cheerio.merge this, elems
-
+
###
Refactor
###
# HANDLE: $(expr, $(...))
if !context or context.cheerio
return this.constructor(context or root).find selector
-
+
# HANDLE: $(expr, context)
else
if _.isString context
context = parse context
return this.constructor(context).find selector
- return cheerio.makeArray( selector, this );
-
+ return cheerio.makeArray( selector, this );
+
selector : ""
sort : [].sort
splice : [].splice
length : 0
-
+
# Give the init function the jQuery prototype for later instantiation
cheerio.fn.init.prototype = cheerio.fn
-
+
# Use underscores extend
cheerio.extend = cheerio.fn.extend = (obj) ->
return _.extend this, obj
View
24 src/parse.coffee
@@ -5,7 +5,7 @@ htmlparser = require "htmlparser2"
###
parser = exports = module.exports = (content) ->
dom = eval content
-
+
root = {
type : 'root'
name : 'root'
@@ -14,18 +14,18 @@ parser = exports = module.exports = (content) ->
next : null
children : []
}
-
+
root.children = connect dom, root
return root
eval = exports.eval = (content) ->
- handler = new htmlparser.DefaultHandler()
+ handler = new htmlparser.DefaultHandler({ ignoreWhitespace: true })
parser = new htmlparser.Parser handler
-
+
parser.includeLocation = false
parser.parseComplete content
-
+
return handler.dom
isTag = (type) ->
@@ -33,7 +33,7 @@ isTag = (type) ->
return true
else
return false
-
+
connect = exports.connect = (dom, parent = null) ->
prevIndex = -1
lastElem = null
@@ -42,10 +42,10 @@ connect = exports.connect = (dom, parent = null) ->
# If tag and no attributes, add empty object
if isTag(dom[i].type) and dom[i].attribs is undefined
dom[i].attribs = {}
-
+
# Set parent
dom[i].parent = parent
-
+
# Previous sibling
prev = dom[prevIndex]
if prev
@@ -57,19 +57,19 @@ connect = exports.connect = (dom, parent = null) ->
dom[i].next = null
if lastElem
lastElem.next = dom[i]
-
+
# Run through the children
if dom[i].children
connect dom[i].children, dom[i]
# Otherwise instantiate it if its a tag
else if isTag(dom[i].type)
dom[i].children = []
-
+
# Get ready for next elem
prevIndex = i
lastElem = dom[i]
-
-
+
+
return dom
module.exports = exports
View
16 src/render.coffee
@@ -3,7 +3,7 @@ _ = require "underscore"
utils = require "./utils"
# List from node-htmlparser (which I stole from jsdom ;-P)
-singleTag =
+singleTag =
area: 1
base: 1
basefont: 1
@@ -21,7 +21,7 @@ singleTag =
include: 1
yield: 1
-tagType =
+tagType =
tag : 1
script : 1
link : 1
@@ -31,7 +31,7 @@ tagType =
render = exports = module.exports = (dom, output = []) ->
if !_.isArray(dom)
dom = [dom]
-
+
for elem in dom
str = elem.name
# Used to remove elements
@@ -51,26 +51,26 @@ render = exports = module.exports = (dom, output = []) ->
if elem.children
output.push render elem.children
-
+
if !singleTag[elem.name] and tagType[elem.type]
output.push "</" + elem.name + ">"
return output.join ""
-
+
renderTag = exports.renderTag = (elem) ->
tag = "<" + elem.name
if(elem.attribs and _.size(elem.attribs) > 0)
-
+
tag += " " + utils.formatAttributes elem.attribs
if !singleTag[elem.name]
tag += ">"
else
tag += "/>"
-
+
return tag
-
+
renderDirective = (elem) ->
return "<" + elem.data + ">"
View
28 src/utils.coffee
@@ -1,7 +1,7 @@
formatAttributes = exports.formatAttributes = (attributes) ->
- if !attributes
+ if !attributes
return ""
-
+
output = []
for key, value of attributes
if key is value
@@ -10,12 +10,12 @@ formatAttributes = exports.formatAttributes = (attributes) ->
output.push key + ' = "' + value + '"';
output.join " "
-
+
module.exports = exports
String::repeat = (times) ->
return new Array( times + 1).join( this );
-
+
print = exports.print = (dom, attr = null, depth = 0, out = []) ->
for elem in dom
type = elem.type
@@ -26,7 +26,7 @@ print = exports.print = (dom, attr = null, depth = 0, out = []) ->
str += str + " " + elem.name
else
str += elem.name
-
+
if attr
attrs = attr.split "."
val = elem
@@ -36,22 +36,22 @@ print = exports.print = (dom, attr = null, depth = 0, out = []) ->
if not val
passback = false
break
-
+
if passback
str += " ( "+ attr + " : " + val + " )"
-
- out.push str
-
+
+ out.push str
+
if elem.children
print elem.children, attr, depth+1, out
-
- return out.join "\n"
-
+
+ return out.join "\n"
+
print_r = exports.print_r = (arr, attr = "name") ->
out = "[ "
-
+
attributes = arr.map (elem) ->
return elem[attr]
out += attributes.join ", "
out += " ]"
- return out
+ return out
Please sign in to comment.
Something went wrong with that request. Please try again.