Skip to content

Commit

Permalink
testing selectors, and some addition fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewmueller committed Dec 17, 2011
1 parent c14a3eb commit 3b247b6
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 48 deletions.
18 changes: 14 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
// Use source if we have coffeescript otherwise use lib
try {
require('coffee-script');
module.exports = require(__dirname + "/src/cheerio.coffee");
require('coffee-script');
base = './src/'
} catch (e) {
module.exports = require(__dirname + "/lib/cheerio.js");
}
base = './lib/'
}

exports = module.exports = require(base + 'cheerio')

exports.parse = require(base + 'parse')
exports.render = require(base + 'render')
exports.utils = require(base + 'utils')

/*
Attach other modules on here, this will allow testing to be done in mocha without recompiling
*/
16 changes: 8 additions & 8 deletions lib/api/manipulation.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions lib/api/utils.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions lib/cheerio.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
(function() {
var api, cheerio, parser, path, plugin, soupselect, _, _i, _len;
var api, cheerio, parse, path, plugin, soupselect, _, _i, _len;
path = require("path");
soupselect = require("cheerio-soupselect");
_ = require("underscore");
parser = require("./parser");
parse = require("./parse");
cheerio = (function() {
var quickExpr, trimLeft, trimRight;
cheerio = function(selector, context, root) {
console.log('zzz');
return new cheerio.fn.init(selector, context, root);
};
quickExpr = /^(?:[^#<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/;
Expand Down Expand Up @@ -37,7 +38,7 @@
}
if (match && (match[1] || !context)) {
if (match[1]) {
root = parser.parse(selector);
root = parse(selector);
return cheerio.merge(this, root.children);
} else {
elems = soupselect.select(context, selector);
Expand All @@ -52,7 +53,7 @@
return this.constructor(context || root).find(selector);
} else {
if (_.isString(context)) {
context = parser.parse(context);
context = parse(context);
}
return this.constructor(context).find(selector);
}
Expand Down
14 changes: 7 additions & 7 deletions src/api/manipulation.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
_ = require 'underscore'
$ = require '../cheerio'
parser = require '../parser'
parse = require '../parse'

removeChild = (parent, elem) ->
$.each parent.children, (i, child) ->
Expand All @@ -10,7 +10,7 @@ removeChild = (parent, elem) ->
append = exports.append = (elems...) ->
dom = []
for elem in elems
dom = dom.concat parser.eval(elem)
dom = dom.concat parse.eval(elem)

this.each ->
if _.isFunction elems[0]
Expand All @@ -26,7 +26,7 @@ append = exports.append = (elems...) ->
prepend = exports.prepend = (elems...) ->
dom = []
for elem in elems
dom = dom.concat parser.eval(elem)
dom = dom.concat parse.eval(elem)

this.each ->
if _.isFunction elems[0]
Expand All @@ -43,7 +43,7 @@ prepend = exports.prepend = (elems...) ->
after = exports.after = (elems...) ->
dom = []
for elem in elems
dom = dom.concat parser.eval(elem)
dom = dom.concat parse.eval(elem)

this.each ->
siblings = this.parent.children
Expand All @@ -61,7 +61,7 @@ after = exports.after = (elems...) ->
before = exports.before = (elems...) ->
dom = []
for elem in elems
dom = dom.concat parser.eval(elem)
dom = dom.concat parse.eval(elem)

this.each ->
siblings = this.parent.children
Expand Down Expand Up @@ -92,7 +92,7 @@ remove = exports.remove = (selector) ->
return this

replaceWith = exports.replaceWith = (content) ->
elems = parser.eval(content)
elems = parse.eval(content)

this.each ->
siblings = this.parent.children
Expand All @@ -110,7 +110,7 @@ empty = exports.empty = () ->

html = exports.html = (htmlString) ->
if typeof htmlString isnt "object" and htmlString isnt undefined
htmlElement = parser.eval htmlString
htmlElement = parse.eval htmlString

this.each (i) ->
this.children = htmlElement
Expand Down
14 changes: 7 additions & 7 deletions src/api/utils.coffee
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
_ = require "underscore"
$ = require "../cheerio"

parser = require "../parser"
renderer = require "../renderer"
parse = require "../parse"
render = require "../render"

# [[Class]] -> type pairs
class2type = {}
Expand Down Expand Up @@ -83,7 +83,7 @@ merge = exports.merge = (first, second) ->

makeArray = exports.makeArray = (array, results) ->
ret = results or []
if array?
if array
type = $.type(array)
if not array.length? or type == "string" or type == "function" or type == "regexp"
push.call ret, array
Expand Down Expand Up @@ -208,24 +208,24 @@ text = exports.text = (elems) ->


load = exports.load = (html) ->
root = parser.parse html
root = parse html

$.extend
'root' : root

fn = (selector, context, r) ->
if r
root = parser.parse r
root = parse r

$ selector, context, root

return _(fn).extend $

html = exports.html = (dom) ->
if dom isnt undefined and dom.type
return renderer.render dom
return render dom
else if this.root and this.root.children
return renderer.render this.root.children
return render this.root.children
else
return ""

Expand Down
12 changes: 5 additions & 7 deletions src/cheerio.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ path = require "path"
soupselect = require "cheerio-soupselect"
_ = require "underscore"

parser = require "./parser"
parse = require "./parse"

cheerio = do ->
cheerio = (selector, context, root) ->
Expand All @@ -24,7 +24,7 @@ cheerio = do ->
# Handle $(""), $(null), or $(undefined)
if not selector
return this

if root
cheerio.extend
'root' : root
Expand All @@ -43,7 +43,7 @@ cheerio = do ->
if match && (match[1] || !context)
if match[1]
# It's an HTML string
root = parser.parse selector
root = parse selector
return cheerio.merge this, root.children
else
# Classes, IDs just defer to soupselect
Expand All @@ -61,12 +61,10 @@ cheerio = do ->
# HANDLE: $(expr, context)
else
if _.isString context
context = parser.parse context
context = parse context
return this.constructor(context).find selector

return cheerio.makeArray( selector, this );
# if context
# return cheerio selector, parser.parse con

selector : ""
sort : [].sort
Expand All @@ -79,7 +77,7 @@ cheerio = do ->
# Use underscores extend
cheerio.extend = cheerio.fn.extend = (obj) ->
return _.extend this, obj

return cheerio

module.exports = cheerio
Expand Down
Loading

0 comments on commit 3b247b6

Please sign in to comment.