jenkins-bot has submitted this change and it was merged. Change subject: Add utilities to work around decodeURIComponent() exceptions ......................................................................
Add utilities to work around decodeURIComponent() exceptions ve.safeDecodeURIComponent() swallows exceptions from decodeURIComponent(), and ve.isUriComponentValid() uses these exceptions to test "validity". Change-Id: Iacc50230af499f7998f0537ac6426d45a6f99099 --- M src/ve.utils.js 1 file changed, 34 insertions(+), 0 deletions(-) Approvals: Trevor Parscal: Looks good to me, approved jenkins-bot: Verified diff --git a/src/ve.utils.js b/src/ve.utils.js index 374e866..fe1380a 100644 --- a/src/ve.utils.js +++ b/src/ve.utils.js @@ -811,6 +811,40 @@ }; /** + * Check if a string is a valid URI component. + * + * A URI component is considered invalid if decodeURIComponent() throws an exception. + * + * @param {string} s String to test + * @return {boolean} decodeURIComponent( s ) did not throw an exception + * @see #safeDecodeURIComponent + */ +ve.isUriComponentValid = function ( s ) { + try { + decodeURIComponent( s ); + } catch ( e ) { + return false; + } + return true; +}; + +/** + * Safe version of decodeURIComponent() that doesn't throw exceptions. + * + * If the native decodeURIComponent() call threw an exception, the original string + * will be returned + * @param {string} s String to decode + * @return {string} Decoded string, or same string if decoding failed + * @see #isUriComponentValid + */ +ve.safeDecodeURIComponent = function ( s ) { + try { + s = decodeURIComponent( s ); + } catch ( e ) {} + return s; +}; + +/** * Get the actual inner HTML of a DOM node. * * In most browsers, .innerHTML is broken and eats newlines in `<pre>` elements, see -- To view, visit https://gerrit.wikimedia.org/r/200809 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: Iacc50230af499f7998f0537ac6426d45a6f99099 Gerrit-PatchSet: 1 Gerrit-Project: VisualEditor/VisualEditor Gerrit-Branch: master Gerrit-Owner: Catrope <roan.katt...@gmail.com> Gerrit-Reviewer: Esanders <esand...@wikimedia.org> Gerrit-Reviewer: Jforrester <jforres...@wikimedia.org> Gerrit-Reviewer: Krinkle <krinklem...@gmail.com> Gerrit-Reviewer: Trevor Parscal <tpars...@wikimedia.org> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits