import * as s from '../src/sanitizer'; import {expect} from 'chai'; const OPTIONS = { host: '', path: '', protocol: '', }; const SAMPLE_OPTIONS = { host: 'nowhere.com', path: '/some-page/', protocol: 'http:', }; describe('attribute class', () => { it('should create an attribute from name and value', () => { let a = s.attribute('href', 'http://nowhere.com'); expect(a.toString()).to.equal('href="http://nowhere.com"'); }); }); describe('basic tag', () => { it('should create a tag from name and attributes', () => { let attrs = [ s.attribute('href', 'http://nowhere.com'), s.attribute('target', '_blank') ]; let t = new s.Tags.Basic('a', attrs); t.writeText('Link text'); expect(t.toString()).to.equal( 'Link text'); }); }); describe('skipped tag', () => { it('should always be an empty string', () => { let attrs = [ s.attribute('href', 'http://nowhere.com'), s.attribute('target', '_blank') ]; let t = new s.Tags.Skipped('a', attrs); t.writeText('Link text'); expect(t.toString()).to.equal(''); }); }); describe('restricted tag', () => { it('should not allow certain attributes', () => { let attrs = [ s.attribute('href', 'http://nowhere.com'), s.attribute('target', '_blank') ]; let t = s.Transforms.restrcitedTag(['href'])(s.tag('a', attrs), OPTIONS); t.writeText('Link text'); expect(t.toString()).to.equal('Link text'); }); it('should create a self-closing tag from name and attributes', () => { let attrs = [ s.attribute('src', 'http://nowhere.com/1.png'), s.attribute('alt', 'image') ]; let t = s.Transforms.chain([ s.Transforms.restrcitedTag(['src']), s.Transforms.selfClosingTag(), ])(s.tag('img', attrs), OPTIONS); t.writeText('Link text'); // should have no effect expect(t.toString()).to.equal(''); }); }); describe('self-closing tag', () => { it('should create a tag from name and attributes', () => { let attrs = [ s.attribute('src', 'http://nowhere.com/1.png'), s.attribute('alt', 'image') ]; let t = new s.Tags.SelfClosing('img', attrs); t.writeText('Link text'); // should have no effect expect(t.toString()).to.equal('image'); }); }); describe('stripped tag', () => { it('should retian only text', () => { let attrs = [ s.attribute('href', 'http://nowhere.com'), s.attribute('target', '_blank') ]; let t = new s.Tags.Stripped('a', attrs); t.writeText('Link text'); expect(t.toString()).to.equal( 'Link text'); }); }); describe('defaultAttrs', () => { it('should create a tagwith default attrs', () => { let attrs = [ s.attribute('href', 'http://nowhere.com'), s.attribute('target', '_blank') ]; let t = s.Transforms.defaultAttrs([ s.attribute('rel', 'nofollow') ])(s.tag('a', attrs), OPTIONS); t.writeText('Link text'); expect(t.toString()).to.equal( 'Link text'); }); }); describe('transformed tag', () => { it('should transform passed tag', () => { let tf = s.Transforms.transformTag('strong'); expect(tf(s.tag('b', []), OPTIONS).toString()).to.equal(''); }); }); describe('transformed attribute', () => { it('should transform attribute value', () => { let tf = s.Transforms.transformAttributes({ 'href': (a, opts) => s.attribute(a.name, opts.host + a.value) }); let result = tf(s.tag('a', [ s.attribute('href', '/1.png'), s.attribute('target', '_blank') ]), SAMPLE_OPTIONS); expect(result.toString()).to.equal(''); }); }); describe('sample html transformation', () => { const INPUT = ` Something

Awesome page

some image

Some BIG text here
With custom font

next
`; let sanitizer = s.sanitizer(); const EXPECTED = '

Awesome page

' + '
some image' + '

Some BIG text here
' + ' With custom font

next
'; it('should sanitize test input', () => { expect(sanitizer.sanitize(INPUT, SAMPLE_OPTIONS)).to.equal(EXPECTED); }); it('should handle https urls', () => { let input = 'Blah'; let result = sanitizer.sanitize(input, SAMPLE_OPTIONS); expect(result).to.equal('Blah'); }); it('should handle mailto: links', () => { let input = 'Blah'; let result = sanitizer.sanitize(input, SAMPLE_OPTIONS); expect(result).to.equal('Blah'); }); it('should retain specials chars', () => { let input = '

some text with non-breakable space

'; let result = sanitizer.sanitize(input); expect(result).to.equal(input); }); it('should create correct url from //-prefixed path', () => { let input = 'Blah'; let result = sanitizer.sanitize(input, { host: 'nowhere.com', path: '/some-page/', protocol: 'http:', }); expect(result).to.equal('Blah'); }); it('should handle plain text', () => { let input = 'В следующем месяце планируется выпустить RTM, поэтому далее под катом разбор некоторых нововведений, которые будут доступны в рамках новой версии: отличия в установке, дефолтные трейс-флаги, новые функции и киллер-фича для анализа плана выполнения'; let result = sanitizer.sanitize(input); expect(result).to.equal(input); }); });