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

Updated autocorrect to not match version numbers #43

Merged
merged 5 commits into from
Jun 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"env": {
"browser": true,
"webextensions": true,
"es6": true
"es2021": true
},
"extends": "eslint:recommended",
"rules": {
Expand Down
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github: [rugk, tdulcet]
2 changes: 1 addition & 1 deletion scripts/manifests/chromemanifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"manifest_version": 2,
"name": "Unicodify DEV VERSION",
"name": "__MSG_extensionName__",
"short_name": "__MSG_extensionNameShort__",
"version": "0.1",
"author": "Teal Dulcet, rugk",
Expand Down
11 changes: 3 additions & 8 deletions src/background/modules/AutocorrectHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ let autocorrections = {};
let longest = 0;

let symbolpatterns = [];
// Do not autocorrect for these patterns
// Exceptions, do not autocorrect for these patterns
let antipatterns = [];

// Chrome
Expand Down Expand Up @@ -48,13 +48,10 @@ function applySettings() {
}
console.log("Longest autocorrection", longest);

symbolpatterns = [];
// Escape special characters
const regExSpecialChars = /[.*+?^${}()|[\]\\]/g;

for (const symbol in autocorrections) {
symbolpatterns.push(symbol.replace(regExSpecialChars, "\\$&"));
}
symbolpatterns = Object.keys(autocorrections).map((symbol) => symbol.replace(regExSpecialChars, "\\$&"));

// Do not autocorrect for these patterns
antipatterns = [];
Expand Down Expand Up @@ -87,9 +84,7 @@ function applySettings() {
antipatterns = antipatterns.filter((item, pos) => antipatterns.indexOf(item) === pos);
console.log("Do not autocorrect for these patterns", antipatterns);

for (const [index, symbol] of antipatterns.entries()) {
antipatterns[index] = symbol.replace(regExSpecialChars, "\\$&");
}
antipatterns = antipatterns.map((symbol) => symbol.replace(regExSpecialChars, "\\$&"));

symbolpatterns = new RegExp(`(${symbolpatterns.join("|")})$`);
antipatterns = new RegExp(`(${antipatterns.join("|")})$`);
Expand Down
7 changes: 6 additions & 1 deletion src/background/modules/ContextMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ function handleMenuChoosen(info, tab) {
* @throws {Error}
*/
async function handleMenuShown(info) {
if (!info.editable) {
return;
}

let text = info.selectionText;

// do not show menu entry when no text is selected
Expand All @@ -60,7 +64,7 @@ async function handleMenuShown(info) {
// shorten preview text as it may not be shown anyway
if (text.length > PREVIEW_STRING_CUT_LENGTH) {
// to be sure, we append … anyway, in case some strange OS has a tooltip for context menus or so
text = `${text.substr(0, PREVIEW_STRING_CUT_LENGTH)}…`;
text = `${text.substring(0, PREVIEW_STRING_CUT_LENGTH)}…`;
}
text = text.normalize();

Expand Down Expand Up @@ -140,6 +144,7 @@ async function addMenuItems(menuItems, unicodeFontSettings = lastCachedUnicodeFo
if (unicodeFontSettings.showReadableText) {
menuText = browser.i18n.getMessage("menuReadableTextWrapper", [translatedMenuText, transformedText]);
}
menuText = menuText.replaceAll("&", "&&");

if (refreshMenu) {
menus.update(transformationId, {
Expand Down
14 changes: 8 additions & 6 deletions src/common/modules/UnicodeTransformationHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export function getTransformationType(transformationId) {
function capitalizeEachWord(text) {
// Regular expression Unicode property escapes and lookbehind assertions require Firefox/Thunderbird 78
// see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#bcd:javascript.builtins.RegExp
// \p{Alphabetic}
tdulcet marked this conversation as resolved.
Show resolved Hide resolved
return text.replace(/(?<=^|\P{Alpha})\p{Alpha}\S*/gu, ([h, ...t]) => h.toLocaleUpperCase() + t.join(""));
}

Expand Down Expand Up @@ -110,12 +111,13 @@ function toggleCase(atext) {
let output = "";

for (let letter of atext) {
const upper = letter.toLocaleUpperCase();
const lower = letter.toLocaleLowerCase();
if (letter === lower && letter !== upper) {
letter = upper;
} else if (letter === upper && letter !== lower) {
letter = lower;
// \p{Changes_When_Uppercased}
rugk marked this conversation as resolved.
Show resolved Hide resolved
if (/\p{CWU}/u.test(letter)) {
letter = letter.toLocaleUpperCase();
}
// \p{Changes_When_Lowercased}
else if (/\p{CWL}/u.test(letter)) {
letter = letter.toLocaleLowerCase();
}
output += letter;
}
Expand Down
11 changes: 6 additions & 5 deletions src/content_scripts/autocorrect.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ let longest = 0;

// Regular expressions
let symbolpatterns = null;
// Do not autocorrect for these patterns
// Exceptions, do not autocorrect for these patterns
let antipatterns = null;

// Thunderbird
Expand Down Expand Up @@ -309,15 +309,16 @@ function autocorrect(event) {
} else {
// Convert fractions and mathematical constants to Unicode characters
if (!output && fracts) {
// Numbers: https://regex101.com/r/7jUaSP/2
const numberRegex = /[0-9]+(\.[0-9]+)?$/;
// Numbers regular expression: https://regex101.com/r/7jUaSP/10
// Do not match version numbers: https://github.com/rugk/unicodify/issues/40
const numberRegex = /(?<!\.)\d+(?<fractionpart>\.\d+)?$/;
const previousText = value.slice(0, caretposition - 1);
const regexResult = numberRegex.exec(previousText);
if (regexResult) {
if (regexResult && insert !== ".") {
const text = value.slice(0, caretposition);
const aregexResult = numberRegex.exec(text);
if (!aregexResult) {
const label = outputLabel(regexResult[0], regexResult[1]);
const label = outputLabel(regexResult[0], regexResult.groups.fractionpart);
const index = firstDifferenceIndex(label, regexResult[0]);
if (index >= 0) {
insert = label.slice(index) + (event.keyCode === 13 ? "\n" : insert);
Expand Down
27 changes: 13 additions & 14 deletions src/options/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,16 @@
</div>
<form>
<section>
<ul>
<h1 data-i18n="__MSG_titleUnicodeAutocorrection__">Unicode autocorrection</h1>
<span class="helper-text" data-i18n="__MSG_optionHelperTextAutocorrection__" data-opt-i18n-keep-children>
You can configure the automatic corrections that are done when you are typing text here.<br>
All the autocorrections are done after typing the first nonmatching character following the character sequence, such as a space (␣). Press Backspace (⌫) to undo an autocorrection.
Only the quotes replacement is done immediately.
<h1 data-i18n="__MSG_titleUnicodeAutocorrection__">Unicode autocorrection</h1>
<span class="helper-text" data-i18n="__MSG_optionHelperTextAutocorrection__" data-opt-i18n-keep-children>
You can configure the automatic corrections that are done when you are typing text here.<br>
All the autocorrections are done after typing the first nonmatching character following the character sequence, such as a space (␣). Press Backspace (⌫) to undo an autocorrection.
Only the quotes replacement is done immediately.
</span><br>
<span class="helper-text" data-i18n="__MSG_optionHelperTextAutocorrectionEmoji__" data-opt-i18n-keep-children>
For Emoji autocorrection, including <code data-i18n="__MSG_optionHelperTextAutocorrectionEmojiColonsCode__">:colon:</code> shortcodes and Emoticons, please also try out our <a id="link-awesome-emoji" data-i18n="__MSG_optionHelperTextAutocorrectionEmojiLinkText__">🤩&nbsp;Awesome Emoji Picker</a> add-on.
</span>

<span class="helper-text" data-i18n="__MSG_optionHelperTextAutocorrectionEmoji__" data-opt-i18n-keep-children>
For Emoji autocorrection, including <code data-i18n="__MSG_optionHelperTextAutocorrectionEmojiColonsCode__">:colon:</code> shortcodes and Emoticons, please also try out our <a id="link-awesome-emoji" data-i18n="__MSG_optionHelperTextAutocorrectionEmojiLinkText__">🤩&nbsp;Awesome Emoji Picker</a> add-on.
</span>
<ul>
<li>
<div class="line">
<input class="setting save-on-change" type="checkbox" id="autocorrectSymbols" data-optiongroup="autocorrect" name="autocorrectSymbols">
Expand Down Expand Up @@ -93,11 +92,11 @@ <h1 data-i18n="__MSG_titleUnicodeAutocorrection__">Unicode autocorrection</h1>

<!-- Remove "mobile-incompatible" once https://bugzilla.mozilla.org/show_bug.cgi?id=1595822 is fixed -->
<section class="mobile-incompatible">
<ul>
<h1 data-i18n="__MSG_titleContextMenu__">Context menu</h1>
<span class="helper-text" data-i18n="__MSG_optionHelperTextContextMenu__">Write, select text and choose the corresponding option in the context menu to quickly change the character style or capitalization.
This is useful on websites that do not support changing the font or text formatting.</span>
<h1 data-i18n="__MSG_titleContextMenu__">Context menu</h1>
<span class="helper-text" data-i18n="__MSG_optionHelperTextContextMenu__">Write, select text and choose the corresponding option in the context menu to quickly change the character style or capitalization.
This is useful on websites that do not support changing the font or text formatting.</span>

<ul>
<li>
<div class="line">
<input class="setting save-on-change" type="checkbox" id="changeFont" data-optiongroup="unicodeFont" name="changeFont">
Expand Down