Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for issue with multiple elements #2

Merged
merged 3 commits into from
May 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
});
});