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

Add sanitize HTML #66

Merged
merged 2 commits into from
Aug 18, 2018
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
24 changes: 16 additions & 8 deletions lib/src/markdown.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { SecurityContext } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { BrowserModule, DomSanitizer } from '@angular/platform-browser';
import { parse } from 'marked';

import { MarkdownService } from './markdown.service';
Expand All @@ -9,12 +11,16 @@ import { MarkedOptions } from './marked-options';
declare var Prism: any;

describe('MarkdowService', () => {
let domSanitizer: DomSanitizer;
let http: HttpTestingController;
let markdownService: MarkdownService;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
imports: [
BrowserModule,
HttpClientTestingModule,
],
providers: [
{ provide: MarkedOptions, useValue: {} },
MarkdownService,
Expand All @@ -23,6 +29,7 @@ describe('MarkdowService', () => {
});

beforeEach(() => {
domSanitizer = TestBed.get(DomSanitizer);
http = TestBed.get(HttpTestingController);
markdownService = TestBed.get(MarkdownService);
});
Expand All @@ -40,8 +47,9 @@ describe('MarkdowService', () => {
it('should return parsed markdown correctly', () => {

const mockRaw = '### Markdown-x';
const expected = domSanitizer.sanitize(SecurityContext.HTML, parse(mockRaw));

expect(markdownService.compile(mockRaw)).toBe(parse(mockRaw));
expect(markdownService.compile(mockRaw)).toBe(expected);
});

it('should return empty string when raw is null/undefined/empty', () => {
Expand All @@ -59,13 +67,13 @@ describe('MarkdowService', () => {
' * sub-list', // keep indent while removing from previous row offset
].join('\n');

const expected = [
const expected = domSanitizer.sanitize(SecurityContext.HTML, parse([
'',
'* list',
' * sub-list',
].join('\n');
].join('\n')));

expect(markdownService.compile(mockRaw)).toBe(parse(expected));
expect(markdownService.compile(mockRaw)).toBe(expected);
});

it('should return line with indent correctly', () => {
Expand All @@ -77,14 +85,14 @@ describe('MarkdowService', () => {
'Lorem Ipsum', // keep everthing else
].join('\n');

const expected = [
const expected = domSanitizer.sanitize(SecurityContext.HTML, parse([
'* list',
' * sub-list',
'',
'Lorem Ipsum',
].join('\n');
].join('\n')));

expect(markdownService.compile(mockRaw)).toBe(parse(expected));
expect(markdownService.compile(mockRaw)).toBe(expected);
});
});

Expand Down
7 changes: 5 additions & 2 deletions lib/src/markdown.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { HttpClient } from '@angular/common/http';
import { Injectable, Optional } from '@angular/core';
import { Injectable, Optional, SecurityContext } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
import { parse, Renderer } from 'marked';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
Expand All @@ -24,6 +25,7 @@ export class MarkdownService {

constructor(
@Optional() private http: HttpClient,
private domSanitizer: DomSanitizer,
public options: MarkedOptions,
) {
if (!this.renderer) {
Expand All @@ -33,7 +35,8 @@ export class MarkdownService {

compile(markdown: string, markedOptions = this.options): string {
const precompiled = this.precompile(markdown);
return parse(precompiled, markedOptions);
const compiled = parse(precompiled, markedOptions);
return this.domSanitizer.sanitize(SecurityContext.HTML, compiled);
}

getSource(src: string): Observable<string> {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"build:lib": "ng build lib",
"postbuild:lib": "cpx ./README.md ./dist/lib",
"link:lib": "rimraf node_modules/ngx-markdown && linklocal",
"lint": "yarn lint:lib && yarn lint:demo",
"lint:demo": "ng lint demo",
"lint:lib": "ng lint lib",
"lint:ci": "ng lint lib --format checkstyle > tslint.xml",
Expand Down