Skip to content

Commit

Permalink
test: reorg tests
Browse files Browse the repository at this point in the history
  • Loading branch information
robertu7 committed Jun 6, 2024
1 parent 0512dfb commit 7019eaf
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 138 deletions.
2 changes: 1 addition & 1 deletion src/transformers/lib/rehypeSqueezeBreaks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const squeezeSoftBreaks = ({
let breakCount = 0

children.forEach((node) => {
if (node.type !== 'element' || node.tagName !== 'br') {
if (!isBr(node)) {
breakCount = 0
newChildren.push(node)
return
Expand Down
124 changes: 62 additions & 62 deletions src/transformers/normalize-sanitize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,68 +129,6 @@ describe('Sanitize and normalize article', () => {
)
})

test('squeeze <br>', () => {
expectProcessArticleHTML(
stripIndent`
<p>1</p>
<p>2</p>
<p>1<br>2</p>
<p>1<br><br>2</p>
<p>1<br><br></p>
`,
stripIndent`
<p>1</p>
<p>2</p>
<p>12</p>
<p>12</p>
<p>1</p>
`,
{ maxHardBreaks: 0, maxSoftBreaks: 0 },
)

// max 1 soft break
expectProcessArticleHTML(
stripIndent`
<p>1</p>
<p>2</p>
<p>1<br>2</p>
<p>1<br><br>2</p>
<p>1<br><br></p>
`,
stripIndent`
<p>1</p>
<p>2</p>
<p>1<br class="smart">2</p>
<p>1<br class="smart">2</p>
<p>1<br class="smart"></p>
`,
{ maxHardBreaks: 0, maxSoftBreaks: 1 },
)

// blockquote
expectProcessArticleHTML(
stripIndent`
<blockquote>
<p>1</p>
<p>2</p>
<p>1<br>2</p>
<p>1<br><br>2</p>
<p>1<br><br></p>
</blockquote>
`,
stripIndent`
<blockquote>
<p>1</p>
<p>2</p>
<p>12</p>
<p>12</p>
<p>1</p>
</blockquote>
`,
{ maxHardBreaks: 0, maxSoftBreaks: 0 },
)
})

test('squeeze and retain all empty paragraphs', () => {
expectProcessArticleHTML(
stripIndent`
Expand Down Expand Up @@ -271,4 +209,66 @@ describe('Sanitize and normalize comment', () => {
`,
)
})

test('squeeze <br>', () => {
expectProcessCommentHTML(
stripIndent`
<p>1</p>
<p>2</p>
<p>1<br>2</p>
<p>1<br><br>2</p>
<p>1<br><br></p>
`,
stripIndent`
<p>1</p>
<p>2</p>
<p>12</p>
<p>12</p>
<p>1</p>
`,
{ maxHardBreaks: 0, maxSoftBreaks: 0 },
)

// max 1 soft break
expectProcessCommentHTML(
stripIndent`
<p>1</p>
<p>2</p>
<p>1<br>2</p>
<p>1<br><br>2</p>
<p>1<br><br></p>
`,
stripIndent`
<p>1</p>
<p>2</p>
<p>1<br class="smart">2</p>
<p>1<br class="smart">2</p>
<p>1<br class="smart"></p>
`,
{ maxHardBreaks: 0, maxSoftBreaks: 1 },
)

// blockquote
expectProcessCommentHTML(
stripIndent`
<blockquote>
<p>1</p>
<p>2</p>
<p>1<br>2</p>
<p>1<br><br>2</p>
<p>1<br><br></p>
</blockquote>
`,
stripIndent`
<blockquote>
<p>1</p>
<p>2</p>
<p>12</p>
<p>12</p>
<p>1</p>
</blockquote>
`,
{ maxHardBreaks: 0, maxSoftBreaks: 0 },
)
})
})
171 changes: 96 additions & 75 deletions src/transformers/sanitize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,6 @@ describe('Sanitization: custom', () => {
{ maxHardBreaks: 0 },
)

// blockquote
expectSanitizeHTML(
stripIndent`
<blockquote>
<p>1</p>
<p>2</p>
<p></p>
<p>3</p>
</blockquote>
`,
stripIndent`
<blockquote>
<p>1</p>
<p>2</p>
<p>3</p>
</blockquote>
`,
{ maxHardBreaks: 0 },
)

// max 1 empty paragraph
expectSanitizeHTML(
stripIndent`
Expand Down Expand Up @@ -120,65 +100,25 @@ describe('Sanitization: custom', () => {
)
})

test('squeeze <br>', () => {
expectSanitizeHTML(
stripIndent`
<p>1</p>
<p>2</p>
<p>1<br>2</p>
<p>1<br><br>2</p>
<p>1<br><br></p>
`,
stripIndent`
<p>1</p>
<p>2</p>
<p>12</p>
<p>12</p>
<p>1</p>
`,
{ maxHardBreaks: 0, maxSoftBreaks: 0 },
)

// max 1 soft break
expectSanitizeHTML(
stripIndent`
<p>1</p>
<p>2</p>
<p>1<br>2</p>
<p>1<br><br>2</p>
<p>1<br><br></p>
`,
stripIndent`
<p>1</p>
<p>2</p>
<p>1<br>2</p>
<p>1<br>2</p>
<p>1<br></p>
`,
{ maxHardBreaks: 0, maxSoftBreaks: 1 },
)

test('squeeze empty paragraphs in blockquote', () => {
// blockquote
expectSanitizeHTML(
stripIndent`
<blockquote>
<p>1</p>
<p>2</p>
<p>1<br>2</p>
<p>1<br><br>2</p>
<p>1<br><br></p>
</blockquote>
`,
<blockquote>
<p>1</p>
<p>2</p>
<p></p>
<p>3</p>
</blockquote>
`,
stripIndent`
<blockquote>
<p>1</p>
<p>2</p>
<p>12</p>
<p>12</p>
<p>1</p>
</blockquote>
`,
{ maxHardBreaks: 0, maxSoftBreaks: 0 },
<blockquote>
<p>1</p>
<p>2</p>
<p>3</p>
</blockquote>
`,
{ maxHardBreaks: 0 },
)
})

Expand Down Expand Up @@ -258,6 +198,87 @@ describe('Sanitization: custom', () => {
`,
)
})

test('squeeze <br>', () => {
expectSanitizeHTML(
stripIndent`
<p>1</p>
<p>2</p>
<p>1<br>2</p>
<p>1<br><br>2</p>
<p>1<br><br></p>
`,
stripIndent`
<p>1</p>
<p>2</p>
<p>12</p>
<p>12</p>
<p>1</p>
`,
{ maxHardBreaks: 0, maxSoftBreaks: 0 },
)

// max 1 soft break
expectSanitizeHTML(
stripIndent`
<p>1</p>
<p>2</p>
<p>1<br>2</p>
<p>1<br><br>2</p>
<p>1<br><br></p>
`,
stripIndent`
<p>1</p>
<p>2</p>
<p>1<br>2</p>
<p>1<br>2</p>
<p>1<br></p>
`,
{ maxHardBreaks: 0, maxSoftBreaks: 1 },
)

// retain all
expectSanitizeHTML(
stripIndent`
<p>1</p>
<p>2</p>
<p>1<br>2</p>
<p>1<br><br>2</p>
<p>1<br><br></p>
`,
stripIndent`
<p>1</p>
<p>2</p>
<p>1<br>2</p>
<p>1<br><br>2</p>
<p>1<br><br></p>
`,
{ maxHardBreaks: 0, maxSoftBreaks: -1 },
)

// blockquote
expectSanitizeHTML(
stripIndent`
<blockquote>
<p>1</p>
<p>2</p>
<p>1<br>2</p>
<p>1<br><br>2</p>
<p>1<br><br></p>
</blockquote>
`,
stripIndent`
<blockquote>
<p>1</p>
<p>2</p>
<p>12</p>
<p>12</p>
<p>1</p>
</blockquote>
`,
{ maxHardBreaks: 0, maxSoftBreaks: 0 },
)
})
})

// via https://github.com/leizongmin/js-xss/blob/master/test/test_xss.js
Expand Down

0 comments on commit 7019eaf

Please sign in to comment.