From 7ebb3007d36c8e23d818b65ae6490c2145f4b76c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Wr=C3=B3blewski?= Date: Thu, 25 Apr 2024 12:18:59 +0200 Subject: [PATCH] Log information about not modified content Add a test --- .../trusted-replace-outbound-text.ts | 8 ++++++-- .../trusted-replace-outbound-text.test.js | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/scriptlets/trusted-replace-outbound-text.ts b/src/scriptlets/trusted-replace-outbound-text.ts index 5995b2b7b..46f03603d 100644 --- a/src/scriptlets/trusted-replace-outbound-text.ts +++ b/src/scriptlets/trusted-replace-outbound-text.ts @@ -137,8 +137,12 @@ export function trustedReplaceOtboundText( const patternRegexp = toRegExp(textToReplace); const modifiedContent = textToReplace ? result.replace(patternRegexp, replacement) : result; - if (logModifiedContent && modifiedContent !== result) { - logMessage(source, `Modified text content: ${modifiedContent}`); + if (logModifiedContent) { + const message = modifiedContent !== result + ? `Modified text content: ${modifiedContent}` + : 'Text content was not modified'; + + logMessage(source, message); } return modifiedContent; diff --git a/tests/scriptlets/trusted-replace-outbound-text.test.js b/tests/scriptlets/trusted-replace-outbound-text.test.js index fd6d8710c..3e34fbe6d 100644 --- a/tests/scriptlets/trusted-replace-outbound-text.test.js +++ b/tests/scriptlets/trusted-replace-outbound-text.test.js @@ -66,6 +66,25 @@ test('replace text - log original and modified content - atob', (assert) => { assert.strictEqual(window.hit, 'FIRED', 'hit function fired'); }); +test('text not matched - log original content and information about not modified content - atob', (assert) => { + console.log = (...args) => { + if (args.length === 1 && args[0].includes('Original text content:')) { + assert.ok(args[0].includes('Text not matched.'), 'should log original text in console'); + } + if (args.length === 1 && args[0].includes('not modified')) { + assert.ok(args[0].includes('Text content was not modified'), 'log information about not modified content'); + } + nativeConsole(...args); + }; + + runScriptlet(name, ['atob', 'NOT_MATCH', '', '', 'true']); + + const text = btoa('Text not matched.'); + const result = atob(text); + assert.deepEqual(result, 'Text not matched.', 'Text content is intact'); + assert.strictEqual(window.hit, 'FIRED', 'hit function fired'); +}); + test('replace text - regular expression - atob', (assert) => { runScriptlet(name, ['atob', '/for.*?-/', 'regexp -']); const text = btoa('Test for regular expression - foo bar');