From 05a79218edc4c95d46c7be8f5af8b17f752d5061 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytautas=20=C5=A0altenis?= Date: Mon, 14 Dec 2015 20:23:49 +0200 Subject: [PATCH 1/4] Implement support for CDATA section Fixes #165. --- block.go | 58 ++++++++++++++++++++++++++++++++++----------------- block_test.go | 17 +++++++++++++++ 2 files changed, 56 insertions(+), 19 deletions(-) diff --git a/block.go b/block.go index b5b0841..4a3b68e 100644 --- a/block.go +++ b/block.go @@ -320,6 +320,11 @@ func (p *parser) html(out *bytes.Buffer, data []byte, doRender bool) int { return size } + // check for HTML CDATA + if size := p.htmlCDATA(out, data, doRender); size > 0 { + return size + } + // no special case recognized return 0 } @@ -397,12 +402,10 @@ func (p *parser) html(out *bytes.Buffer, data []byte, doRender bool) int { return i } -// HTML comment, lax form -func (p *parser) htmlComment(out *bytes.Buffer, data []byte, doRender bool) int { - i := p.inlineHtmlComment(out, data) - // needs to end with a blank line - if j := p.isEmpty(data[i:]); j > 0 { - size := i + j +func (p *parser) renderHtmlBlock(out *bytes.Buffer, data []byte, start int, doRender bool) int { + // html block needs to end with a blank line + if i := p.isEmpty(data[start:]); i > 0 { + size := start + i if doRender { // trim trailing newlines end := size @@ -416,6 +419,35 @@ func (p *parser) htmlComment(out *bytes.Buffer, data []byte, doRender bool) int return 0 } +// HTML comment, lax form +func (p *parser) htmlComment(out *bytes.Buffer, data []byte, doRender bool) int { + i := p.inlineHtmlComment(out, data) + return p.renderHtmlBlock(out, data, i, doRender) +} + +// HTML CDATA section +func (p *parser) htmlCDATA(out *bytes.Buffer, data []byte, doRender bool) int { + cdataTag := "') { + i++ + } + i++ + // no end-of-comment marker + if i >= len(data) { + return 0 + } + return p.renderHtmlBlock(out, data, i, doRender) +} + // HR, which is the only self-closing block tag considered func (p *parser) htmlHr(out *bytes.Buffer, data []byte, doRender bool) int { if data[0] != '<' || (data[1] != 'h' && data[1] != 'H') || (data[2] != 'r' && data[2] != 'R') { @@ -432,19 +464,7 @@ func (p *parser) htmlHr(out *bytes.Buffer, data []byte, doRender bool) int { } if data[i] == '>' { - i++ - if j := p.isEmpty(data[i:]); j > 0 { - size := i + j - if doRender { - // trim newlines - end := size - for end > 0 && data[end-1] == '\n' { - end-- - } - p.r.BlockHtml(out, data[:end]) - } - return size - } + return p.renderHtmlBlock(out, data, i+1, doRender) } return 0 diff --git a/block_test.go b/block_test.go index b33c257..b95edc2 100644 --- a/block_test.go +++ b/block_test.go @@ -1530,3 +1530,20 @@ func TestBlockComments(t *testing.T) { } doTestsBlock(t, tests, 0) } + +func TestCDATA(t *testing.T) { + var tests = []string{ + "Some text\n\n\n", + "

Some text

\n\n\n", + + "CDATA ]]\n\n\n", + "

CDATA ]]

\n\n\n", + + "CDATA >\n\n]]>\n", + "

CDATA >

\n\n]]>\n", + + "Lots of text\n\n\n", + "

Lots of text

