Skip to content

Commit 4ef0602

Browse files
committed
Strict check for open/close
1 parent 87e3899 commit 4ef0602

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-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/renderer/md/code.js

+13
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ describe('#code', function() {
2222
var expect = '<pre><code class="lang-html">hoge</code></pre>\n';
2323
assert.equal(html, expect);
2424
});
25+
it('言語とタイトル指定が正しい場合は拡張したやつ', function() {
26+
var html1 = renderer.render('```#title\nhoge\n```');
27+
var expect = '' +
28+
'<section class="CG2-livecode">\n' +
29+
'<header class="CG2-livecode__header">\n' +
30+
'<div class="CG2-livecode__label">title</div>\n' +
31+
'</header>\n' +
32+
'<div class="CG2-livecode__body">' +
33+
'<pre><code>hoge</code></pre>\n' +
34+
'</div>\n' +
35+
'</section>\n';
36+
assert.equal(html1, expect);
37+
});
2538
it('言語とタイトル指定が正しい場合は拡張したやつ', function() {
2639
var html1 = renderer.render('```html#title\nhoge\n```');
2740
// スペースがあっても気にしない

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)