jenkins-bot has submitted this change and it was merged. (
https://gerrit.wikimedia.org/r/372909 )
Change subject: Fix 'year page' identification bug.
......................................................................
Fix 'year page' identification bug.
Fixes issue causing some events to appear without any associated pages.
Bug: T169277
Change-Id: Ia8e824c43995ce478679ade0ee94359f48ecce3b
---
M routes/on-this-day.js
M test/features/onthisday/on-this-day.js
2 files changed, 47 insertions(+), 8 deletions(-)
Approvals:
BearND: Looks good to me, approved
jenkins-bot: Verified
diff --git a/routes/on-this-day.js b/routes/on-this-day.js
index 6ef9f10..99e06e3 100644
--- a/routes/on-this-day.js
+++ b/routes/on-this-day.js
@@ -122,6 +122,17 @@
}
/**
+ * Determines whether anchor is for specific year page.
+ * @param {!AnchorElement} anchor
+ * @param {!Integer} year
+ * @param {!String} era
+ * @return {!boolean}
+ */
+function isAnchorForYear(anchor, year, era) {
+ return new RegExp(`^${Math.abs(year)}\\s*${era}$`, 'i').test(anchor.title);
+}
+
+/**
* Converts document list element to WMFEvent model.
* A regular expression determines valid "year list elements" and separating
their components.
* For example: '399 BC - Death of Socrates'
@@ -146,21 +157,20 @@
}
let year = parseInt(match[1], 10);
-
- // Negate BC years so they sort correctly
const isBC = (match[2] !== undefined);
+ let era = '';
if (isBC) {
+ // Negate BC years so they sort correctly
year = -year;
+ era = match[2];
}
const textAfterYear = match[3].trim();
- function isAnchorNotForYear(anchor) {
- return Math.abs(parseInt(anchor.title, 10)) !== Math.abs(year);
- }
-
const pages = Array.from(listElement.querySelectorAll('a'))
- .filter(isAnchorNotForYear)
+ .filter((anchor) => {
+ return !isAnchorForYear(anchor, year, era);
+ })
.map(wmfPageFromAnchorElement);
return new WMFEvent(textAfterYear, pages, year);
@@ -539,7 +549,8 @@
eventsForYearListElements,
reverseChronologicalWMFEventComparator,
hydrateAllTitles,
- listElementsByHeadingID
+ listElementsByHeadingID,
+ isAnchorForYear
}
};
};
diff --git a/test/features/onthisday/on-this-day.js
b/test/features/onthisday/on-this-day.js
index 5c7232d..faf4f7a 100644
--- a/test/features/onthisday/on-this-day.js
+++ b/test/features/onthisday/on-this-day.js
@@ -450,4 +450,32 @@
const listElements =
onThisDay.testing.listElementsByHeadingID(document, ['Births']);
assert.ok(listElements.length === 208);
});
+
+ describe('isAnchorForYear', () => {
+ const a = domino.createDocument().createElement('A');
+ it('correctly identifies anchor linking to year article', () => {
+ a.title = '2008';
+ assert.ok(onThisDay.testing.isAnchorForYear(a, 2008, ''));
+ });
+ it('correctly rejects anchor linking article starting with a year', ()
=> {
+ a.title = '2008 Something something';
+ assert.ok(!onThisDay.testing.isAnchorForYear(a, 2008, ''));
+ });
+ it('correctly rejects anchor linking article starting with a number',
() => {
+ a.title = '123456 Something something';
+ assert.ok(!onThisDay.testing.isAnchorForYear(a, 2008, ''));
+ });
+ it('correctly rejects anchor linking article not starting with a
year', () => {
+ a.title = 'Something something';
+ assert.ok(!onThisDay.testing.isAnchorForYear(a, 2008, ''));
+ });
+ it('correctly identifies anchor linking to year article with an era
string', () => {
+ a.title = '2008 BC';
+ assert.ok(onThisDay.testing.isAnchorForYear(a, 2008, 'BC'));
+ });
+ it('correctly identifies anchor linking to year article with era
string w/o space', () => {
+ a.title = '55BC';
+ assert.ok(onThisDay.testing.isAnchorForYear(a, 55, 'BC'));
+ });
+ });
});
--
To view, visit https://gerrit.wikimedia.org/r/372909
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Ia8e824c43995ce478679ade0ee94359f48ecce3b
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/services/mobileapps
Gerrit-Branch: master
Gerrit-Owner: Mhurd <[email protected]>
Gerrit-Reviewer: BearND <[email protected]>
Gerrit-Reviewer: Dbrant <[email protected]>
Gerrit-Reviewer: Fjalapeno <[email protected]>
Gerrit-Reviewer: GWicke <[email protected]>
Gerrit-Reviewer: Jdlrobson <[email protected]>
Gerrit-Reviewer: Jhernandez <[email protected]>
Gerrit-Reviewer: Mholloway <[email protected]>
Gerrit-Reviewer: Mobrovac <[email protected]>
Gerrit-Reviewer: Ppchelko <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits