Skip to content

Commit 0b07ee8

Browse files
fix(regression): remove overly strict invalid tag format check in Swig parser (#5691)
Co-authored-by: Mimi <1119186082@qq.com>
1 parent 573c70f commit 0b07ee8

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

lib/hexo/post.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,7 @@ class PostRenderEscape {
151151
swig_string_quote = '';
152152
}
153153
}
154-
// {% } or {% %
155-
if (((char !== '%' && next_char === '}') || (char === '%' && next_char !== '}')) && swig_string_quote === '') {
156-
// From swig back to plain text
157-
swig_tag_name = '';
158-
state = STATE_PLAINTEXT;
159-
pushAndReset(`{%${str.slice(buffer_start, idx)}${char}`);
160-
} else if (char === '%' && next_char === '}' && swig_string_quote === '') { // From swig back to plain text
154+
if (char === '%' && next_char === '}' && swig_string_quote === '') { // From swig back to plain text
161155
idx++;
162156
if (swig_tag_name !== '' && str.includes(`end${swig_tag_name}`)) {
163157
state = STATE_SWIG_FULL_TAG;

test/scripts/hexo/post.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,6 +1525,53 @@ describe('Post', () => {
15251525
data.content.should.contains('22222');
15261526
});
15271527

1528+
it('render() - tags with swig character', async () => {
1529+
const tagSpy = spy();
1530+
hexo.extend.tag.register('testTag', (args, content) => {
1531+
tagSpy(args, content);
1532+
return '';
1533+
}, {
1534+
ends: true
1535+
});
1536+
let content = '{% testTag 111 222 %}\n3333\n{% endtestTag %}';
1537+
await post.render('', {
1538+
content,
1539+
engine: 'markdown'
1540+
});
1541+
tagSpy.calledOnce.should.be.true;
1542+
tagSpy.firstCall.args[0].should.eql(['111', '222']);
1543+
tagSpy.firstCall.args[1].should.eql('3333');
1544+
1545+
content = '{% testTag 111% % 222 %}\n333\n{% endtestTag %}';
1546+
await post.render('', {
1547+
content,
1548+
engine: 'markdown'
1549+
});
1550+
tagSpy.calledTwice.should.be.true;
1551+
tagSpy.secondCall.args[0].should.eql(['111%', '%', '222']);
1552+
tagSpy.secondCall.args[1].should.eql('333');
1553+
1554+
content = '{% testTag 111 } 222} %}\n333\n{% endtestTag %}';
1555+
await post.render('', {
1556+
content,
1557+
engine: 'markdown'
1558+
});
1559+
tagSpy.calledThrice.should.be.true;
1560+
tagSpy.thirdCall.args[0].should.eql(['111', '}', '222}']);
1561+
tagSpy.thirdCall.args[1].should.eql('333');
1562+
1563+
content = '{% testTag 111 222 %}\n333% % } %}\n{% endtestTag %}';
1564+
await post.render('', {
1565+
content,
1566+
engine: 'markdown'
1567+
});
1568+
tagSpy.callCount.should.eql(4);
1569+
tagSpy.getCall(3).args[0].should.eql(['111', '222']);
1570+
tagSpy.getCall(3).args[1].should.eql('333% % } %}');
1571+
1572+
hexo.extend.tag.unregister('testTag');
1573+
});
1574+
15281575
it('render() - incomplete tags throw error', async () => {
15291576
const content = 'nunjucks should throw {# } error';
15301577

0 commit comments

Comments
 (0)