diff --git a/README.md b/README.md index c45bf3a..c10e1af 100644 --- a/README.md +++ b/README.md @@ -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 [{ @@ -50,8 +52,11 @@ function load(filePath) { }]; } +let loadConfigurations = {decodeEntities: false}; + module.exports = { - load: load + load: load, + loadConfigurations: loadConfigurations }; ``` diff --git a/dom-processor.js b/dom-processor.js index 5c871a8..cae9cf2 100644 --- a/dom-processor.js +++ b/dom-processor.js @@ -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); diff --git a/test/load-configuration.test.js b/test/load-configuration.test.js new file mode 100644 index 0000000..b922208 --- /dev/null +++ b/test/load-configuration.test.js @@ -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 '&&'; + } + }]; + } + }; + + var processor = new DomProcessor(configLoader); + + it('should decode entities (replace & with &)', function() { + configLoader.loadConfigurations = {decodeEntities: true}; + var result = processor.process('&&'); + assert(result === '&&'); + }); + + it('should not decode entities (not replace & with &)', function() { + configLoader.loadConfigurations = {decodeEntities: false}; + var result = processor.process('&&'); + assert(result === '&&'); + }); + + it('should replace
&&
'); + + assert(result === '&&
'); + }); + + it('should leave unmatched elements unchanged and not decode entities', function() { + configLoader.loadConfigurations = {decodeEntities: false}; + var result = processor.process('&&
'); + + assert(result === '&&
'); + }); +});