fix: strip_html for multi line <script>/<style>/comments, #70

This commit is contained in:
Harttle
2023-03-02 23:01:15 +08:00
parent d4e519c4a6
commit 42d25902e8
3 changed files with 27 additions and 1 deletions
+1 -1
View File
@@ -32,5 +32,5 @@ export function newline_to_br (v: string) {
} }
export function strip_html (v: string) { export function strip_html (v: string) {
return stringify(v).replace(/<script.*?<\/script>|<!--.*?-->|<style.*?<\/style>|<.*?>/g, '') return stringify(v).replace(/<script[\s\S]*?<\/script>|<style[\s\S]*?<\/style>|<.*?>|<!--[\s\S]*?-->/g, '')
} }
+10
View File
@@ -390,4 +390,14 @@ describe('Issues', function () {
const html = await liquid.parseAndRender(tpl, ctx) const html = await liquid.parseAndRender(tpl, ctx)
expect(html.trim()).to.equal('<a href="https://example.com">Lot more code here</a>') expect(html.trim()).to.equal('<a href="https://example.com">Lot more code here</a>')
}) })
it('#70 strip multiline content of <style>', async() => {
const str = `
<style type="text/css">
.test-one-line {display: none;}
</style>`
const engine = new Liquid()
const template = '{{ str | strip_html }}'
const html = await engine.parseAndRender(template, { str })
expect(html).to.match(/^\s*$/)
})
}) })
+16
View File
@@ -1,6 +1,10 @@
import { test } from '../../stub/render' import { test } from '../../stub/render'
import { expect } from 'chai'
import { Liquid } from '../../../src/liquid'
describe('filters/html', function () { describe('filters/html', function () {
let liquid: Liquid
beforeEach(() => liquid = new Liquid())
describe('escape', function () { describe('escape', function () {
it('should escape \' and &', function () { it('should escape \' and &', function () {
return test('{{ "Have you read \'James & the Giant Peach\'?" | escape }}', return test('{{ "Have you read \'James & the Giant Peach\'?" | escape }}',
@@ -43,14 +47,26 @@ describe('filters/html', function () {
return test('{{ "<!--Have you read-->Ulysses?" | strip_html }}', return test('{{ "<!--Have you read-->Ulysses?" | strip_html }}',
'Ulysses?') 'Ulysses?')
}) })
it('should strip multiline comments', function () {
expect(liquid.parseAndRenderSync('{{"<!--foo\r\nbar \ncoo\t \r\n -->"|strip_html}}')).to.equal('')
})
it('should strip all style tags and their contents', function () { it('should strip all style tags and their contents', function () {
return test('{{ "<style>cite { font-style: italic; }</style><cite>Ulysses<cite>?" | strip_html }}', return test('{{ "<style>cite { font-style: italic; }</style><cite>Ulysses<cite>?" | strip_html }}',
'Ulysses?') 'Ulysses?')
}) })
it('should strip multiline styles', function () {
expect(liquid.parseAndRenderSync('{{"<style> \n.header {\r\n color: black;\r\n}\n</style>" | strip_html}}')).to.equal('')
})
it('should strip all scripts tags and their contents', function () { it('should strip all scripts tags and their contents', function () {
return test('{{ "<script async>console.log(\'hello world\')</script><cite>Ulysses<cite>?" | strip_html }}', return test('{{ "<script async>console.log(\'hello world\')</script><cite>Ulysses<cite>?" | strip_html }}',
'Ulysses?') 'Ulysses?')
}) })
it('should strip multiline scripts', function () {
expect(liquid.parseAndRenderSync('{{ "<script> \nfoo\r\nbar\n</script>" | strip_html }}')).to.equal('')
})
it('should not strip non-matched <script>', function () {
expect(liquid.parseAndRenderSync('{{ "<script></script>text<script></script>" | strip_html }}')).to.equal('text')
})
it('should strip until empty', function () { it('should strip until empty', function () {
return test('{{"<br/><br />< p ></p></ p >" | strip_html }}', '') return test('{{"<br/><br />< p ></p></ p >" | strip_html }}', '')
}) })