Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve HTML comments within html block #282

Merged
merged 4 commits into from
Jan 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### Fixed

- Refactor auto scaling component ([#276](https://github.com/marp-team/marp-core/pull/276))
- Preserve HTML comments within html block ([#282](https://github.com/marp-team/marp-core/pull/282))

### Changed

Expand Down
33 changes: 31 additions & 2 deletions src/html/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,26 @@ export function markdown(md): void {
(original: (...args: any[]) => string) =>
(...args) => {
const ret = original(...args)

// Pick comments
const splitted: string[] = []
let pos = 0

while (pos < ret.length) {
const startIdx = ret.indexOf('<!--', pos)
let endIdx = startIdx !== -1 ? ret.indexOf('-->', startIdx + 4) : -1

if (endIdx === -1) {
splitted.push(ret.slice(pos))
break
}

endIdx += 3
splitted.push(ret.slice(pos, startIdx), ret.slice(startIdx, endIdx))
pos = endIdx
}

// Apply filter to each contents by XSS
const allowList = {}
const html: MarpOptions['html'] = md.options.html

Expand Down Expand Up @@ -58,8 +78,17 @@ export function markdown(md): void {
},
})

const sanitized = filter.process(ret)
return md.options.xhtmlOut ? xhtmlOutFilter.process(sanitized) : sanitized
return splitted
.map((part, idx) => {
if (idx % 2 === 1) return part

const sanitized = filter.process(part)

return md.options.xhtmlOut
? xhtmlOutFilter.process(sanitized)
: sanitized
})
.join('')
}

md.renderer.rules.html_inline = sanitizedRenderer(html_inline)
Expand Down
50 changes: 50 additions & 0 deletions test/marp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,56 @@ describe('Marp', () => {
expect($('header > strong')).toHaveLength(1)
expect($('footer > em')).toHaveLength(1)
})

it('keeps raw HTML comments within valid HTML block', () => {
const { html: $script, comments: comments$script } = marp().render(
"<script><!--\nconst script = '<b>test</b>'\n--></script>"
)
expect($script).toContain("const script = '<b>test</b>'")
expect(comments$script[0]).toHaveLength(0)

// Complex comment
const complexComment = `
<!--
function matchwo(a,b)
{

if (a < b && a < 0) then {
return 1;

} else {

return 0;
}
}

// ex
-->
`.trim()
const { html: $complex } = marp().render(
`<script>${complexComment}</script>`
)
expect($complex).toContain(complexComment)

// NOTE: Marpit framework will collect the comment block if the whole of HTML block was comment
const { html: $comment, comments: comments$comment } = marp().render(
"<!--\nconst script = '<b>test</b>'\n-->"
)
expect($comment).not.toContain("const script = '<b>test</b>'")
expect(comments$comment[0]).toHaveLength(1)
})

it('sanitizes CDATA section', () => {
// HTML Living Standard denys using CDATA in HTML context so must be sanitized
const cdata = `
<![CDATA[
<p>XSS</p>
<script>alert('XSS')</script>
]]>
`.trim()
const { html } = marp().render(cdata)
expect(html).not.toContain(cdata)
})
})

describe('with true', () => {
Expand Down