@@ -52,10 +52,13 @@ export class Diff2HtmlUI {
5252
5353 synchronisedScroll ( ) : void {
5454 this . targetElement . querySelectorAll ( '.d2h-file-wrapper' ) . forEach ( wrapper => {
55- const [ left , right ] = [ ] . slice . call ( wrapper . querySelectorAll ( '.d2h-file-side-diff' ) ) as HTMLElement [ ] ;
55+ const [ left , right ] = Array < Element > ( ) . slice . call ( wrapper . querySelectorAll ( '.d2h-file-side-diff' ) ) ;
56+
5657 if ( left === undefined || right === undefined ) return ;
58+
5759 const onScroll = ( event : Event ) : void => {
5860 if ( event === null || event . target === null ) return ;
61+
5962 if ( event . target === left ) {
6063 right . scrollTop = left . scrollTop ;
6164 right . scrollLeft = left . scrollLeft ;
@@ -70,29 +73,28 @@ export class Diff2HtmlUI {
7073 }
7174
7275 fileListToggle ( startVisible : boolean ) : void {
73- const hashTag = this . getHashTag ( ) ;
74-
75- const showBtn = this . targetElement . querySelector ( '.d2h-show' ) as HTMLElement ;
76- const hideBtn = this . targetElement . querySelector ( '.d2h-hide' ) as HTMLElement ;
77- const fileList = this . targetElement . querySelector ( '.d2h-file-list' ) as HTMLElement ;
76+ const showBtn : HTMLElement | null = this . targetElement . querySelector ( 'd2h-show' ) ;
77+ const hideBtn : HTMLElement | null = this . targetElement . querySelector ( '.d2h-hide' ) ;
78+ const fileList : HTMLElement | null = this . targetElement . querySelector ( '.d2h-file-list' ) ;
7879
7980 if ( showBtn === null || hideBtn === null || fileList === null ) return ;
8081
81- function show ( ) : void {
82+ const show : ( ) => void = ( ) => {
8283 showBtn . style . display = 'none' ;
8384 hideBtn . style . display = 'inline' ;
8485 fileList . style . display = 'block' ;
85- }
86+ } ;
8687
87- function hide ( ) : void {
88+ const hide : ( ) => void = ( ) => {
8889 showBtn . style . display = 'inline' ;
8990 hideBtn . style . display = 'none' ;
9091 fileList . style . display = 'none' ;
91- }
92+ } ;
9293
9394 showBtn . addEventListener ( 'click' , ( ) => show ( ) ) ;
9495 hideBtn . addEventListener ( 'click' , ( ) => hide ( ) ) ;
9596
97+ const hashTag = this . getHashTag ( ) ;
9698 if ( hashTag === 'files-summary-show' ) show ( ) ;
9799 else if ( hashTag === 'files-summary-hide' ) hide ( ) ;
98100 else if ( startVisible ) show ( ) ;
@@ -117,11 +119,11 @@ export class Diff2HtmlUI {
117119 if ( this . hljs === null ) return ;
118120
119121 const text = line . textContent ;
120- const lineParent = line . parentNode as HTMLElement ;
122+ const lineParent = line . parentNode ;
121123
122- if ( lineParent === null || text === null ) return ;
124+ if ( text === null || lineParent === null || ! this . isElement ( lineParent ) ) return ;
123125
124- const lineState = lineParent . className . indexOf ( 'd2h-del' ) !== - 1 ? oldLinesState : newLinesState ;
126+ const lineState = lineParent . classList . contains ( 'd2h-del' ) ? oldLinesState : newLinesState ;
125127
126128 const language = file . getAttribute ( 'data-lang' ) ;
127129 const result =
@@ -130,9 +132,9 @@ export class Diff2HtmlUI {
130132 : this . hljs . highlightAuto ( text ) ;
131133
132134 if ( this . instanceOfIHighlightResult ( result ) ) {
133- if ( lineParent . className . indexOf ( 'd2h-del' ) !== - 1 ) {
135+ if ( lineParent . classList . contains ( 'd2h-del' ) ) {
134136 oldLinesState = result . top ;
135- } else if ( lineParent . className . indexOf ( 'd2h-ins' ) !== - 1 ) {
137+ } else if ( lineParent . classList . contains ( 'd2h-ins' ) ) {
136138 newLinesState = result . top ;
137139 } else {
138140 oldLinesState = result . top ;
@@ -159,18 +161,15 @@ export class Diff2HtmlUI {
159161 const diffTable = body . getElementsByClassName ( 'd2h-diff-table' ) [ 0 ] ;
160162
161163 diffTable . addEventListener ( 'mousedown' , event => {
162- if ( event === null || event . target === null ) return ;
163-
164- const mouseEvent = event as MouseEvent ;
165- const target = mouseEvent . target as HTMLElement ;
166- const table = target . closest ( '.d2h-diff-table' ) ;
164+ if ( event === null || ! this . isElement ( event . target ) ) return ;
167165
166+ const table = event . target . closest ( '.d2h-diff-table' ) ;
168167 if ( table !== null ) {
169- if ( target . closest ( '.d2h-code-line,.d2h-code-side-line' ) !== null ) {
168+ if ( event . target . closest ( '.d2h-code-line,.d2h-code-side-line' ) !== null ) {
170169 table . classList . remove ( 'selecting-left' ) ;
171170 table . classList . add ( 'selecting-right' ) ;
172171 this . currentSelectionColumnId = 1 ;
173- } else if ( target . closest ( '.d2h-code-linenumber,.d2h-code-side-linenumber' ) !== null ) {
172+ } else if ( event . target . closest ( '.d2h-code-linenumber,.d2h-code-side-linenumber' ) !== null ) {
174173 table . classList . remove ( 'selecting-right' ) ;
175174 table . classList . add ( 'selecting-left' ) ;
176175 this . currentSelectionColumnId = 0 ;
@@ -179,8 +178,9 @@ export class Diff2HtmlUI {
179178 } ) ;
180179
181180 diffTable . addEventListener ( 'copy' , event => {
182- const clipboardEvent = event as ClipboardEvent ;
183- const clipboardData = clipboardEvent . clipboardData ;
181+ if ( ! this . isClipboardEvent ( event ) ) return ;
182+
183+ const clipboardData = event . clipboardData ;
184184 const text = this . getSelectedText ( ) ;
185185
186186 if ( clipboardData === null || text === undefined ) return ;
@@ -231,4 +231,12 @@ export class Diff2HtmlUI {
231231
232232 return text ;
233233 }
234+
235+ private isElement ( arg ?: unknown ) : arg is Element {
236+ return arg !== null && ( arg as Element ) ?. classList !== undefined ;
237+ }
238+
239+ private isClipboardEvent ( arg ?: unknown ) : arg is ClipboardEvent {
240+ return arg !== null && ( arg as ClipboardEvent ) ?. clipboardData !== undefined ;
241+ }
234242}
0 commit comments