From 9f6a50e486d1b919da5435e3dd09d21883e74421 Mon Sep 17 00:00:00 2001 From: czippel Date: Thu, 27 Apr 2017 10:56:43 +0200 Subject: [PATCH 1/2] added load configurations for cheerio --- README.md | 9 +++++++-- dom-processor.js | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) 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); From 8a3d6b56992fdba55baa5d94462cb32b698aef5b Mon Sep 17 00:00:00 2001 From: czippel Date: Tue, 2 May 2017 08:00:31 +0200 Subject: [PATCH 2/2] added simple test for load configuration --- test/load-configuration.test.js | 59 +++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 test/load-configuration.test.js 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
with and decode entities', function() { + configLoader.loadConfigurations = {decodeEntities: true}; + var result = processor.process('
'); + + assert(result === '&&'); + }); + + it('should replace
with and not decode entities', function() { + configLoader.loadConfigurations = {decodeEntities: false}; + var result = processor.process('
'); + + assert(result === '&&'); + }); + + it('should leave unmatched elements unchanged and decode entities', function() { + configLoader.loadConfigurations = {decodeEntities: true}; + var result = processor.process('

&&

'); + + assert(result === '

&&

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

&&

'); + + assert(result === '

&&

'); + }); +});