Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#214 feature/implement replace js function #351

Merged
merged 20 commits into from
Aug 23, 2023

Conversation

0styx0
Copy link

@0styx0 0styx0 commented Aug 6, 2023

Summary

Feature implemented: Users can replace using JS functions. The provided input will run as the return statement to string.replace and can use the same variables mentioned in MDN

Example:

  • Replace: dino(saur)
    • Input type: Regular Expression
  • With: "great big " + match + " biga" + p1.toUpperCase()
    • Output type: Function
  • Result: great big dinosaur bigaSAUR
Screenshot The word 'dinosaur

UI changes:

  • Added a warning about executing arbitrary JS in the options page
  • Added an "Output Type" select in the sidebar and in the options page featuring options "text" and "function"

This PR addresses issue #214

Copy link
Owner

@Woundorf Woundorf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, thanks for the pull request and sorry for not responding before.

First of all, congratulation because it is a great work and you clearly know JavaScript better than me. And also thanks for translating the new strings to Catalan. I suppose you have used automatic translation but it is still fairly good.

I have added some comments with things that should be improved and other things.

InputTypeEditor.getOption(params, 1, browser.i18n.getMessage("inputType.wholeWords")) +
InputTypeEditor.getOption(params, 2, browser.i18n.getMessage("inputType.regExp")) +
this.options.reduce((accum, curOption, i) =>
DropdownEditor.getOption(params, i, browser.i18n.getMessage(curOption)) + accum
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dropdown elements appear in reverse order from specified in code. The concatenation should be accum + DropdownEditor.getOption(...) instead.

@@ -218,6 +218,7 @@ <h4 class="i18n">options.otherSettings</h4>
</div>
</div>
<div id="substitutionsContent" class="tab-pane" role="tabpanel">
<p class="i18n">options.output.function.warning</p>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a graphical warning sign would be more outstanding. For example:
<p><i class="fas fa-exclamation-triangle"></i> <span class="i18n">options.output.function.warning</span></p>

scripts/core.js Outdated
this.caseSensitive = !!caseSensitive;
this.outputType = +outputType;
this.inputType = +inputType;

if (this.inputType < this.INPUT_TEXT || this.inputType > this.INPUT_REG_EXP) this.inputType = this.INPUT_TEXT; // avoid invalid values
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you could add another line like this for the output type, to ensure it is a valid value.

scripts/core.js Outdated

case this.INPUT_WHOLE_WORDS:
// necessary according to https://stackoverflow.com/q/1520800
this.regExp.lastIndex = 0;
return string.replace(this.regExp, (word, index, string) => {
// todo by someone who understands: clarify what the following block does and why it does it
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't remember the details because I wrote it many years ago, but it has to do with correct functioning of replace whole words with non-ASCII characters, including respecting the special strings $$, $&, etc. It was added in df6fe58.

scripts/core.js Outdated

}

replaceWithFn(userReplaceInput, curStrToReplace) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add some documentation to this method, like in the others. Maybe some of the comments below could be moved to this method documentation.

scripts/core.js Outdated
let inputType = this.prototype.INPUT_TYPE_STRINGS.indexOf(json.inputType);
return new Substitution(json.input, json.output, json.caseSensitive, inputType);
const inputType = this.prototype.INPUT_TYPE_STRINGS.indexOf(json.inputType);
const outputType = this.prototype.OUTPUT_TYPE_STRINGS.indexOf(json.outputType);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you should check if the JSON really has an outputType, otherwise it fails. Look how the mode is filled in SubstitutionGroup.fromJSON for reference.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have to update the version in substitutionListToJSON. Use version 2.6, which will be the version of FoxReplace when this is released. Also, in checkVersion set 2.6 as the current version and add 2.1 to the list of old versions.

@@ -47,6 +47,7 @@ function replaceWindow(aWindow, aSubstitutionList, aPrefs) {
case group.HTML_NONE: replaceText(doc, consecutiveGroups, aPrefs); break;
case group.HTML_OUTPUT: replaceTextWithHtml(doc, consecutiveGroups, aPrefs); break;
case group.HTML_INPUT_OUTPUT: replaceHtml(doc, consecutiveGroups, aPrefs); break;
default: console.error('Fox Replace: Invalid output type');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is necessary, but in any case it is FoxReplace, without space, and it would be the HTML mode that is wrong, not the output type. But since there are no other checks like this in this file, I would remove the line entirely.

@@ -24,7 +24,7 @@
<link rel="stylesheet" href="../lib/bootstrap.css">
<script src="../lib/xregexp-all.js"></script>
<script src="../scripts/core.js"></script>
<script src="sidebar.js"></script>
<script defer src="./sidebar.js"></script>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of this defer?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To ensure sidebar.js only loads once sidebar.html fully loads. If sidebar.js runs before sidebar.html fully renders, dom selectors will fail

<option value="0" selected="" class="i18n">outputType.text</option>
<option value="1" class="i18n">outputType.function</option>
</select>
</div>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could add the warning also here, below the output type, preferably shown only if "function" is selected.

@0styx0
Copy link
Author

0styx0 commented Aug 21, 2023

@Woundorf thanks for your feedback! addressed all comments

Copy link
Owner

@Woundorf Woundorf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only one issue with the DropdownEditor. Everything else is fine.

@@ -139,7 +146,7 @@ class DropdownEditor {
init(params) {
this.gui = $('<select>' +
this.options.reduce((accum, curOption, i) =>
DropdownEditor.getOption(params, i, browser.i18n.getMessage(curOption)) + accum
accum + DropdownEditor.getOption(params, i, browser.i18n.getMessage(curOption)) + accum
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have added the accum to the beginning but not removed it from the end. Now some items are repeated multiple times.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ugh good catch. hit c-z too many times. fixed

@Woundorf Woundorf merged commit 0a5ed28 into Woundorf:master Aug 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants