Skip to content

Commit 3722a22

Browse files
committed
Working version for tags without attributes
1 parent a9cdb08 commit 3722a22

File tree

5 files changed

+13
-3
lines changed

5 files changed

+13
-3
lines changed

index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var Parser = require('htmlparser2').Parser;
2+
var isEmpty = require('lodash.isempty');
23

34
module.exports = function() {
45
var parent = [];
@@ -15,10 +16,12 @@ module.exports = function() {
1516
},
1617
onclosetag: function() {
1718
var p = parent.parent;
18-
// Delete elementList if it is empty
19+
// Delete elementList and/or attributes if empty
1920
var lastChild = p[p.length - 1];
20-
if (lastChild[2].length === 0) {
21-
delete lastChild[2];
21+
for (var i = 1; i < 3; i++) {
22+
if (isEmpty(lastChild[i])) {
23+
delete lastChild[i];
24+
}
2225
}
2326
delete parent.parent;
2427
parent = p;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"homepage": "https://github.com/CMTegner/jsonmlify",
2626
"dependencies": {
2727
"htmlparser2": "^3.7.3",
28+
"lodash.isempty": "^2.4.1",
2829
"tape": "^2.13.4"
2930
}
3031
}

test/03-no-attributes/expected.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[["div"]]

test/03-no-attributes/markup.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div></div>

test/test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,7 @@ test('should return an empty array for empty input markup', function(t) {
2121
test('should not set a node-list array on the node if it has no children', function(t) {
2222
verify('02-single-shallow-tag', t);
2323
});
24+
25+
test('should not set a attribute hash on the node if it has none', function(t) {
26+
verify('03-no-attributes', t);
27+
});

0 commit comments

Comments
 (0)