Skip to content

Commit ee1f515

Browse files
authored
Merge pull request #16 from posthtml/v0.3.0
V0.3.0
2 parents 00b41b0 + 5e1650a commit ee1f515

4 files changed

Lines changed: 58 additions & 6 deletions

File tree

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ Tag objects generally look something like this:
7777

7878
Tag objects can contain three keys. The `tag` key takes the name of the tag as the value. This can include custom tags. The optional `attrs` key takes an object with key/value pairs representing the attributes of the html tag. A boolean attribute has an empty string as its value. Finally, the optional `content` key takes an array as its value, which is a PostHTML AST. In this manner, the AST is a tree that should be walked recursively.
7979

80+
## Options
81+
82+
### `directives`
83+
Type: `Array`
84+
Default: `[{name: '!doctype', start: '<', end: '>'}]`
85+
Description: *Adds processing of custom directives*
86+
8087
## License
8188

8289
[MIT](LICENSE)

index.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ var isObject = require('isobject');
88
*/
99
var defaultOptions = {lowerCaseTags: false, lowerCaseAttributeNames: false};
1010

11+
var defaultDirectives = [{name: '!doctype', start: '<', end: '>'}];
12+
1113
/**
1214
* Parse html to PostHTMLTree
1315
* @param {String} html
@@ -22,12 +24,28 @@ function postHTMLParser(html, options) {
2224
return this[this.length - 1];
2325
};
2426

25-
var parser = new htmlparser.Parser({
26-
onprocessinginstruction: function(name, data) {
27-
if (name.toLowerCase() === '!doctype') {
28-
results.push('<' + data + '>');
27+
function parserDirective(name, data) {
28+
var directives = options.directives || defaultDirectives;
29+
var last = bufArray.last();
30+
31+
for (var i = 0; i < directives.length; i++) {
32+
var directive = directives[i];
33+
var directiveText = directive.start + data + directive.end;
34+
35+
if (name.toLowerCase() === directive.name) {
36+
if (!last) {
37+
results.push(directiveText);
38+
return;
39+
}
40+
41+
last.content || (last.content = []);
42+
last.content.push(directiveText);
2943
}
30-
},
44+
}
45+
}
46+
47+
var parser = new htmlparser.Parser({
48+
onprocessinginstruction: parserDirective,
3149
oncomment: function(data) {
3250
var comment = '<!--' + data + '-->',
3351
last = bufArray.last();
@@ -101,3 +119,4 @@ function parserWrapper() {
101119

102120
module.exports = parserWrapper;
103121
module.exports.defaultOptions = defaultOptions;
122+
module.exports.defaultDirectives = defaultDirectives;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "posthtml-parser",
3-
"version": "0.2.1",
3+
"version": "0.3.0",
44
"description": "Parse HTML/XML to PostHTMLTree",
55
"keywords": [
66
"html",

test/test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,32 @@ describe('PostHTML-Parser test', function() {
6666
expect(parser('<!doctype html>')).to.eql(['<!doctype html>']);
6767
});
6868

69+
it('should be parse directive', function() {
70+
var customDirectives = {directives: [
71+
{name: '?php', start: '<', end: '>'}
72+
]};
73+
74+
expect(parser('<?php echo "Hello word"; ?>', customDirectives)).to.eql(['<?php echo "Hello word"; ?>']);
75+
});
76+
77+
it('should be parse directives and tag', function() {
78+
var customDirectives = {directives: [
79+
{name: '!doctype', start: '<', end: '>'},
80+
{name: '?php', start: '<', end: '>'}
81+
]};
82+
83+
var html = '<!doctype html><html><?php echo \"Hello word\"; ?></html>';
84+
var tree = [
85+
'<!doctype html>',
86+
{
87+
content: ['<?php echo \"Hello word\"; ?>'],
88+
tag: 'html'
89+
}
90+
];
91+
92+
expect(parser(html, customDirectives)).to.eql(tree);
93+
});
94+
6995
it('should be parse tag', function() {
7096
expect(parser('<html></html>')).to.eql([{ tag: 'html' }]);
7197
});

0 commit comments

Comments
 (0)