Skip to content

Commit

Permalink
Merge branch 'master' into array-includes
Browse files Browse the repository at this point in the history
  • Loading branch information
NovemLinguae authored Dec 11, 2024
2 parents bfd676d + a6afebb commit b476e8f
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 32 deletions.
1 change: 0 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
scripts/
tests/
lib/
1 change: 0 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
"no-jquery/no-sizzle": "warn",
"no-loop-func": "warn",
"no-new": "warn",
"no-return-assign": "warn",
"no-script-url": "warn",
"no-unused-expressions": "warn",
"no-use-before-define": "warn",
Expand Down
58 changes: 29 additions & 29 deletions modules/twinkletag.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ Twinkle.tag.callback = function twinkletagCallback() {
event: function twinkletagquickfilter() {
// flush the DOM of all existing underline spans
$allCheckboxDivs.find('.search-hit').each((i, e) => {
const label_element = e.parentElement;
const labelElement = e.parentElement;
// This would convert <label>Hello <span class=search-hit>wo</span>rld</label>
// to <label>Hello world</label>
label_element.innerHTML = label_element.textContent;
labelElement.innerHTML = labelElement.textContent;
});

if (this.value) {
Expand All @@ -100,16 +100,16 @@ Twinkle.tag.callback = function twinkletagCallback() {
const searchRegex = new RegExp(mw.util.escapeRegExp(searchString), 'i');

$allCheckboxDivs.find('label').each(function () {
const label_text = this.textContent;
const searchHit = searchRegex.exec(label_text);
const labelText = this.textContent;
const searchHit = searchRegex.exec(labelText);
if (searchHit) {
const range = document.createRange();
const textnode = this.childNodes[0];
range.selectNodeContents(textnode);
range.setStart(textnode, searchHit.index);
range.setEnd(textnode, searchHit.index + searchString.length);
const underline_span = $('<span>').addClass('search-hit').css('text-decoration', 'underline')[0];
range.surroundContents(underline_span);
const underlineSpan = $('<span>').addClass('search-hit').css('text-decoration', 'underline')[0];
range.surroundContents(underlineSpan);
this.parentElement.style.display = 'block'; // show
}
});
Expand Down Expand Up @@ -1439,7 +1439,7 @@ Twinkle.tag.callbacks = {
'Listing article at Wikipedia:Pages needing translation into English');
pntPage.setFollowRedirect(true);
pntPage.load((pageobj) => {
const old_text = pageobj.getPageText();
const oldText = pageobj.getPageText();

const lang = params.translationLanguage;
const reason = params.translationComments;
Expand All @@ -1451,18 +1451,18 @@ Twinkle.tag.callbacks = {
templateText = '{{subst:Dual fluency request|pg=' + Morebits.pageNameNorm + '|Language=' +
(lang || 'uncertain') + '|Comments=' + reason.trim() + '}} ~~~~';
// Place in section == Translated pages that could still use some cleanup ==
text = old_text + '\n\n' + templateText;
text = oldText + '\n\n' + templateText;
summary = 'Translation cleanup requested on ';
} else if (params.tags.includes('Not English')) {
templateText = '{{subst:Translation request|pg=' + Morebits.pageNameNorm + '|Language=' +
(lang || 'uncertain') + '|Comments=' + reason.trim() + '}} ~~~~';
// Place in section == Pages for consideration ==
text = old_text.replace(/\n+(==\s?Translated pages that could still use some cleanup\s?==)/,
text = oldText.replace(/\n+(==\s?Translated pages that could still use some cleanup\s?==)/,
'\n\n' + templateText + '\n\n$1');
summary = 'Translation' + (lang ? ' from ' + lang : '') + ' requested on ';
}

if (text === old_text) {
if (text === oldText) {
pageobj.getStatusElement().error('failed to find target spot for the discussion');
return;
}
Expand Down Expand Up @@ -1522,10 +1522,10 @@ Twinkle.tag.callbacks = {
// otherwise moves it to `getRedirectsFor` array earmarking it for
// later removal
params.tagsToRemove.forEach((tag) => {
const tag_re = new RegExp('\\{\\{' + Morebits.pageNameRegex(tag) + '\\s*(\\|[^}]+)?\\}\\}\\n?');
const tagRegex = new RegExp('\\{\\{' + Morebits.pageNameRegex(tag) + '\\s*(\\|[^}]+)?\\}\\}\\n?');

if (tag_re.test(pageText)) {
pageText = pageText.replace(tag_re, '');
if (tagRegex.test(pageText)) {
pageText = pageText.replace(tagRegex, '');
} else {
getRedirectsFor.push('Template:' + tag);
}
Expand All @@ -1552,9 +1552,9 @@ Twinkle.tag.callbacks = {
let removed = false;
page.linkshere.concat({title: page.title}).forEach((el) => {
const tag = el.title.slice(9);
const tag_re = new RegExp('\\{\\{' + Morebits.pageNameRegex(tag) + '\\s*(\\|[^}]*)?\\}\\}\\n?');
if (tag_re.test(pageText)) {
pageText = pageText.replace(tag_re, '');
const tagRegex = new RegExp('\\{\\{' + Morebits.pageNameRegex(tag) + '\\s*(\\|[^}]*)?\\}\\}\\n?');
if (tagRegex.test(pageText)) {
pageText = pageText.replace(tagRegex, '');
removed = true;
return false; // break out of $.each
}
Expand Down Expand Up @@ -1743,10 +1743,10 @@ Twinkle.tag.callbacks = {
// Reposition the tags on the page into {{multiple issues}}, if found with its
// proper name, else moves it to `getRedirectsFor` array to be handled later
groupableExistingTags.forEach((tag) => {
const tag_re = new RegExp('(\\{\\{' + Morebits.pageNameRegex(tag) + '\\s*(\\|[^}]+)?\\}\\}\\n?)');
if (tag_re.test(pageText)) {
tagText += tag_re.exec(pageText)[1];
pageText = pageText.replace(tag_re, '');
const tagRegex = new RegExp('(\\{\\{' + Morebits.pageNameRegex(tag) + '\\s*(\\|[^}]+)?\\}\\}\\n?)');
if (tagRegex.test(pageText)) {
tagText += tagRegex.exec(pageText)[1];
pageText = pageText.replace(tagRegex, '');
} else {
getRedirectsFor.push('Template:' + tag);
}
Expand All @@ -1772,10 +1772,10 @@ Twinkle.tag.callbacks = {
let found = false;
page.linkshere.forEach((el) => {
const tag = el.title.slice(9);
const tag_re = new RegExp('(\\{\\{' + Morebits.pageNameRegex(tag) + '\\s*(\\|[^}]*)?\\}\\}\\n?)');
if (tag_re.test(pageText)) {
tagText += tag_re.exec(pageText)[1];
pageText = pageText.replace(tag_re, '');
const tagRegex = new RegExp('(\\{\\{' + Morebits.pageNameRegex(tag) + '\\s*(\\|[^}]*)?\\}\\}\\n?)');
if (tagRegex.test(pageText)) {
tagText += tagRegex.exec(pageText)[1];
pageText = pageText.replace(tagRegex, '');
found = true;
return false; // break out of $.each
}
Expand Down Expand Up @@ -2009,7 +2009,7 @@ Twinkle.tag.callbacks = {
* @return {true|undefined}
*/
Twinkle.tag.checkIncompatible = function(incompatibleTags, tagsToCheck, extraMessage = null) {
const count = incompatibleTags.reduce((sum, tag) => sum += tagsToCheck.includes(tag), 0);
const count = incompatibleTags.filter((tag) => tagsToCheck.includes(tag)).length;
if (count > 1) {
const incompatibleTagsString = '{{' + incompatibleTags.join('}}, {{') + '}}';
let message = 'Please select only one of: ' + incompatibleTagsString + '.';
Expand Down Expand Up @@ -2172,10 +2172,10 @@ Twinkle.tag.callback.evaluate = function twinkletagCallbackEvaluate(e) {
Morebits.wiki.actionCompleted.followRedirect = false;
}

const wikipedia_page = new Morebits.wiki.Page(Morebits.pageNameNorm, 'Tagging ' + Twinkle.tag.mode);
wikipedia_page.setCallbackParameters(params);
wikipedia_page.setChangeTags(Twinkle.changeTags); // Here to apply to triage
wikipedia_page.load(Twinkle.tag.callbacks[Twinkle.tag.mode]);
const wikipediaPage = new Morebits.wiki.Page(Morebits.pageNameNorm, 'Tagging ' + Twinkle.tag.mode);
wikipediaPage.setCallbackParameters(params);
wikipediaPage.setChangeTags(Twinkle.changeTags); // Here to apply to triage
wikipediaPage.load(Twinkle.tag.callbacks[Twinkle.tag.mode]);

};

Expand Down
31 changes: 31 additions & 0 deletions tests/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"extends": [
"wikimedia/server"
],
"env": {
"jest": true
},
"root": true,
"rules": {
"array-bracket-spacing": "off",
"computed-property-spacing": "off",
"no-shadow": "off",
"object-curly-spacing": "off",
"space-in-parens": "off",

"camelcase": "warn",
"comma-dangle": "warn",
"eol-last": "warn",
"new-cap": "warn",
"prefer-arrow-callback": "warn",
"prefer-const": "warn",
"no-trailing-spaces": "warn",
"no-undef": "warn",
"no-useless-escape": "warn",
"no-var": "warn",
"semi": "warn",
"strict": "warn",
"quotes": "warn",
"unicorn/prefer-date-now": "warn"
}
}
9 changes: 8 additions & 1 deletion tests/twinkletag.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
describe('modules/twinkletag', () => {
describe('checkIncompatible', () => {
test('no conflicts', () => {
test('no conflicts, 0 tags to check', () => {
const incompatibleTags = ['Bad GIF', 'Bad JPEG', 'Bad SVG', 'Bad format'];
const tagsToCheck = [];
const expected = undefined;
expect(Twinkle.tag.checkIncompatible(incompatibleTags, tagsToCheck)).toBe(expected);
});

test('no conflicts, 1 tag to check', () => {
const incompatibleTags = ['Bad GIF', 'Bad JPEG', 'Bad SVG', 'Bad format'];
const tagsToCheck = ['Better source requested'];
const expected = undefined;
Expand Down

0 comments on commit b476e8f

Please sign in to comment.