Skip to content
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
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ console.log(result);
```

### config-loader
An object with a `load`-method which is called with the path of the currently processed file.
An object with a `load`-method which is called with the path of the currently processed file and additional load configurations for cheerio.

The `load`-method returns an `Array` of replacement-configurations. Each replacement has two properties:
- `selector`: [Selector](https://github.com/cheeriojs/cheerio#selectors) of the elements to be replaced.
- `replace`: Function to create and return the new element. It is called with matching [element](https://github.com/cheeriojs/cheerio#attributes) as first argument.

The `loadConfigurations`-object cointains configurations for cheerio

```js
function load(filePath) {
return [{
Expand All @@ -50,8 +52,11 @@ function load(filePath) {
}];
}

let loadConfigurations = {decodeEntities: false};

module.exports = {
load: load
load: load,
loadConfigurations: loadConfigurations
};
```

Expand Down
2 changes: 1 addition & 1 deletion dom-processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var cheerio = require('cheerio');

function DomPocessor(configLoader) {
this.process = function(html, currentPath) {
var $ = cheerio.load(html);
var $ = cheerio.load(html, configLoader.loadConfigurations);
var configs = configLoader.load(currentPath);
configs.forEach(function(conf) {
var $el = $(conf.selector);
Expand Down
59 changes: 59 additions & 0 deletions test/load-configuration.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict';
var assert = console.assert;
var DomProcessor = require('../dom-processor');

describe('load configuration', function() {

var configLoader = {
load: function() {
return [{
selector: 'div',
replace: function() {
return '<span>&&</span>';
}
}];
}
};

var processor = new DomProcessor(configLoader);

it('should decode entities (replace & with &amp;)', function() {
configLoader.loadConfigurations = {decodeEntities: true};
var result = processor.process('&&');
assert(result === '&amp;&amp;');
});

it('should not decode entities (not replace & with &amp;)', function() {
configLoader.loadConfigurations = {decodeEntities: false};
var result = processor.process('&&');
assert(result === '&&');
});

it('should replace <div> with <span> and decode entities', function() {
configLoader.loadConfigurations = {decodeEntities: true};
var result = processor.process('<div></div>');

assert(result === '<span>&amp;&amp;</span>');
});

it('should replace <div> with <span> and not decode entities', function() {
configLoader.loadConfigurations = {decodeEntities: false};
var result = processor.process('<div></div>');

assert(result === '<span>&&</span>');
});

it('should leave unmatched elements unchanged and decode entities', function() {
configLoader.loadConfigurations = {decodeEntities: true};
var result = processor.process('<p>&&</p>');

assert(result === '<p>&amp;&amp;</p>');
});

it('should leave unmatched elements unchanged and not decode entities', function() {
configLoader.loadConfigurations = {decodeEntities: false};
var result = processor.process('<p>&&</p>');

assert(result === '<p>&&</p>');
});
});