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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ Tag objects generally look something like this:

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.

## Options

### `directives`
Type: `Array`
Default: `[{name: '!doctype', start: '<', end: '>'}]`
Description: *Adds processing of custom directives*

## License

[MIT](LICENSE)
29 changes: 24 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ var isObject = require('isobject');
*/
var defaultOptions = {lowerCaseTags: false, lowerCaseAttributeNames: false};

var defaultDirectives = [{name: '!doctype', start: '<', end: '>'}];

/**
* Parse html to PostHTMLTree
* @param {String} html
Expand All @@ -22,12 +24,28 @@ function postHTMLParser(html, options) {
return this[this.length - 1];
};

var parser = new htmlparser.Parser({
onprocessinginstruction: function(name, data) {
if (name.toLowerCase() === '!doctype') {
results.push('<' + data + '>');
function parserDirective(name, data) {
var directives = options.directives || defaultDirectives;
var last = bufArray.last();

for (var i = 0; i < directives.length; i++) {
var directive = directives[i];
var directiveText = directive.start + data + directive.end;

if (name.toLowerCase() === directive.name) {
if (!last) {
results.push(directiveText);
return;
}

last.content || (last.content = []);
last.content.push(directiveText);
}
},
}
}

var parser = new htmlparser.Parser({
onprocessinginstruction: parserDirective,
oncomment: function(data) {
var comment = '<!--' + data + '-->',
last = bufArray.last();
Expand Down Expand Up @@ -101,3 +119,4 @@ function parserWrapper() {

module.exports = parserWrapper;
module.exports.defaultOptions = defaultOptions;
module.exports.defaultDirectives = defaultDirectives;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "posthtml-parser",
"version": "0.2.1",
"version": "0.3.0",
"description": "Parse HTML/XML to PostHTMLTree",
"keywords": [
"html",
Expand Down
26 changes: 26 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,32 @@ describe('PostHTML-Parser test', function() {
expect(parser('<!doctype html>')).to.eql(['<!doctype html>']);
});

it('should be parse directive', function() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, need some test case when <?php ...> nested into any html tag (for example, <div>).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very thanks, i find bug, and fixed him. )

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<?php wp_nav_menu(array('theme_location' => 'primary', 'menu_class' => 'header-nav-bar')); ?>
is not working correctly

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@IgorMotorny Hi, I need more information. Create a separate Issue and describe the problem case. Thanks.

var customDirectives = {directives: [
{name: '?php', start: '<', end: '>'}
]};

expect(parser('<?php echo "Hello word"; ?>', customDirectives)).to.eql(['<?php echo "Hello word"; ?>']);
});

it('should be parse directives and tag', function() {
var customDirectives = {directives: [
{name: '!doctype', start: '<', end: '>'},
{name: '?php', start: '<', end: '>'}
]};

var html = '<!doctype html><html><?php echo \"Hello word\"; ?></html>';
var tree = [
'<!doctype html>',
{
content: ['<?php echo \"Hello word\"; ?>'],
tag: 'html'
}
];

expect(parser(html, customDirectives)).to.eql(tree);
});

it('should be parse tag', function() {
expect(parser('<html></html>')).to.eql([{ tag: 'html' }]);
});
Expand Down