Skip to content

Commit 96c8eaf

Browse files
authored
Merge pull request #21 from pxgrid/topic/strict-openclose
Topic/strict openclose
2 parents 87e3899 + b0b06f7 commit 96c8eaf

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

lib/tokenizer.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,33 @@ function tokenize(str) {
3434
// 空文字は無視します
3535
if (str.length === 0) { return tokens; }
3636

37-
var isCGMD = false;
37+
var isCGMD = null; // or note, column, imgbox, ...
3838
// 全ての行をなめる
3939
while ((line = lines.shift()) !== undefined) {
4040
// CGMDのスタート / エンドでフラグを打つ
4141
if (CGMD_START_RE.exec(line)) {
42-
isCGMD = true;
42+
// ネストしようとした
43+
if (isCGMD !== null)
44+
throw new Error(`[${isCGMD}] is still opened, nesting is not allowed!`);
45+
46+
isCGMD = line.slice(1, -1);
4347
tokens.push(new CGMD_Token());
4448
tokens[tokens.length - 1].addBody(line);
4549
continue;
4650
}
4751
if (CGMD_END_RE.exec(line)) {
48-
isCGMD = false;
52+
// 異なる種類で閉じようとした
53+
if (isCGMD !== line.slice(2, -1))
54+
throw new Error(`[${isCGMD}] should not be closed by ${line}!`);
55+
56+
isCGMD = null;
4957
tokens[tokens.length - 1].addBody(line);
5058
continue;
5159
}
5260

5361
if (
5462
// ふつうのMDでありつつ
55-
isCGMD === false &&
63+
isCGMD === null &&
5664
(
5765
// 最初か
5866
tokens.length === 0 ||
@@ -69,8 +77,8 @@ function tokenize(str) {
6977
}
7078

7179
// 全行見終わってまだこのフラグが立ってるなら閉じ忘れ
72-
if (isCGMD) {
73-
throw new Error('Missing close tag for CGMD!');
80+
if (isCGMD !== null) {
81+
throw new Error(`Missing close tag for [${isCGMD}]!`);
7482
}
7583

7684
return tokens;

test/cgmd/tokenizer.js

+12
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@ describe('#tokenize', function() {
4343
});
4444
});
4545

46+
it('cgmdネストもエラー', function() {
47+
assert.throws(function() {
48+
Tokenizer.tokenize('[demo]\n# Nesting not allowed!\n[imgbox]');
49+
});
50+
});
51+
52+
it('cgmd違うので閉じるとエラー', function() {
53+
assert.throws(function() {
54+
Tokenizer.tokenize('[imgbox]\n# Wrong closing!\n[/column]');
55+
});
56+
});
57+
4658
it('cgmdの規定のタイプ以外は無視', function() {
4759
var tokens = Tokenizer.tokenize([
4860
'```toml',

0 commit comments

Comments
 (0)