Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

grunt-dom-munger

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

grunt-dom-munger - npm Package Compare versions

Comparing version 1.0.1 to 2.0.0

test/expected/index_cheerio.html

16

Gruntfile.js

@@ -33,2 +33,3 @@ /*

options: {
engine:'jsdom',
read: {selector:'link',attribute:'href',writeto:'mylinks',isPath:true},

@@ -47,2 +48,17 @@ remove: '#removeMe',

},
test_cheerio: {
options: {
read: {selector:'link',attribute:'href',writeto:'mylinks',isPath:true},
remove: '#removeMe',
update: {selector:'html',attribute:'appmode',value:'production'},
append: {selector:'body',html:'<div id="appended">Im being appended</div>'},
prepend: {selector:'body',html:'<span>Im being prepended</span>'},
text: {selector:'title',text:'CHANGED TITLE'},
callback: function($){
$('#sample2').text('Ive been updated via callback');
}
},
src: 'test/fixtures/index.html',
dest: 'tmp/index_cheerio.html'
}
},

@@ -49,0 +65,0 @@ concat: {

9

package.json
{
"name": "grunt-dom-munger",
"description": "Read and manipulate HTML with jsdom and jquery. Ex. read <script> tags from your html. Remove nodes, add nodes, and more.",
"version": "1.0.1",
"description": "Read and manipulate HTML with jquery (or cheerio). Ex. read <script> tags from your html. Remove nodes, add nodes, and more.",
"version": "2.0.0",
"homepage": "https://github.com/cgross/grunt-dom-munger",

@@ -46,5 +46,6 @@ "author": {

],
"dependencies": {
"jsdom": "~0.5.5"
"optionalDependencies": {
"jsdom": "~0.5.5",
"cheerio": "~0.12.0"
}
}
# grunt-dom-munger
> Read and manipulate HTML documents with jsdom and jquery.
> Read and manipulate HTML documents with jsdom+jquery or cheerio.

@@ -184,2 +184,20 @@ Use this task to read and transform your HTML documents. Typical use cases include:

#### options.engine
By default, `dom-munger` uses `cheerio` to read and manipulate the DOM. Prior to version 2, `dom-munger` used 'jsdom' and `jquery` which provided more features but was harder to install on Windows and was a bit heavier. To fallback to using `jsdom`+`jquery`, specify the `engine` option.
```js
grunt.initConfig({
dom_munger: {
your_target: {
options: {
engine: 'jsdom',
...
},
src: 'index.html',
dest: 'dist/index.html'
}
}
})
```
## Full End-to-End Example for Concatentation and Minification

@@ -249,4 +267,5 @@

* v2.0.0 - Moved to `cheerio` engine. Upgraded jquery to v2.
* v1.0.1 - `remove` moved to the second to last operation performed (only `callback` is later).
* v1.0.0 - Read task modified to write values to `dom_munger.data` rather than to write directly to a task config.
* v0.1.0 - Initial release.

@@ -10,13 +10,111 @@ /*

var jsdom = require("jsdom");
var jsdom; //only init-ed when needed = require("jsdom");
var path = require('path');
var fs = require('fs');
var cheerio;// only init-ed when needed = require('cheerio');
module.exports = function(grunt) {
var jqueryContents = fs.readFileSync(path.join(__dirname,'../vendor/jquery-1.9.1.min.js'));
var jqueryContents;
var processFile = function(f,dest,options,$,window){
grunt.log.subhead('Processing ' + f.cyan);
var updated = false;
if (options.read){
if (!options.read.selector || !options.read.attribute || !options.read.writeto){
grunt.log.error('Read config missing selector, attribute, and/or writeto options');
} else {
var vals = $(options.read.selector).map(function(i,elem){
return $(elem).attr(options.read.attribute);
});
if (options.engine === 'jsdom'){
vals = vals.toArray();
}
if (options.read.isPath){
var relativeTo = path.dirname(grunt.file.expand(f));
vals = vals.map(function(val){
return path.join(relativeTo,val);
});
}
grunt.config(['dom_munger','data',options.read.writeto],vals);
grunt.log.writeln('Wrote ' + (options.read.selector + '.' + options.read.attribute).cyan + ' to ' + ('dom_munger.data.'+options.read.writeto).cyan);
}
}
if (options.update){
if (!options.update.selector || !options.update.attribute || !options.update.value){
grunt.log.error('Update config missing selector, attribute, and/or value options');
} else {
$(options.update.selector).attr(options.update.attribute,options.update.value);
grunt.log.writeln('Updated ' + options.update.attribute.cyan + ' to ' + options.update.value.cyan);
updated = true;
}
}
if (options.append){
if (!options.append.selector || !options.append.html){
grunt.log.error('Append config missing selector and/or html options');
} else {
$(options.append.selector).append(options.append.html);
grunt.log.writeln("Appended to " + options.append.selector.cyan);
updated = true;
}
}
if (options.prepend){
if (!options.prepend.selector || !options.prepend.html){
grunt.log.error('Prepend config missing selector and/or html options');
} else {
$(options.prepend.selector).prepend(options.prepend.html);
grunt.log.writeln("Prepended to " + options.prepend.selector.cyan);
updated = true;
}
}
if (options.text){
if (!options.text.selector || !options.text.text){
grunt.log.error('Text config missing selector and/or text options');
} else {
$(options.text.selector).text(options.text.text);
grunt.log.writeln('Applied text to ' + options.text.selector.cyan);
updated = true;
}
}
if (options.remove){
$(options.remove).remove();
grunt.log.writeln('Removed ' + options.remove.cyan);
updated = true;
}
if (options.callback){
options.callback($);
//just assume its updating something
updated = true;
}
if (updated){
var updatedContents;
if (options.engine === 'cheerio'){
updatedContents = $.html()
} else {
updatedContents = window.document.doctype.toString()+window.document.innerHTML;
}
grunt.file.write(dest || f,updatedContents);
grunt.log.writeln('File ' + (dest || f).cyan + ' created/updated.');
}
};
grunt.registerMultiTask('dom_munger', 'Read and manipulate html.', function() {
var options = this.options({
engine:'cheerio'
});

@@ -31,2 +129,7 @@ var done = this.async();

if (['jsdom','cheerio'].indexOf(options.engine) === -1){
grunt.log.error('Engine options must be either jsdom or cheerio.');
done(false);
}
this.files.forEach(function(f) {

@@ -49,100 +152,44 @@

jsdom.env({
html: srcContents,
src: [jqueryContents],
done: function process(errors,window){
if (options.engine === 'cheerio'){
grunt.log.subhead('Processing ' + f.cyan);
if (!cheerio){
cheerio = require('cheerio');
}
var $ = window.$;
var updated = false;
var $ = cheerio.load(srcContents);
processFile(f,dest,options,$);
if (options.read){
if (!options.read.selector || !options.read.attribute || !options.read.writeto){
grunt.log.error('Read config missing selector, attribute, and/or writeto options');
} else {
var vals = $.map($(options.read.selector),function(elem){
return $(elem).attr(options.read.attribute);
});
countdown --;
if (countdown === 0){
done();
}
if (options.read.isPath){
var relativeTo = path.dirname(grunt.file.expand(f));
vals = $.map(vals,function(val){
return path.join(relativeTo,val);
});
}
} else {
grunt.config(['dom_munger','data',options.read.writeto],vals);
grunt.log.writeln('Wrote ' + (options.read.selector + '.' + options.read.attribute).cyan + ' to ' + ('dom_munger.data.'+options.read.writeto).cyan);
}
}
if (!jsdom){
jsdom = require('jsdom');
jqueryContents = fs.readFileSync(path.join(__dirname,'../vendor/jquery-2.0.2.min.js'));
}
if (options.update){
if (!options.update.selector || !options.update.attribute || !options.update.value){
grunt.log.error('Update config missing selector, attribute, and/or value options');
} else {
$(options.update.selector).attr(options.update.attribute,options.update.value);
grunt.log.writeln('Updated ' + options.update.attribute.cyan + ' to ' + options.update.value.cyan);
updated = true;
}
}
jsdom.env({
html: srcContents,
src: [jqueryContents],
done: function process(errors,window){
if (options.append){
if (!options.append.selector || !options.append.html){
grunt.log.error('Append config missing selector and/or html options');
} else {
$(options.append.selector).append(options.append.html);
grunt.log.writeln("Appended to " + options.append.selector.cyan);
updated = true;
}
}
processFile(f,dest,options,window.$,window);
if (options.prepend){
if (!options.prepend.selector || !options.prepend.html){
grunt.log.error('Prepend config missing selector and/or html options');
} else {
$(options.prepend.selector).prepend(options.prepend.html);
grunt.log.writeln("Prepended to " + options.prepend.selector.cyan);
updated = true;
}
}
countdown --;
if (countdown === 0){
done();
}
if (options.text){
if (!options.text.selector || !options.text.text){
grunt.log.error('Text config missing selector and/or text options');
} else {
$(options.text.selector).text(options.text.text);
grunt.log.writeln('Applied text to ' + options.text.selector.cyan);
updated = true;
}
}
});
}
if (options.remove){
$(options.remove).remove();
grunt.log.writeln('Removed ' + options.remove.cyan);
updated = true;
}
if (options.callback){
options.callback($);
//just assume its updating something
updated = true;
}
if (updated){
var updatedContents = window.document.doctype.toString()+window.document.innerHTML;
grunt.file.write(dest || f,updatedContents);
grunt.log.writeln('File ' + (dest || f).cyan + ' created/updated.');
}
countdown --;
if (countdown === 0){
done();
}
}
});
});
});
});
};

@@ -48,2 +48,11 @@ 'use strict';

},
cheerio: function(test) {
test.expect(1);
var actual = grunt.file.read('tmp/index_cheerio.html');
var expected = grunt.file.read('test/expected/index_cheerio.html');
test.equal(actual, expected, 'should update the html file with CHEERIO correctly.');
test.done();
}
};
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc