Skip to content

Commit c7a45be

Browse files
author
Jiang Shang
committed
bugfix 循环的 key 不是数值是字符串,fxxk
1 parent 7779a7a commit c7a45be

File tree

4 files changed

+35
-31
lines changed

4 files changed

+35
-31
lines changed

Diff for: dist/Parser.js

+11-9
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ var Parser = (function () {
345345
var emptyCount = 0;
346346
// analyze by line
347347
for (var key in lines) {
348+
key = parseInt(key); // ES6 的 bug for key in Array 循环时返回的 key 是字符串,不是 int
348349
var line = lines[key];
349350
// code block is special
350351
if (matches = line.match(/^(\s*)(~|`){3,}([^`~]*)$/i)) {
@@ -399,8 +400,9 @@ var Parser = (function () {
399400
// list
400401
case /^(\s*)((?:[0-9a-z]\.)|\-|\+|\*)\s+/.test(line):
401402
var matches = line.match(/^(\s*)((?:[0-9a-z]\.)|\-|\+|\*)\s+/);
403+
402404
var listSpace = matches[1].length;
403-
var emptyCount = 0;
405+
emptyCount = 0;
404406

405407
// opened
406408
if (this.isBlock('list')) {
@@ -545,7 +547,7 @@ var Parser = (function () {
545547
}
546548

547549
emptyCount++;
548-
} else if (emptyCount == 0) {
550+
} else if (emptyCount === 0) {
549551
this.setBlock(key);
550552
} else {
551553
this.startBlock('normal', key);
@@ -578,7 +580,7 @@ var Parser = (function () {
578580
}
579581
} else {
580582
var block = this.getBlock();
581-
if (!block || !block.length || block[0] !== 'normal') {
583+
if (block === null || block.length === 0 || block[0] !== 'normal') {
582584
this.startBlock('normal', key);
583585
} else {
584586
this.setBlock(key);
@@ -622,12 +624,12 @@ var Parser = (function () {
622624
}
623625

624626
if ('normal' === type) {
625-
// one sigle empty line
626-
if (from === to && lines[from].match(/^\s*$/) && prevBlock && nextBlock) {
627+
// combine two splitted list
628+
if (from === to && lines[from].match(/^\s*$/) && prevBlock.length && nextBlock.length) {
627629
if (prevBlock[0] === 'list' && nextBlock[0] === 'list') {
628630
// combine 3 blocks
629631
blocks[key - 1] = ['list', prevBlock[1], nextBlock[2], null];
630-
array_splice(blocks, key, 2);
632+
blocks.splice(key, 2);
631633
}
632634
}
633635
}
@@ -1022,11 +1024,11 @@ var Parser = (function () {
10221024
value: function parseNormal(lines) {
10231025
var _this3 = this;
10241026

1025-
lines.forEach(function (line, key) {
1026-
lines[key] = _this3.parseInline(line);
1027+
lines = lines.map(function (line) {
1028+
return _this3.parseInline(line);
10271029
});
10281030

1029-
var str = lines.join("\n");
1031+
var str = lines.join("\n").trim();
10301032
str = str.replace(/(\n\s*){2,}/, "</p><p>");
10311033
str = str.replace(/\n/, "<br>");
10321034

Diff for: hyperdown.js

+11-9
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@
391391
var emptyCount = 0;
392392
// analyze by line
393393
for (var key in lines) {
394+
key = parseInt(key); // ES6 的 bug for key in Array 循环时返回的 key 是字符串,不是 int
394395
var line = lines[key];
395396
// code block is special
396397
if (matches = line.match(/^(\s*)(~|`){3,}([^`~]*)$/i)) {
@@ -445,8 +446,9 @@
445446
// list
446447
case /^(\s*)((?:[0-9a-z]\.)|\-|\+|\*)\s+/.test(line):
447448
var matches = line.match(/^(\s*)((?:[0-9a-z]\.)|\-|\+|\*)\s+/);
449+
448450
var listSpace = matches[1].length;
449-
var emptyCount = 0;
451+
emptyCount = 0;
450452

451453
// opened
452454
if (this.isBlock('list')) {
@@ -591,7 +593,7 @@
591593
}
592594

593595
emptyCount++;
594-
} else if (emptyCount == 0) {
596+
} else if (emptyCount === 0) {
595597
this.setBlock(key);
596598
} else {
597599
this.startBlock('normal', key);
@@ -624,7 +626,7 @@
624626
}
625627
} else {
626628
var block = this.getBlock();
627-
if (!block || !block.length || block[0] !== 'normal') {
629+
if (block === null || block.length === 0 || block[0] !== 'normal') {
628630
this.startBlock('normal', key);
629631
} else {
630632
this.setBlock(key);
@@ -668,12 +670,12 @@
668670
}
669671

670672
if ('normal' === type) {
671-
// one sigle empty line
672-
if (from === to && lines[from].match(/^\s*$/) && prevBlock && nextBlock) {
673+
// combine two splitted list
674+
if (from === to && lines[from].match(/^\s*$/) && prevBlock.length && nextBlock.length) {
673675
if (prevBlock[0] === 'list' && nextBlock[0] === 'list') {
674676
// combine 3 blocks
675677
blocks[key - 1] = ['list', prevBlock[1], nextBlock[2], null];
676-
array_splice(blocks, key, 2);
678+
blocks.splice(key, 2);
677679
}
678680
}
679681
}
@@ -1068,11 +1070,11 @@
10681070
value: function parseNormal(lines) {
10691071
var _this3 = this;
10701072

1071-
lines.forEach(function (line, key) {
1072-
lines[key] = _this3.parseInline(line);
1073+
lines = lines.map(function (line) {
1074+
return _this3.parseInline(line);
10731075
});
10741076

1075-
var str = lines.join("\n");
1077+
var str = lines.join("\n").trim();
10761078
str = str.replace(/(\n\s*){2,}/, "</p><p>");
10771079
str = str.replace(/\n/, "<br>");
10781080

Diff for: src/Parser.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ export default class Parser {
295295
let emptyCount = 0
296296
// analyze by line
297297
for (let key in lines) {
298+
key = parseInt(key) // ES6 的 bug for key in Array 循环时返回的 key 是字符串,不是 int
298299
let line = lines[key]
299300
// code block is special
300301
if (matches = line.match(/^(\s*)(~|`){3,}([^`~]*)$/i)) {
@@ -353,8 +354,9 @@ export default class Parser {
353354
// list
354355
case /^(\s*)((?:[0-9a-z]\.)|\-|\+|\*)\s+/.test(line):
355356
let matches = line.match(/^(\s*)((?:[0-9a-z]\.)|\-|\+|\*)\s+/)
357+
356358
let listSpace = matches[1].length
357-
let emptyCount = 0
359+
emptyCount = 0
358360

359361
// opened
360362
if (this.isBlock('list')) {
@@ -481,7 +483,7 @@ export default class Parser {
481483
}
482484

483485
emptyCount++
484-
} else if (emptyCount == 0) {
486+
} else if (emptyCount === 0) {
485487
this.setBlock(key)
486488
} else {
487489
this.startBlock('normal', key)
@@ -514,7 +516,7 @@ export default class Parser {
514516
}
515517
} else {
516518
let block = this.getBlock()
517-
if (!block || !block.length || block[0] !== 'normal') {
519+
if (block === null || block.length === 0 || block[0] !== 'normal') {
518520
this.startBlock('normal', key)
519521
} else {
520522
this.setBlock(key)
@@ -535,7 +537,7 @@ export default class Parser {
535537
optimizeBlocks(blocks, lines) {
536538
blocks = this.call('beforeOptimizeBlocks', blocks, lines)
537539

538-
blocks.forEach(function(block, key) {
540+
blocks.forEach( (block, key) => {
539541
let prevBlock = blocks[key - 1] ? blocks[key - 1] : null
540542
let nextBlock = blocks[key + 1] ? blocks[key + 1] : null
541543

@@ -552,13 +554,13 @@ export default class Parser {
552554
}
553555

554556
if ('normal' === type) {
555-
// one sigle empty line
557+
// combine two splitted list
556558
if (from === to && lines[from].match(/^\s*$/)
557-
&& prevBlock && nextBlock) {
559+
&& prevBlock.length && nextBlock.length) {
558560
if (prevBlock[0] === 'list' && nextBlock[0] === 'list') {
559561
// combine 3 blocks
560562
blocks[key - 1] = ['list', prevBlock[1], nextBlock[2], null]
561-
array_splice(blocks, key, 2)
563+
blocks.splice(key, 2)
562564
}
563565
}
564566
}
@@ -840,11 +842,11 @@ export default class Parser {
840842
* @return string
841843
*/
842844
parseNormal(lines) {
843-
lines.forEach((line, key) => {
844-
lines[key] = this.parseInline(line)
845+
lines = lines.map( line => {
846+
return this.parseInline(line)
845847
})
846848

847-
let str = lines.join("\n")
849+
let str = lines.join("\n").trim()
848850
str = str.replace(/(\n\s*){2,}/, "</p><p>")
849851
str = str.replace(/\n/, "<br>")
850852

Diff for: test/test.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('HyperDown.js', function() {
3030

3131
describe('list', function() {
3232
it('ul', function() {
33-
assert.equal('<ul><li><p>list</p></li></ul>', parser.makeHtml('- list'));
33+
assert.equal('<ul><li><p>list</p></li></ul>', parser.makeHtml('\n\n - list'));
3434
});
3535
it('ol', function() {
3636
assert.equal('<ol><li><p>list</p></li></ol>', parser.makeHtml('1. list'));
@@ -81,6 +81,4 @@ describe('HyperDown.js', function() {
8181
assert.equal('<table><thead><tr><th>test</th><th>test</th></tr></thead><thead><tr><th>------</th><th colspan="2">------</th></tr></thead><thead><tr><th>test</th><th>test</th></tr></thead></tbody></table>', parser.makeHtml('test | test\n------ | ------|\ntest | test'));
8282
});
8383
});
84-
85-
8684
});

0 commit comments

Comments
 (0)