fix: a NPE

This commit is contained in:
Zhicheng Wang 2017-07-31 09:29:32 +08:00
parent b17f64b14d
commit 92a739099d
1 changed files with 5 additions and 3 deletions

View File

@ -20,7 +20,7 @@ export class TocService {
constructor( constructor(
@Inject(DOCUMENT) private document: any, @Inject(DOCUMENT) private document: any,
private domSanitizer: DomSanitizer, private domSanitizer: DomSanitizer,
private scrollSpyService: ScrollSpyService) { } private scrollSpyService: ScrollSpyService) { }
genToc(docElement?: Element, docId = '') { genToc(docElement?: Element, docId = '') {
@ -66,13 +66,15 @@ export class TocService {
private findTocHeadings(docElement: Element): HTMLHeadingElement[] { private findTocHeadings(docElement: Element): HTMLHeadingElement[] {
const headings = docElement.querySelectorAll('h1,h2,h3'); const headings = docElement.querySelectorAll('h1,h2,h3');
const skipNoTocHeadings = (heading: HTMLHeadingElement) => !/(?:no-toc|notoc)/i.test(heading.className) && !this.isOriginalText(heading); const skipNoTocHeadings = (heading: HTMLHeadingElement) => {
return !/(?:no-toc|notoc)/i.test(heading.className) && !this.isOriginalText(heading);
};
return Array.prototype.filter.call(headings, skipNoTocHeadings); return Array.prototype.filter.call(headings, skipNoTocHeadings);
} }
private isOriginalText(heading: HTMLHeadingElement): boolean { private isOriginalText(heading: HTMLHeadingElement): boolean {
if (heading.hasAttribute('translation-origin')) { if (heading && heading.hasAttribute('translation-origin')) {
const prevNode = heading.previousElementSibling; const prevNode = heading.previousElementSibling;
if (prevNode.hasAttribute('translation-result')) { if (prevNode.hasAttribute('translation-result')) {
return true; return true;