Skip to content

Commit 135f30c

Browse files
committed
Fix bug where nodes where added multiple times
Related to get-alex/alex#218.
1 parent 816c4a1 commit 135f30c

File tree

6 files changed

+157
-18
lines changed

6 files changed

+157
-18
lines changed

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ function toNLCST(tree, file, Parser) {
165165
}
166166
} else if (child && start === -1) {
167167
find(child)
168+
start = index + 1
168169
} else {
169170
;(viable ? add : findAll)(children.slice(start, index))
170171

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>🤔</p>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"type": "RootNode",
3+
"children": [
4+
{
5+
"type": "ParagraphNode",
6+
"children": [
7+
{
8+
"type": "SentenceNode",
9+
"children": [
10+
{
11+
"type": "SymbolNode",
12+
"value": "🤔",
13+
"position": {
14+
"start": {
15+
"line": 1,
16+
"column": 4,
17+
"offset": 3
18+
},
19+
"end": {
20+
"line": 1,
21+
"column": 6,
22+
"offset": 5
23+
}
24+
}
25+
}
26+
],
27+
"position": {
28+
"start": {
29+
"line": 1,
30+
"column": 4,
31+
"offset": 3
32+
},
33+
"end": {
34+
"line": 1,
35+
"column": 6,
36+
"offset": 5
37+
}
38+
}
39+
}
40+
],
41+
"position": {
42+
"start": {
43+
"line": 1,
44+
"column": 4,
45+
"offset": 3
46+
},
47+
"end": {
48+
"line": 1,
49+
"column": 6,
50+
"offset": 5
51+
}
52+
}
53+
}
54+
],
55+
"position": {
56+
"start": {
57+
"line": 1,
58+
"column": 1,
59+
"offset": 0
60+
},
61+
"end": {
62+
"line": 1,
63+
"column": 10,
64+
"offset": 9
65+
}
66+
}
67+
}

test/fixtures/eof-eol/input.html

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

test/fixtures/eof-eol/output.json

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"type": "RootNode",
3+
"children": [
4+
{
5+
"type": "ParagraphNode",
6+
"children": [
7+
{
8+
"type": "SentenceNode",
9+
"children": [
10+
{
11+
"type": "SymbolNode",
12+
"value": "🤔",
13+
"position": {
14+
"start": {
15+
"line": 1,
16+
"column": 4,
17+
"offset": 3
18+
},
19+
"end": {
20+
"line": 1,
21+
"column": 6,
22+
"offset": 5
23+
}
24+
}
25+
}
26+
],
27+
"position": {
28+
"start": {
29+
"line": 1,
30+
"column": 4,
31+
"offset": 3
32+
},
33+
"end": {
34+
"line": 1,
35+
"column": 6,
36+
"offset": 5
37+
}
38+
}
39+
}
40+
],
41+
"position": {
42+
"start": {
43+
"line": 1,
44+
"column": 4,
45+
"offset": 3
46+
},
47+
"end": {
48+
"line": 1,
49+
"column": 6,
50+
"offset": 5
51+
}
52+
}
53+
}
54+
],
55+
"position": {
56+
"start": {
57+
"line": 1,
58+
"column": 1,
59+
"offset": 0
60+
},
61+
"end": {
62+
"line": 2,
63+
"column": 1,
64+
"offset": 10
65+
}
66+
}
67+
}

test/index.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,6 @@ var negate = require('negate')
1212
var hidden = require('is-hidden')
1313
var toNLCST = require('..')
1414

15-
var read = fs.readFileSync
16-
var join = path.join
17-
18-
var ROOT = join(__dirname, 'fixtures')
19-
20-
var fixtures = fs.readdirSync(ROOT)
21-
2215
test('hast-util-to-nlcst', function(t) {
2316
t.throws(
2417
function() {
@@ -146,17 +139,26 @@ test('hast-util-to-nlcst', function(t) {
146139
})
147140

148141
test('Fixtures', function(t) {
149-
fixtures.filter(negate(hidden)).forEach(function(fixture) {
150-
var filepath = join(ROOT, fixture)
151-
var input = vfile(read(join(filepath, 'input.html')))
152-
var output = read(join(filepath, 'output.json'))
153-
154-
t.deepEqual(
155-
toNLCST(rehype().parse(input), input, Latin),
156-
JSON.parse(output),
157-
'should work on `' + fixture + '`'
158-
)
159-
})
142+
var root = path.join(__dirname, 'fixtures')
143+
144+
fs.readdirSync(root)
145+
.filter(negate(hidden))
146+
.forEach(function(fixture) {
147+
var input = path.join(root, fixture, 'input.html')
148+
var output = path.join(root, fixture, 'output.json')
149+
var file = vfile(fs.readFileSync(input))
150+
var actual = toNLCST(rehype().parse(file), file, Latin)
151+
var expected
152+
153+
try {
154+
expected = JSON.parse(fs.readFileSync(output))
155+
} catch (error) {
156+
fs.writeFileSync(output, JSON.stringify(actual, null, 2) + '\n')
157+
return
158+
}
159+
160+
t.deepEqual(actual, expected, 'should work on `' + fixture + '`')
161+
})
160162

161163
t.end()
162164
})

0 commit comments

Comments
 (0)