Skip to content

Commit a370b6f

Browse files
author
Jiang Shang
committed
bugfix: for of 循环在 safari 下报错
1 parent 304706d commit a370b6f

File tree

3 files changed

+80
-142
lines changed

3 files changed

+80
-142
lines changed

hyperdown.js

Lines changed: 52 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,11 @@
111111
}, {
112112
key: 'hook',
113113
value: function hook(type, callback) {
114-
this.hooks[type].push(callback);
114+
if (this.hooks[type]) {
115+
this.hooks[type].push(callback);
116+
} else {
117+
this.hooks[type] = [callback];
118+
}
115119
}
116120

117121
/**
@@ -399,7 +403,7 @@
399403
var emptyCount = 0;
400404
// analyze by line
401405
for (var key in lines) {
402-
key = parseInt(key); // ES6 的 bug for key in Array 循环时返回的 key 是字符串,不是 int
406+
key = parseInt(key); // ES6 的 for key in Array 循环时返回的 key 是字符串,不是 int
403407
var line = lines[key];
404408
// code block is special
405409
if (matches = line.match(/^(\s*)(~|`){3,}([^`~]*)$/i)) {
@@ -492,6 +496,7 @@
492496
// pre
493497
case /^ {4}/.test(line):
494498
emptyCount = 0;
499+
495500
if (this.isBlock('pre')) {
496501
this.setBlock(key);
497502
} else if (this.isBlock('normal')) {
@@ -733,31 +738,11 @@
733738
}, {
734739
key: 'parsePre',
735740
value: function parsePre(lines) {
736-
var _iteratorNormalCompletion2 = true;
737-
var _didIteratorError2 = false;
738-
var _iteratorError2 = undefined;
739-
740-
try {
741-
for (var _iterator2 = lines[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
742-
var line = _step2.value;
743-
744-
line = this.htmlspecialchars(line.substr(4));
745-
}
746-
} catch (err) {
747-
_didIteratorError2 = true;
748-
_iteratorError2 = err;
749-
} finally {
750-
try {
751-
if (!_iteratorNormalCompletion2 && _iterator2['return']) {
752-
_iterator2['return']();
753-
}
754-
} finally {
755-
if (_didIteratorError2) {
756-
throw _iteratorError2;
757-
}
758-
}
759-
}
741+
var _this3 = this;
760742

743+
lines.forEach(function (line, ind) {
744+
lines[ind] = _this3.htmlspecialchars(line.substr(4));
745+
});
761746
var str = lines.join('\n');
762747

763748
return (/^\s*$/.test(str) ? '' : '<pre><code>' + str + '</code></pre>'
@@ -828,6 +813,8 @@
828813
}, {
829814
key: 'parseList',
830815
value: function parseList(lines) {
816+
var _this4 = this;
817+
831818
var html = '';
832819
var minSpace = 99999;
833820
var rows = [];
@@ -848,93 +835,50 @@
848835

849836
var found = false;
850837
var secondMinSpace = 99999;
851-
var _iteratorNormalCompletion3 = true;
852-
var _didIteratorError3 = false;
853-
var _iteratorError3 = undefined;
854-
855-
try {
856-
for (var _iterator3 = rows[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
857-
var row = _step3.value;
858-
859-
if (Array.isArray(row) && row[0] != minSpace) {
860-
secondMinSpace = Math.min(secondMinSpace, row[0]);
861-
found = true;
862-
}
863-
}
864-
} catch (err) {
865-
_didIteratorError3 = true;
866-
_iteratorError3 = err;
867-
} finally {
868-
try {
869-
if (!_iteratorNormalCompletion3 && _iterator3['return']) {
870-
_iterator3['return']();
871-
}
872-
} finally {
873-
if (_didIteratorError3) {
874-
throw _iteratorError3;
875-
}
838+
rows.forEach(function (row) {
839+
if (Array.isArray(row) && row[0] != minSpace) {
840+
secondMinSpace = Math.min(secondMinSpace, row[0]);
841+
found = true;
876842
}
877-
}
878-
843+
});
879844
secondMinSpace = found ? 0 : minSpace;
880845

881846
var lastType = '';
882847
var leftLines = [];
883848

884-
var _iteratorNormalCompletion4 = true;
885-
var _didIteratorError4 = false;
886-
var _iteratorError4 = undefined;
887-
888-
try {
889-
for (var _iterator4 = rows[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
890-
var row = _step4.value;
891-
892-
if (Array.isArray(row)) {
893-
var _row = _slicedToArray(row, 4);
849+
rows.forEach(function (row) {
850+
if (Array.isArray(row)) {
851+
var _row = _slicedToArray(row, 4);
894852

895-
var space = _row[0];
896-
var type = _row[1];
897-
var line = _row[2];
898-
var text = _row[3];
853+
var space = _row[0];
854+
var type = _row[1];
855+
var line = _row[2];
856+
var text = _row[3];
899857

900-
if (space !== minSpace) {
901-
var pattern = new RegExp("^\s{" + secondMinSpace + "}");
902-
leftLines.push(line.replace(pattern, ''));
903-
} else {
904-
if (lastType !== type) {
905-
if (lastType.length) {
906-
html += '</' + lastType + '>';
907-
}
908-
909-
html += '<' + type + '>';
858+
if (space !== minSpace) {
859+
var pattern = new RegExp("^\s{" + secondMinSpace + "}");
860+
leftLines.push(line.replace(pattern, ''));
861+
} else {
862+
if (lastType !== type) {
863+
if (lastType.length) {
864+
html += '</' + lastType + '>';
910865
}
911866

912-
if (leftLines.length) {
913-
html += "<li>" + this.parse(leftLines.join("\n")) + "</li>";
914-
}
867+
html += '<' + type + '>';
868+
}
915869

916-
leftLines = [text];
917-
lastType = type;
870+
if (leftLines.length) {
871+
html += "<li>" + _this4.parse(leftLines.join("\n")) + "</li>";
918872
}
919-
} else {
920-
var pattern = new RegExp("^\s{" + secondMinSpace + "}");
921-
leftLines.push(row.replace(pattern, ''));
922-
}
923-
}
924-
} catch (err) {
925-
_didIteratorError4 = true;
926-
_iteratorError4 = err;
927-
} finally {
928-
try {
929-
if (!_iteratorNormalCompletion4 && _iterator4['return']) {
930-
_iterator4['return']();
931-
}
932-
} finally {
933-
if (_didIteratorError4) {
934-
throw _iteratorError4;
873+
874+
leftLines = [text];
875+
lastType = type;
935876
}
877+
} else {
878+
var pattern = new RegExp("^\s{" + secondMinSpace + "}");
879+
leftLines.push(row.replace(pattern, ''));
936880
}
937-
}
881+
});
938882

939883
if (leftLines.length) {
940884
html += "<li>" + this.parse(leftLines.join("\n")) + ('</li></' + lastType + '>');
@@ -951,7 +895,7 @@
951895
}, {
952896
key: 'parseTable',
953897
value: function parseTable(lines, value) {
954-
var _this3 = this;
898+
var _this5 = this;
955899

956900
var _value = _slicedToArray(value, 2);
957901

@@ -1030,7 +974,7 @@
1030974
html += ' align="' + aligns[key] + '"';
1031975
}
1032976

1033-
html += '>' + _this3.parseInline(text) + ('</' + tag + '>');
977+
html += '>' + _this5.parseInline(text) + ('</' + tag + '>');
1034978
});
1035979

1036980
html += '</tr>';
@@ -1076,10 +1020,10 @@
10761020
}, {
10771021
key: 'parseNormal',
10781022
value: function parseNormal(lines) {
1079-
var _this4 = this;
1023+
var _this6 = this;
10801024

10811025
lines = lines.map(function (line) {
1082-
return _this4.parseInline(line);
1026+
return _this6.parseInline(line);
10831027
});
10841028

10851029
var str = lines.join("\n").trim();
@@ -1137,30 +1081,11 @@
11371081
}, {
11381082
key: 'parseHtml',
11391083
value: function parseHtml(lines, type) {
1140-
var _iteratorNormalCompletion5 = true;
1141-
var _didIteratorError5 = false;
1142-
var _iteratorError5 = undefined;
1143-
1144-
try {
1145-
for (var _iterator5 = lines[Symbol.iterator](), _step5; !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) {
1146-
var line = _step5.value;
1084+
var _this7 = this;
11471085

1148-
line = this.parseInline(line, this.specialWhiteList[type] ? this.specialWhiteList[type] : '');
1149-
}
1150-
} catch (err) {
1151-
_didIteratorError5 = true;
1152-
_iteratorError5 = err;
1153-
} finally {
1154-
try {
1155-
if (!_iteratorNormalCompletion5 && _iterator5['return']) {
1156-
_iterator5['return']();
1157-
}
1158-
} finally {
1159-
if (_didIteratorError5) {
1160-
throw _iteratorError5;
1161-
}
1162-
}
1163-
}
1086+
lines.forEach(function (line) {
1087+
line = _this7.parseInline(line, _this7.specialWhiteList[type] ? _this7.specialWhiteList[type] : '');
1088+
});
11641089

11651090
return lines.join("\n");
11661091
}
@@ -1331,7 +1256,7 @@
13311256
prev[2] = current[2];
13321257
this.blocks[this.pos - 1] = prev;
13331258
this.current = prev[0];
1334-
unset(this.blocks[this.pos]);
1259+
this.blocks.splice(this.pos, 1);
13351260
this.pos--;
13361261

13371262
return this;

src/Parser.js

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ export default class Parser {
4242
* @param callback
4343
*/
4444
hook (type, callback) {
45-
this.hooks[type].push(callback)
45+
if(this.hooks[type]) {
46+
this.hooks[type].push(callback)
47+
} else {
48+
this.hooks[type] = [callback]
49+
}
4650
}
4751

4852
/**
@@ -303,7 +307,7 @@ export default class Parser {
303307
let emptyCount = 0
304308
// analyze by line
305309
for (let key in lines) {
306-
key = parseInt(key) // ES6 的 bug for key in Array 循环时返回的 key 是字符串,不是 int
310+
key = parseInt(key) // ES6 的 for key in Array 循环时返回的 key 是字符串,不是 int
307311
let line = lines[key]
308312
// code block is special
309313
if (matches = line.match(/^(\s*)(~|`){3,}([^`~]*)$/i)) {
@@ -313,7 +317,7 @@ export default class Parser {
313317

314318
if (isAfterList) {
315319
this.combineBlock()
316-
.setBlock(key);
320+
.setBlock(key)
317321
} else {
318322
this.setBlock(key)
319323
.endBlock()
@@ -401,6 +405,7 @@ export default class Parser {
401405
// pre
402406
case /^ {4}/.test(line):
403407
emptyCount = 0
408+
404409
if (this.isBlock('pre')) {
405410
this.setBlock(key)
406411
} else if (this.isBlock('normal')) {
@@ -611,12 +616,12 @@ export default class Parser {
611616
* @return string
612617
*/
613618
parsePre(lines) {
614-
for (let line of lines) {
615-
line = this.htmlspecialchars(line.substr(4))
616-
}
619+
lines.forEach( (line, ind) => {
620+
lines[ind] = this.htmlspecialchars(line.substr(4))
621+
})
617622
let str = lines.join('\n')
618623

619-
return /^\s*$/.test(str) ? '' : '<pre><code>' + str + '</code></pre>';
624+
return /^\s*$/.test(str) ? '' : '<pre><code>' + str + '</code></pre>'
620625
}
621626

622627
/**
@@ -692,18 +697,18 @@ export default class Parser {
692697

693698
let found = false
694699
let secondMinSpace = 99999
695-
for (let row of rows) {
700+
rows.forEach( row => {
696701
if (Array.isArray(row) && row[0] != minSpace) {
697702
secondMinSpace = Math.min(secondMinSpace, row[0])
698703
found = true
699704
}
700-
}
705+
})
701706
secondMinSpace = found ? 0 : minSpace
702707

703708
let lastType = ''
704709
let leftLines = []
705710

706-
for (let row of rows) {
711+
rows.forEach( row => {
707712
if (Array.isArray(row)) {
708713
let [space, type, line, text] = row
709714

@@ -730,7 +735,7 @@ export default class Parser {
730735
let pattern = new RegExp("^\s{" + secondMinSpace + "}")
731736
leftLines.push(row.replace(pattern, ''))
732737
}
733-
}
738+
})
734739

735740
if (leftLines.length) {
736741
html += "<li>" + this.parse(leftLines.join("\n")) + `</li></${lastType}>`
@@ -898,10 +903,10 @@ export default class Parser {
898903
* @return string
899904
*/
900905
parseHtml(lines, type) {
901-
for (let line of lines) {
906+
lines.forEach(line => {
902907
line = this.parseInline(line,
903908
this.specialWhiteList[type] ? this.specialWhiteList[type] : '')
904-
}
909+
})
905910

906911
return lines.join("\n")
907912
}
@@ -1044,7 +1049,7 @@ export default class Parser {
10441049
prev[2] = current[2]
10451050
this.blocks[this.pos - 1] = prev
10461051
this.current = prev[0]
1047-
unset(this.blocks[this.pos])
1052+
this.blocks.splice(this.pos, 1)
10481053
this.pos--
10491054

10501055
return this

test/test.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,15 @@ describe('HyperDown.js', function() {
8585

8686
describe('footnote', function() {
8787
it('脚注 ', function() {
88-
assert.equal('', parser.makeHtml('[^demo]: 这是一个示例脚注。'));
88+
var footnote = 'Never write "[click here][^2]".\n [^2]: http://www.w3.org/QA/Tips/noClickHere';
89+
assert.equal('demo', parser.makeHtml(footnote));
90+
});
91+
});
92+
93+
describe('complex', function() {
94+
it('list + code', function() {
95+
var codeInList = "1. 如下代码片段中的空检查是否有意义?\n Question * q = new Question;\n if (q == NULL ) {\n }\n 从上面我们了解到,\`operator new\` 在分配内存失败的情况下会调用 \`new_handler\` 尝试让系统释放点内存,然后再次尝试申请内存。如果这时系统中内存确实紧张,即使调用。";
96+
assert.equal('', parser.makeHtml(codeInList));
8997
});
9098
});
9199
});

0 commit comments

Comments
 (0)