Skip to content

Commit 2cdc3e6

Browse files
committed
Allow pass options to parser
Add wrapper over posthtml-parser as requested in posthtml/posthtml/pull/150#issuecomment-232707133; Add tests to cover possible wrapper issues; Add isobject dependency
1 parent e90adc2 commit 2cdc3e6

File tree

3 files changed

+84
-7
lines changed

3 files changed

+84
-7
lines changed

index.js

+31-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
'use strict';
22

33
var htmlparser = require('htmlparser2');
4+
var isObject = require('isobject');
5+
6+
/**
7+
* @see https://github.com/fb55/htmlparser2/wiki/Parser-options
8+
*/
9+
var defaultOptions = {lowerCaseTags: false};
410

511
/**
612
* Parse html to PostHTMLTree
713
* @param {String} html
8-
* @return {Object}
14+
* @param {Object} [options=defaultOptions]
15+
* @return {PostHTMLTree}
916
*/
10-
module.exports = function postHTMLParser(html) {
17+
function postHTMLParser(html, options) {
1118
var bufArray = [],
1219
results = [];
1320

@@ -67,10 +74,30 @@ module.exports = function postHTMLParser(html) {
6774
last.content || (last.content = []);
6875
last.content.push(text);
6976
}
70-
}, {lowerCaseTags: false});
77+
}, options || defaultOptions);
7178

7279
parser.write(html);
7380
parser.end();
7481

7582
return results;
76-
};
83+
}
84+
85+
function parserWrapper() {
86+
var option;
87+
88+
function parser(html) {
89+
var opt = option || defaultOptions;
90+
return postHTMLParser(html, opt);
91+
}
92+
93+
if (arguments.length === 1 && isObject(arguments[0])) {
94+
option = arguments[0];
95+
return parser;
96+
}
97+
98+
option = arguments[1];
99+
return parser(arguments[0]);
100+
}
101+
102+
module.exports = parserWrapper;
103+
module.exports.defaultOptions = defaultOptions;

package.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,17 @@
2525
},
2626
"homepage": "https://github.com/posthtml/posthtml-parser#readme",
2727
"dependencies": {
28-
"htmlparser2": "^3.8.3"
28+
"htmlparser2": "^3.8.3",
29+
"isobject": "^2.1.0"
2930
},
3031
"devDependencies": {
3132
"chai": "^3.3.0",
3233
"istanbul": "^0.4.0",
3334
"jscs": "^2.3.5",
3435
"jshint": "^2.8.0",
35-
"mocha": "^2.3.3"
36+
"mocha": "^2.3.3",
37+
"rewire": "^2.5.2",
38+
"sinon": "^1.17.4",
39+
"sinon-chai": "^2.8.0"
3640
}
3741
}

test/test.js

+47-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,55 @@
11
var parser = require('..');
2+
var parserWithMockedDeps = require('rewire')('..');
23
var describe = require('mocha').describe;
34
var it = require('mocha').it;
4-
var expect = require('chai').expect;
5+
var beforeEach = require('mocha').beforeEach;
6+
var chai = require('chai');
7+
var sinon = require('sinon');
8+
var expect = chai.expect;
9+
chai.use(require('sinon-chai'));
510

611
describe('PostHTML-Parser test', function() {
12+
describe('Call signatures', function() {
13+
var customOptions = {lowerCaseTags: false, lowerCaseAttributeNames: false};
14+
var MockedHtmlParser2;
15+
var parserSpy;
16+
17+
beforeEach(function() {
18+
// jscs:disable requireFunctionDeclarations
19+
MockedHtmlParser2 = function() {};
20+
MockedHtmlParser2.prototype = {
21+
write: function() {},
22+
end: function() {}
23+
};
24+
// jscs:enable requireFunctionDeclarations
25+
26+
// Create spy on mocked htmlparser2 to collect call stats
27+
parserSpy = sinon.spy(MockedHtmlParser2);
28+
29+
// Replace real htmlparser2 dependency of posthtml-parser with mocked
30+
parserWithMockedDeps.__set__({
31+
htmlparser: {Parser: parserSpy}
32+
});
33+
});
34+
35+
it('should use default options when called with 1 param', function() {
36+
parserWithMockedDeps('');
37+
expect(parserSpy.firstCall.args[1]).to.eql(parser.defaultOptions);
38+
});
39+
40+
it('should use custom options when called with 2 params', function() {
41+
parserWithMockedDeps('', customOptions);
42+
expect(parserSpy.firstCall.args[1]).to.eql(customOptions);
43+
});
44+
45+
it('should use custom params when called as factory function', function() {
46+
var factory = parserWithMockedDeps(customOptions);
47+
expect(factory).to.be.a('function');
48+
expect(factory('')).to.be.an('array');
49+
expect(parserSpy.firstCall.args[1]).to.eql(customOptions);
50+
});
51+
});
52+
753
it('should be parse doctype in uppercase', function() {
854
expect(parser('<!DOCTYPE html>')).to.eql(['<!DOCTYPE html>']);
955
});

0 commit comments

Comments
 (0)