Comparing version 0.0.2 to 1.0.0
123
dom-eee.js
@@ -34,6 +34,6 @@ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.eee = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
var dom = spec.selector === ':self' ? element : | ||
env.single(element, spec.selector); | ||
env.single(element, spec.selector); | ||
if (dom) { | ||
if (spec.attribute) { | ||
// Extracts attribute value. | ||
// Extracts attribute value. | ||
return env.attribute(dom, spec.attribute); | ||
@@ -53,2 +53,3 @@ } else if (spec.property) { | ||
function evaluate(env, element, spec, options) { | ||
// TODO check that element is set. | ||
var ret = {}; | ||
@@ -76,24 +77,24 @@ Object.keys(spec).forEach(function(key) { | ||
module.exports = function(root, spec, options) { | ||
if (typeof options === 'undefined') { | ||
options = {}; | ||
} | ||
if (typeof options.env === 'undefined') { | ||
options.env = 'browser'; | ||
} | ||
var env = environments[options.env]; | ||
if (!env) { | ||
throw new Error('Unknown environment: ' + options.env); | ||
} | ||
var instance = null; | ||
if (options.env === 'cheerio') { | ||
if (typeof options.cheerio === 'undefined') { | ||
throw new Error('Cheerio context not set.'); | ||
} else { | ||
// Cheerio-specific instance. | ||
instance = env(options.cheerio); | ||
} | ||
} else { | ||
// New generic environment instance. | ||
instance = env(); | ||
} | ||
if (typeof options === 'undefined') { | ||
options = {}; | ||
} | ||
if (typeof options.env === 'undefined') { | ||
options.env = 'browser'; | ||
} | ||
var env = environments[options.env]; | ||
if (!env) { | ||
throw new Error('Unknown environment: ' + options.env); | ||
} | ||
var instance = null; | ||
if (options.env === 'cheerio') { | ||
if (typeof options.cheerio === 'undefined') { | ||
throw new Error('Cheerio context not set.'); | ||
} else { | ||
// Cheerio-specific instance. | ||
instance = env(options.cheerio); | ||
} | ||
} else { | ||
// New generic environment instance. | ||
instance = env(); | ||
} | ||
return evaluate(instance, root, spec, options); | ||
@@ -106,20 +107,20 @@ }; | ||
module.exports = function() { | ||
return { | ||
single: function(element, selector) { | ||
return element.querySelector(selector); | ||
}, | ||
collection: function(element, selector) { | ||
return Array.prototype.slice.call( | ||
element.querySelectorAll(selector), 0); | ||
}, | ||
attribute: function(element, attribute) { | ||
return element.getAttribute(attribute); | ||
}, | ||
property: function(element, property) { | ||
return element[property]; | ||
}, | ||
text: function(element) { | ||
return element.textContent; | ||
}, | ||
}; | ||
return { | ||
single: function(element, selector) { | ||
return element.querySelector(selector); | ||
}, | ||
collection: function(element, selector) { | ||
return Array.prototype.slice.call( | ||
element.querySelectorAll(selector), 0); | ||
}, | ||
attribute: function(element, attribute) { | ||
return element.getAttribute(attribute); | ||
}, | ||
property: function(element, property) { | ||
return element[property]; | ||
}, | ||
text: function(element) { | ||
return element.textContent; | ||
}, | ||
}; | ||
}; | ||
@@ -131,19 +132,19 @@ | ||
module.exports = function($) { | ||
return { | ||
single: function(element, selector) { | ||
return $(selector, element); | ||
}, | ||
collection: function(element, selector) { | ||
return $(selector, element).toArray(); | ||
}, | ||
attribute: function(element, attribute) { | ||
return $(element).attr(attribute); | ||
}, | ||
property: function(element, property) { | ||
return $(element).prop(property); | ||
}, | ||
text: function(element) { | ||
return $(element).text(); | ||
} | ||
}; | ||
return { | ||
single: function(element, selector) { | ||
return $(selector, element); | ||
}, | ||
collection: function(element, selector) { | ||
return $(selector, element).toArray(); | ||
}, | ||
attribute: function(element, attribute) { | ||
return $(element).attr(attribute); | ||
}, | ||
property: function(element, property) { | ||
return $(element).prop(property); | ||
}, | ||
text: function(element) { | ||
return $(element).text(); | ||
} | ||
}; | ||
}; | ||
@@ -156,4 +157,4 @@ | ||
module.exports = { | ||
browser: browser, | ||
cheerio: cheerio | ||
browser: browser, | ||
cheerio: cheerio | ||
}; | ||
@@ -160,0 +161,0 @@ |
@@ -51,2 +51,3 @@ var environments = require('./lib/env'); | ||
function evaluate(env, element, spec, options) { | ||
// TODO check that element is set. | ||
var ret = {}; | ||
@@ -53,0 +54,0 @@ Object.keys(spec).forEach(function(key) { |
{ | ||
"name": "dom-eee", | ||
"version": "0.0.2", | ||
"version": "1.0.0", | ||
"devDependencies": { | ||
@@ -5,0 +5,0 @@ "browserify": "^13.0.0", |
@@ -12,2 +12,101 @@ # Helper to extract data from DOM | ||
## Example | ||
This example uses Cheerio: | ||
```javascript | ||
var cheerio = require('cheerio'); | ||
var eee = require('dom-eee'); | ||
var html = '<ul><li>item1</li><li>item2 <span>with span</span></li></ul>'; | ||
var $ = cheerio.load(html); | ||
var result = eee($.root(), | ||
{ | ||
items: { | ||
selector: 'li', | ||
type: 'collection', | ||
extract: { text: { selector: ':self' } }, | ||
filter: { exists: 'span' } | ||
} | ||
}, | ||
{ env: 'cheerio', cheerio: $ }); | ||
console.log(result); | ||
``` | ||
Prints: | ||
``` | ||
{ items: [ { text: 'item2 with span' } ] } | ||
``` | ||
## Extraction expressions | ||
The system works by evaluating an object-formatted DSL | ||
expression. The syntax of the DSL and its semantics is | ||
described below. | ||
ObjectExpression: | ||
```javascript | ||
{ | ||
"prop1": Expression, | ||
"prop2": Expression | ||
} | ||
``` | ||
ObjectExpression returns an object with given properties. | ||
Property values are described by further Expressions. | ||
Expression is either CollectionExpression or | ||
SingleExpression, returning a value described by it. | ||
CollectionExpression: | ||
```javascript | ||
{ | ||
"type": "collection", | ||
"selector": CSSSelector, | ||
"extract": ObjectExpression, | ||
"filter": FilterExpression | ||
} | ||
``` | ||
CollectionExpression returns an array of items. Items are extracted | ||
by applying `extract` expression to each element matched by the | ||
`selector` CSS rule. If the rule matches no elements then an empty | ||
array is returned. | ||
Optionally, the `filter` property might be set. Then the array of | ||
raw elements is first filtered through the FilterExpression. | ||
SingleExpression: | ||
```javascript | ||
{ | ||
"type": "single", | ||
"selector": CSSSelector, | ||
"property": String, | ||
"attribute": String | ||
} | ||
``` | ||
Properties `property` and `attribute` are optional. If present | ||
the extracted value is either a property or an attribute of the | ||
node matched by the `selector`. If not present, the text contents | ||
of the element is returned. If `selector` matches nothing then | ||
null is returned. | ||
Property `type` is optional. When not set, `single` is assumed as | ||
the default. | ||
FilterExpression: | ||
```javascript | ||
{ | ||
"exists": CSSSelector | ||
} | ||
``` | ||
An element passes a FilterExpression if it has elements that match | ||
the CSS rule in the `exists` property. | ||
## Testing | ||
@@ -14,0 +113,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
18191
13
409
1
118