How to use the cheerio.parse function in cheerio

To help you get started, we’ve selected a few cheerio examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github matthewmueller / cheerio-select / test / select.element.js View on Github external
/**
 * Module dependencies.
 */
var $ = require('../'),
    _ = require('underscore'),
    parse = require('cheerio').parse,
    expect  = require('expect.js'),
    read = require('fs').readFileSync,
    helpers = require('./helpers'),
    t = helpers.t;

/*
 * Load test data
 */
var html = read(__dirname + '/fixtures/index.html'),
    dom  = parse(html);

/**
 * Mocha tests
 */
describe('select.element', function() {

  it('("*", dom) : should select all elements', function() {
    var elems = $('*', dom),
        nodeType = /tag|script|link/;

    // Select all
    expect(elems.length).to.be.above(30);

    // They should all be tags
    _.each(elems, function(elem) {
      expect(nodeType.test(elem.type)).to.be.ok();
github matthewmueller / cheerio-select / test / select.class.js View on Github external
it('(".e") : should find second classes', function() {
    var test = parse("<div><div class="test e"></div><div class="test"></div></div>");
    expect($(".e", test)).to.have.length(1);
    
    // Now add .e to second div and test again
    test.children[0].children[1].attribs['class'] = 'e';
    expect($(".e", test)).to.have.length(2);
  });
});
github matthewmueller / cheerio-select / test / helpers.js View on Github external
/**
 * Module dependencies.
 */
var $ = require('../'),
    _ = require('underscore'),
    parse = require('cheerio').parse,
    expect  = require('expect.js'),
    helpers = require('./helpers'),
    read = require('fs').readFileSync;

/* 
 * Load test data
 */
var html = read(__dirname + '/fixtures/index.html'),
    dom  = parse(html);

/*
 * Helper classes
 */

exports.ids = function(elem) {
  return elem.attribs.id;
};

exports.t = function(description, selector, ids, d) {
  it(description, function() {
    var result = $(selector, d || dom).map(exports.ids);
    expect(result).to.eql(ids);
  });
};
github matthewmueller / cheerio-select / test / select.class.js View on Github external
/**
 * Module dependencies.
 */
var $ = require('../'),
    _ = require('underscore'),
    parse = require('cheerio').parse,
    expect  = require('expect.js'),
    read = require('fs').readFileSync,
    helpers = require('./helpers'),
    t = helpers.t;

/*
 * Load test data
 */
var html = read(__dirname + '/fixtures/index.html'),
    dom  = parse(html);

/**
 * Mocha tests
 */
describe('select.class', function() {
  t( "Class Selector", ".blog", ["mark","simon"] );
  t( "Class Selector", ".GROUPS", ["groups"] );
  t( "Class Selector", ".blog.link", ["simon"] );
  t( "Class Selector w/ Element", "a.blog", ["mark","simon"] );
  t( "Parent Class Selector", "p .blog", ["mark","simon"] );

  t('("p .blog") : should find elements within class', 'p .blog', ['mark', 'simon']);

  // No support for UTF8 yet...
  t( "Class selector using UTF8", ".台北Táiběi", ["utf8class1"] );
  t( "Class selector using UTF8", ".台北", ["utf8class1","utf8class2"] );