\n\n\n", + } + doTestsBlock(t, tests, 0) +} From 594d923645085ba482d4341bff4d17ee7d9fab02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytautas=20=C5=A0altenis?= Date: Wed, 16 Dec 2015 20:51:44 +0200 Subject: [PATCH 2/4] Convert constant variables into consts --- block.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/block.go b/block.go index 4a3b68e..9ccffaf 100644 --- a/block.go +++ b/block.go @@ -427,8 +427,8 @@ func (p *parser) htmlComment(out *bytes.Buffer, data []byte, doRender bool) int // HTML CDATA section func (p *parser) htmlCDATA(out *bytes.Buffer, data []byte, doRender bool) int { - cdataTag := " Date: Wed, 16 Dec 2015 20:53:40 +0200 Subject: [PATCH 3/4] Add a few more CDATA tests --- block_test.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/block_test.go b/block_test.go index b95edc2..60da6ca 100644 --- a/block_test.go +++ b/block_test.go @@ -1544,6 +1544,27 @@ func TestCDATA(t *testing.T) { "Lots of text\n\n\n", "

Lots of text

\n\n\n", + + "]]>\n", + "]]>\n", } doTestsBlock(t, tests, 0) + doTestsBlock(t, []string{ + "``` html\n\n```\n", + "
<![CDATA[foo]]>\n
\n", + + "\n", + "\n", + + ` def func(): +> pass +]]> +`, + ` def func(): +> pass +]]> +`, + }, EXTENSION_FENCED_CODE) } From ee63ffd3e233a02a3d4ab121bc6d5897d67df38e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytautas=20=C5=A0altenis?= Date: Fri, 25 Dec 2015 13:04:56 +0200 Subject: [PATCH 4/4] Fix initialisms in function names --- block.go | 10 +++++----- inline.go | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/block.go b/block.go index 9ccffaf..7fb472e 100644 --- a/block.go +++ b/block.go @@ -402,7 +402,7 @@ func (p *parser) html(out *bytes.Buffer, data []byte, doRender bool) int { return i } -func (p *parser) renderHtmlBlock(out *bytes.Buffer, data []byte, start int, doRender bool) int { +func (p *parser) renderHTMLBlock(out *bytes.Buffer, data []byte, start int, doRender bool) int { // html block needs to end with a blank line if i := p.isEmpty(data[start:]); i > 0 { size := start + i @@ -421,8 +421,8 @@ func (p *parser) renderHtmlBlock(out *bytes.Buffer, data []byte, start int, doRe // HTML comment, lax form func (p *parser) htmlComment(out *bytes.Buffer, data []byte, doRender bool) int { - i := p.inlineHtmlComment(out, data) - return p.renderHtmlBlock(out, data, i, doRender) + i := p.inlineHTMLComment(out, data) + return p.renderHTMLBlock(out, data, i, doRender) } // HTML CDATA section @@ -445,7 +445,7 @@ func (p *parser) htmlCDATA(out *bytes.Buffer, data []byte, doRender bool) int { if i >= len(data) { return 0 } - return p.renderHtmlBlock(out, data, i, doRender) + return p.renderHTMLBlock(out, data, i, doRender) } // HR, which is the only self-closing block tag considered @@ -464,7 +464,7 @@ func (p *parser) htmlHr(out *bytes.Buffer, data []byte, doRender bool) int { } if data[i] == '>' { - return p.renderHtmlBlock(out, data, i+1, doRender) + return p.renderHTMLBlock(out, data, i+1, doRender) } return 0 diff --git a/inline.go b/inline.go index 044ae70..7dac6b8 100644 --- a/inline.go +++ b/inline.go @@ -575,7 +575,7 @@ func link(p *parser, out *bytes.Buffer, data []byte, offset int) int { return i } -func (p *parser) inlineHtmlComment(out *bytes.Buffer, data []byte) int { +func (p *parser) inlineHTMLComment(out *bytes.Buffer, data []byte) int { if len(data) < 5 { return 0 } @@ -599,7 +599,7 @@ func leftAngle(p *parser, out *bytes.Buffer, data []byte, offset int) int { data = data[offset:] altype := LINK_TYPE_NOT_AUTOLINK end := tagLength(data, &altype) - if size := p.inlineHtmlComment(out, data); size > 0 { + if size := p.inlineHTMLComment(out, data); size > 0 { end = size } if end > 2 {