Вход Регистрация
Файл: error-kitty/node_modules/cheerio/lib/static.js
Строк: 171
<?php
/**
 * Module dependencies
 */

var select = require('CSSselect'),
    
parse = require('./parse'),
    
render = require('dom-serializer'),
    
= require('lodash');

/**
 * $.load(str)
 */

var load exports.load = function(contentoptions) {
  var 
Cheerio = require('./cheerio');

  
options _.defaults(options || {}, Cheerio.prototype.options);

  var 
root parse(contentoptions);

  var 
initialize = function(selectorcontextropts) {
    
opts _.defaults(opts || {}, options);
    return new 
Cheerio(selectorcontext|| rootopts);
  };

  
// Add in the static methods
  
initialize.__proto__ exports;

  
// Add in the root
  
initialize._root root;
  
// store options
  
initialize._options options;

  return 
initialize;
};

/**
 * $.html([selector | dom])
 */

var html exports.html = function(domoptions) {
  var 
Cheerio = require('./cheerio');

  
// be flexible about parameters, sometimes we call html(),
  // with options as only parameter
  // check dom argument for dom element specific properties
  // assume there is no 'length' or 'type' properties in the options object
  
if (Object.prototype.toString.call(dom) === '[object Object]' && !options && !('length' in dom) && !('type' in dom))
  {
    
options dom;
    
dom undefined;
  }

  
// sometimes $.html() used without preloading html
  // so fallback non existing options to the default ones
  
options _.defaults(options || {}, this._optionsCheerio.prototype.options);

  if (
dom) {
    
dom = (typeof dom === 'string') ? select(domthis._rootoptions) : dom;
    return 
render(domoptions);
  } else if (
this._root && this._root.children) {
    return 
render(this._root.childrenoptions);
  } else {
    return 
'';
  }
};

/**
 * $.xml([selector | dom])
 */

var xml exports.xml = function(dom) {
  if (
dom) {
    
dom = (typeof dom === 'string') ? select(domthis._rootthis.options) : dom;
    return 
render(dom, { xmlModetrue });
  } else if (
this._root && this._root.children) {
    return 
render(this._root.children, { xmlModetrue });
  } else {
    return 
'';
  }
};

/**
 * $.text(dom)
 */

var text exports.text = function(elems) {
  if (!
elems) return '';

  var 
ret '',
      
len elems.length,
      
elem;

  for (var 
0len++) {
    
elem elems[i];
    if (
elem.type === 'text'ret += elem.data;
    else if (
elem.children && elem.type !== 'comment') {
      
ret += text(elem.children);
    }
  }

  return 
ret;
};

/**
 * $.parseHTML(data [, context ] [, keepScripts ])
 * Parses a string into an array of DOM nodes. The `context` argument has no
 * meaning for Cheerio, but it is maintained for API compatibility with jQuery.
 */
var parseHTML exports.parseHTML = function(datacontextkeepScripts) {
  var 
parsed;

  if (!
data || typeof data !== 'string') {
    return 
null;
  }

  if (
typeof context === 'boolean') {
    
keepScripts context;
  }

  
parsed this.load(data);
  if (!
keepScripts) {
    
parsed('script').remove();
  }

  return 
parsed.root()[0].children;
};

/**
 * $.root()
 */
var root exports.root = function() {
  return 
this(this._root);
};

/**
 * $.contains()
 */
var contains exports.contains = function(containercontained) {

  
// According to the jQuery API, an element does not "contain" itself
  
if (contained === container) {
    return 
false;
  }

  
// Step up the descendants, 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;
};
?>
Онлайн: 0
Реклама