Skip to content

Commit 8b14eda

Browse files
author
Henrik Ingo
committed
Broader try-catch for handling impress-console-button disappearance (impress#652)
The previous attempt at merely reading a property of event.target was incorrect. It worked at first but errors reappeared later, so must have been a reace. This wraps the entire navigation event handlers in the try-catch, and then checks for the very specific error and suppresses it. Other errors are rethrown as is.
1 parent 784a6d2 commit 8b14eda

File tree

2 files changed

+84
-66
lines changed

2 files changed

+84
-66
lines changed

js/impress.js

+42-33
Original file line numberDiff line numberDiff line change
@@ -2965,55 +2965,64 @@
29652965
// check if event target (or any of its parents is a link)
29662966
var target = event.target;
29672967
try {
2968+
while ( ( target.tagName !== "A" ) &&
2969+
( target !== document.documentElement ) ) {
2970+
target = target.parentNode;
2971+
}
29682972

2969-
// For example, when clicking on the button to launch speaker console, the button
2970-
// is immediately deleted from the DOM. In this case target is a DOM element when
2971-
// we get it, but turns out to be null if you try to actually do anything with it.
2972-
var foo = target.id; // jshint ignore:line
2973-
}
2974-
catch ( err ) {
2975-
return;
2976-
}
2977-
2978-
while ( ( target.tagName !== "A" ) &&
2979-
( target !== document.documentElement ) ) {
2980-
target = target.parentNode;
2981-
}
2973+
if ( target.tagName === "A" ) {
2974+
var href = target.getAttribute( "href" );
29822975

2983-
if ( target.tagName === "A" ) {
2984-
var href = target.getAttribute( "href" );
2976+
// If it's a link to presentation step, target this step
2977+
if ( href && href[ 0 ] === "#" ) {
2978+
target = document.getElementById( href.slice( 1 ) );
2979+
}
2980+
}
29852981

2986-
// If it's a link to presentation step, target this step
2987-
if ( href && href[ 0 ] === "#" ) {
2988-
target = document.getElementById( href.slice( 1 ) );
2982+
if ( api.goto( target ) ) {
2983+
event.stopImmediatePropagation();
2984+
event.preventDefault();
29892985
}
29902986
}
2987+
catch ( err ) {
29912988

2992-
if ( api.goto( target ) ) {
2993-
event.stopImmediatePropagation();
2994-
event.preventDefault();
2989+
// For example, when clicking on the button to launch speaker console, the button
2990+
// is immediately deleted from the DOM. In this case target is a DOM element when
2991+
// we get it, but turns out to be null if you try to actually do anything with it.
2992+
if ( err instanceof TypeError &&
2993+
err.message === "target is null" ) {
2994+
return;
2995+
}
2996+
throw err;
29952997
}
29962998
}, false );
29972999

29983000
// Delegated handler for clicking on step elements
29993001
gc.addEventListener( document, "click", function( event ) {
30003002
var target = event.target;
30013003
try {
3002-
var foo = target.id; // jshint ignore:line
3003-
}
3004-
catch ( err ) {
3005-
return;
3006-
}
30073004

3008-
// Find closest step element that is not active
3009-
while ( !( target.classList.contains( "step" ) &&
3010-
!target.classList.contains( "active" ) ) &&
3011-
( target !== document.documentElement ) ) {
3012-
target = target.parentNode;
3005+
// Find closest step element that is not active
3006+
while ( !( target.classList.contains( "step" ) &&
3007+
!target.classList.contains( "active" ) ) &&
3008+
( target !== document.documentElement ) ) {
3009+
target = target.parentNode;
3010+
}
3011+
3012+
if ( api.goto( target ) ) {
3013+
event.preventDefault();
3014+
}
30133015
}
3016+
catch ( err ) {
30143017

3015-
if ( api.goto( target ) ) {
3016-
event.preventDefault();
3018+
// For example, when clicking on the button to launch speaker console, the button
3019+
// is immediately deleted from the DOM. In this case target is a DOM element when
3020+
// we get it, but turns out to be null if you try to actually do anything with it.
3021+
if ( err instanceof TypeError &&
3022+
err.message === "target is null" ) {
3023+
return;
3024+
}
3025+
throw err;
30173026
}
30183027
}, false );
30193028

src/plugins/navigation/navigation.js

+42-33
Original file line numberDiff line numberDiff line change
@@ -125,55 +125,64 @@
125125
// check if event target (or any of its parents is a link)
126126
var target = event.target;
127127
try {
128+
while ( ( target.tagName !== "A" ) &&
129+
( target !== document.documentElement ) ) {
130+
target = target.parentNode;
131+
}
128132

129-
// For example, when clicking on the button to launch speaker console, the button
130-
// is immediately deleted from the DOM. In this case target is a DOM element when
131-
// we get it, but turns out to be null if you try to actually do anything with it.
132-
var foo = target.id; // jshint ignore:line
133-
}
134-
catch ( err ) {
135-
return;
136-
}
133+
if ( target.tagName === "A" ) {
134+
var href = target.getAttribute( "href" );
137135

138-
while ( ( target.tagName !== "A" ) &&
139-
( target !== document.documentElement ) ) {
140-
target = target.parentNode;
141-
}
142-
143-
if ( target.tagName === "A" ) {
144-
var href = target.getAttribute( "href" );
136+
// If it's a link to presentation step, target this step
137+
if ( href && href[ 0 ] === "#" ) {
138+
target = document.getElementById( href.slice( 1 ) );
139+
}
140+
}
145141

146-
// If it's a link to presentation step, target this step
147-
if ( href && href[ 0 ] === "#" ) {
148-
target = document.getElementById( href.slice( 1 ) );
142+
if ( api.goto( target ) ) {
143+
event.stopImmediatePropagation();
144+
event.preventDefault();
149145
}
150146
}
147+
catch ( err ) {
151148

152-
if ( api.goto( target ) ) {
153-
event.stopImmediatePropagation();
154-
event.preventDefault();
149+
// For example, when clicking on the button to launch speaker console, the button
150+
// is immediately deleted from the DOM. In this case target is a DOM element when
151+
// we get it, but turns out to be null if you try to actually do anything with it.
152+
if ( err instanceof TypeError &&
153+
err.message === "target is null" ) {
154+
return;
155+
}
156+
throw err;
155157
}
156158
}, false );
157159

158160
// Delegated handler for clicking on step elements
159161
gc.addEventListener( document, "click", function( event ) {
160162
var target = event.target;
161163
try {
162-
var foo = target.id; // jshint ignore:line
163-
}
164-
catch ( err ) {
165-
return;
166-
}
167164

168-
// Find closest step element that is not active
169-
while ( !( target.classList.contains( "step" ) &&
170-
!target.classList.contains( "active" ) ) &&
171-
( target !== document.documentElement ) ) {
172-
target = target.parentNode;
165+
// Find closest step element that is not active
166+
while ( !( target.classList.contains( "step" ) &&
167+
!target.classList.contains( "active" ) ) &&
168+
( target !== document.documentElement ) ) {
169+
target = target.parentNode;
170+
}
171+
172+
if ( api.goto( target ) ) {
173+
event.preventDefault();
174+
}
173175
}
176+
catch ( err ) {
174177

175-
if ( api.goto( target ) ) {
176-
event.preventDefault();
178+
// For example, when clicking on the button to launch speaker console, the button
179+
// is immediately deleted from the DOM. In this case target is a DOM element when
180+
// we get it, but turns out to be null if you try to actually do anything with it.
181+
if ( err instanceof TypeError &&
182+
err.message === "target is null" ) {
183+
return;
184+
}
185+
throw err;
177186
}
178187
}, false );
179188

0 commit comments

Comments
 (0)