Skip to content

Commit

Permalink
Merge pull request #2 from zett90/master
Browse files Browse the repository at this point in the history
Fix for issue with multiple elements
  • Loading branch information
BenjaminEckardt committed May 16, 2017
2 parents 7be166c + 05cf94d commit d5aa2b8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
12 changes: 9 additions & 3 deletions dom-processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ function DomPocessor(configLoader) {
var $ = cheerio.load(html, configLoader.loadConfigurations);
var configs = configLoader.load(currentPath);
configs.forEach(function(conf) {
var $el = $(conf.selector);
var result = typeof conf.replace === 'function' ? conf.replace($el) : conf.replace;
$el.replaceWith(result);
var $elements = $(conf.selector);
var result;
var $element;
$elements.each(function (i, el) {
$element = $(el);
result = typeof conf.replace === 'function' ? conf.replace($element) : conf.replace;
$element.replaceWith(result);
});

});
return $.html();
};
Expand Down
30 changes: 30 additions & 0 deletions test/multiple-element.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';
var assert = console.assert;
var DomProcessor = require('../dom-processor');

describe('multiple element test (replace function should get the corresponding element)', function() {

var configLoader = {
load: function() {
return [{
selector: 'div',
replace: function($element) {
return $element.attr('text');
}
}];
}
};

var processor = new DomProcessor(configLoader);

it('should replace <div text="test"> with test', function() {
var result = processor.process('<div text="test"></div>');

assert(result === 'test');
});

it('should replace <div text="test"></div><div text="-secondtest"></div> with test-secondtest', function() {
var result = processor.process('<div text="test"></div><div text="-secondtest"></div>');
assert(result === 'test-secondtest');
});
});

0 comments on commit d5aa2b8

Please sign in to comment.