BearND has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/397867 )

Change subject: References: citation type
......................................................................

References: citation type

Instead of an citations array we return a single value in the type
field.
We still collect all citation types in a set.
If there is exactly one type left and it's one of the known values
("web", "news", "journal", or "book") then that value is returned.
Else "generic" is returned.

Bug: T182652
Change-Id: Ic703fbdf642661481b53e3af21eba1a3583366a0
---
M lib/references/structureReferenceListContent.js
M test/lib/references/structureReferenceListContent.test.js
2 files changed, 34 insertions(+), 25 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/mobileapps 
refs/changes/67/397867/1

diff --git a/lib/references/structureReferenceListContent.js 
b/lib/references/structureReferenceListContent.js
index cbf6064..e6af549 100644
--- a/lib/references/structureReferenceListContent.js
+++ b/lib/references/structureReferenceListContent.js
@@ -6,6 +6,9 @@
 */
 
 const NodeType = require('../nodeType');
+const CITATION_TYPES = {
+    'web': 1, 'news': 1, 'journal': 1, 'book': 1
+};
 
 const hasOnlyWhitespace = node =>
     node.nodeType === NodeType.TEXT_NODE && /^\s*$/.test(node.textContent);
@@ -65,6 +68,20 @@
     content.html = content.html ? content.html + html : html;
 };
 
+const isKnownCitationType = (type) => {
+    return CITATION_TYPES[type];
+};
+
+const getCitationType = (citations) => {
+    if (citations.size === 1) {
+        const value = citations.values().next().value;
+        if (isKnownCitationType(value)) {
+            return value;
+        }
+    }
+    return 'generic';
+};
+
 /**
  * Returns reference content in an html string and citations set.
  * @param {!Element} spanElement 'span.mw-reference-text'
@@ -85,12 +102,8 @@
         }
     }
 
-    if (content.citations.size === 0) {
-        delete content.citations;
-    } else {
-        content.citations = Array.from(content.citations);
-    }
-
+    content.type = getCitationType(content.citations);
+    delete content.citations;
     return content;
 };
 
@@ -121,16 +134,12 @@
         element = element.nextElementSibling;
     }
 
-    const result = {
+    return {
         id: getCiteNoteId(listItemElement),
         back_links: backLinks,
         content: content.html,
-        citations: content.citations
+        type: content.type
     };
-    if (!result.citations) {
-        delete result.citations;
-    }
-    return result;
 };
 
 /**
diff --git a/test/lib/references/structureReferenceListContent.test.js 
b/test/lib/references/structureReferenceListContent.test.js
index e4e6b48..027a9d1 100644
--- a/test/lib/references/structureReferenceListContent.test.js
+++ b/test/lib/references/structureReferenceListContent.test.js
@@ -111,7 +111,8 @@
             const doc = createDocument(simpleDogRef);
             assert.deepEqual(
                 
mut.unit.getReferenceContent(doc.querySelector('span.mw-reference-text')), {
-                    html: 'some HTML'
+                    html: 'some HTML',
+                    type: 'generic'
                 });
             assert.ok(logger.log.notCalled);
         });
@@ -121,7 +122,7 @@
             assert.deepEqual(
                 
mut.unit.getReferenceContent(doc.querySelector('span.mw-reference-text')), {
                     html: '<cite class="citation journal">cite 
1</cite><span>more HTML</span>',
-                    citations: [ 'journal' ]
+                    type: 'journal'
                 });
             assert.ok(logger.log.notCalled);
         });
@@ -131,10 +132,7 @@
             assert.deepEqual(
                 
mut.unit.getReferenceContent(doc.querySelector('span.mw-reference-text')), {
                     html: ulRefContent,
-                    citations: [
-                        'web',
-                        'book'
-                    ]
+                    type: 'generic'
                 });
             assert.ok(logger.log.notCalled);
         });
@@ -143,7 +141,8 @@
             const doc = createDocument(indianFilmsRef);
             assert.deepEqual(
                 
mut.unit.getReferenceContent(doc.querySelector('span.mw-reference-text')), {
-                    html: indianFilmsRefContent
+                    html: indianFilmsRefContent,
+                    type: 'generic'
                 });
             assert.ok(logger.log.notCalled);
         });
@@ -154,11 +153,12 @@
             const doc = createDocument(simpleDogRef);
             const result = 
mut.unit.buildOneReferenceItem(doc.querySelector('li'), logger);
             assert.deepEqual(result, {
-                back_links: [ {
+                back_links: [{
                     "href": "./Dog#cite_ref-101",
                     "text": "↑"
                 }],
                 content: 'some HTML',
+                type: 'generic',
                 id: '101'
             });
             assert.ok(logger.log.notCalled);
@@ -176,9 +176,7 @@
                     "text": "2"
                 }],
                 content: '<cite class="citation journal">cite 
1</cite><span>more HTML</span>',
-                citations: [
-                    'journal'
-                ],
+                type: 'journal',
                 id: 'perri2016-13'
             });
             assert.ok(logger.log.notCalled);
@@ -195,7 +193,8 @@
                         "href": "./Dog#cite_ref-101",
                         "text": "↑"
                     }],
-                    content: 'some HTML'
+                    content: 'some HTML',
+                    type: 'generic'
                 }
             });
             assert.deepEqual(result.order, [ '101' ]);
@@ -211,7 +210,8 @@
                         "href": 
"./List_of_most_viewed_YouTube_videos#cite_ref-4",
                         "text": "↑"
                     }],
-                    content: mostViewedYoutubeVideosContent
+                    content: mostViewedYoutubeVideosContent,
+                    type: 'generic'
                 }
             });
             assert.deepEqual(result.order, [ '4' ]);

-- 
To view, visit https://gerrit.wikimedia.org/r/397867
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic703fbdf642661481b53e3af21eba1a3583366a0
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/services/mobileapps
Gerrit-Branch: master
Gerrit-Owner: BearND <bsitzm...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to