From f651143cdb309f088c53cb5cc5d17b424c11a7b6 Mon Sep 17 00:00:00 2001 From: Andrew Herron Date: Mon, 19 Feb 2024 13:26:17 +1100 Subject: [PATCH] Add module guard so the script still works in browsers. Attempt to keep the old closure reference code as well. --- javascript/diff_match_patch_uncompressed.js | 38 ++++++++++++++------- 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/javascript/diff_match_patch_uncompressed.js b/javascript/diff_match_patch_uncompressed.js index bb6c654..489f355 100644 --- a/javascript/diff_match_patch_uncompressed.js +++ b/javascript/diff_match_patch_uncompressed.js @@ -1197,7 +1197,7 @@ diff_match_patch.prototype.diff_cleanupMerge = function(diffs) { /** * Rearrange diff boundaries that split Unicode surrogate pairs. - * + * * @param {!Array.} diffs Array of diff tuples. */ diff_match_patch.prototype.diff_cleanupSplitSurrogates = function(diffs) { @@ -1264,17 +1264,17 @@ diff_match_patch.prototype.digit16 = function(c) { /** * Decode URI-encoded string but allow for encoded surrogate halves - * + * * diff_match_patch needs this relaxation of the requirements because * not all libraries and versions produce valid URI strings in toDelta * and we don't want to crash this code when the input is valid input * but at the same time invalid utf-8 - * + * * @example: decodeURI( 'abcd%3A %F0%9F%85%B0' ) = 'abcd: \ud83c\udd70' * @example: decodeURI( 'abcd%3A %ED%A0%BC' ) = 'abcd: \ud83c' - * + * * @cite: @mathiasbynens utf8.js at https://github.com/mathiasbynens/utf8.js - * + * * @param {String} text input string encoded by encodeURI() or equivalent * @return {String} */ @@ -2384,10 +2384,24 @@ diff_match_patch.patch_obj.prototype.toString = function() { return text.join('').replace(/%20/g, ' '); }; - -// The following export code was added by @ForbesLindesay -module.exports = diff_match_patch; -module.exports['diff_match_patch'] = diff_match_patch; -module.exports['DIFF_DELETE'] = DIFF_DELETE; -module.exports['DIFF_INSERT'] = DIFF_INSERT; -module.exports['DIFF_EQUAL'] = DIFF_EQUAL; \ No newline at end of file +// CLOSURE:begin_strip +// Lines below here will not be included in the Closure-compatible library. +if (typeof module === 'object') { + // The following export code was added by @ForbesLindesay + module.exports = diff_match_patch; + module.exports['diff_match_patch'] = diff_match_patch; + module.exports['DIFF_DELETE'] = DIFF_DELETE; + module.exports['DIFF_INSERT'] = DIFF_INSERT; + module.exports['DIFF_EQUAL'] = DIFF_EQUAL; +} else { + // Export these global variables so that they survive Google's JS compiler. + // In a browser, 'this' will be 'window'. + /** @suppress {globalThis} */ + this['diff_match_patch'] = diff_match_patch; + /** @suppress {globalThis} */ + this['DIFF_DELETE'] = DIFF_DELETE; + /** @suppress {globalThis} */ + this['DIFF_INSERT'] = DIFF_INSERT; + /** @suppress {globalThis} */ + this['DIFF_EQUAL'] = DIFF_EQUAL; +}