Skip to content

Commit

Permalink
Log information about not modified content
Browse files Browse the repository at this point in the history
Add a test
  • Loading branch information
AdamWr committed Apr 25, 2024
1 parent 48545bf commit 7ebb300
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/scriptlets/trusted-replace-outbound-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 19 additions & 0 deletions tests/scriptlets/trusted-replace-outbound-text.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 7ebb300

Please sign in to comment.