jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/392677 )

Change subject: Hygiene: Refine processing of media items in Parsoid HTML
......................................................................


Hygiene: Refine processing of media items in Parsoid HTML

Updates the logic to better reflect the Parsoid HTML spec. This will
provide a more solid base for updating the response with additional info.

https://www.mediawiki.org/wiki/Specs/HTML/1.5.0

No change to the output is intended here.

Change-Id: Ie72746f7b78db92624a59e2ab1e45a06312b5fd3
---
M lib/media.js
M routes/media.js
2 files changed, 45 insertions(+), 17 deletions(-)

Approvals:
  BearND: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/lib/media.js b/lib/media.js
index 2884740..fe541ee 100644
--- a/lib/media.js
+++ b/lib/media.js
@@ -1,13 +1,43 @@
 'use strict';
 
 const api = require('./api-util');
-const NodeType = require('./nodeType');
 
 const MIN_IMAGE_SIZE = 64;
 const MAX_IMAGE_WIDTH = 1280;
 
-const tagNamesToTypes = { IMG: "image", VIDEO: "video" };
+const SELECTORS = [
+    'figure[typeof^=mw:Image]',
+    'figure[typeof^=mw:Video]',
+    'span[typeof^=mw:Image]',
+    'span[typeof^=mw:Video]'
+];
 
+/**
+ * A MediaWiki media type as represented in Parsoid HTML.
+ * @param {!String} resourceSelector the selector for the child element 
containing the core resource
+ * @param {!String} name the image type as referred to internally and in the 
endpoint response
+ */
+class MediaType {
+    constructor(resourceSelector, name) {
+        this.resourceSelector = resourceSelector;
+        this.name = name;
+    }
+}
+
+const Image = new MediaType('img', 'image');
+const Video = new MediaType('video', 'video');
+const Unknown = new MediaType(null, 'unknown');
+
+function getMediaType(typeofAttr) {
+    switch (typeofAttr.slice(0, 8)) {
+        case 'mw:Image':
+            return Image;
+        case 'mw:Video':
+            return Video;
+        default:
+            return Unknown;
+    }
+}
 
 function getExtMetadataValues(extmetadata) {
     const ext = {};
@@ -24,24 +54,21 @@
  *          appearance
  */
 function getMediaItemInfoFromPage(selection) {
-    return [].map.call(selection, (node) => {
-        const type = tagNamesToTypes[node.tagName];
+    return [].map.call(selection, (elem) => {
+        const mediaType = getMediaType(elem.getAttribute('typeof'));
+        const resourceElem = elem.querySelector(mediaType.resourceSelector);
         let startTime;
         let endTime;
-        if (type === 'video') {
-            let parent = node.parentNode;
-            while (parent) {
-                if (parent.nodeType === NodeType.ELEMENT_NODE && 
parent.getAttribute('data-mw')) {
-                    const data = JSON.parse(parent.getAttribute('data-mw'));
-                    startTime = data.starttime;
-                    endTime = data.endtime;
-                }
-                parent = parent.parentNode;
+        if (mediaType === Video) {
+            const dataMw = JSON.parse(elem.getAttribute('data-mw'));
+            if (dataMw) {
+                startTime = dataMw.starttime;
+                endTime = dataMw.endtime;
             }
         }
         return {
-            title: node.getAttribute('resource').replace(/^.\//, ''),
-            type,
+            title: resourceElem && 
resourceElem.getAttribute('resource').replace(/^.\//, ''),
+            type: mediaType.name,
             start_time: startTime,
             end_time: endTime
         };
@@ -101,5 +128,6 @@
 module.exports = {
     getMediaItemInfoFromPage,
     getMetadataFromApi,
-    filterResult
+    filterResult,
+    SELECTORS
 };
diff --git a/routes/media.js b/routes/media.js
index 7a7fdc2..0ada4c8 100644
--- a/routes/media.js
+++ b/routes/media.js
@@ -21,7 +21,7 @@
         const headers = response.headers;
         const doc = domino.createDocument(response.body);
         // todo: handle Mathoid-rendered math images
-        const selection = doc.querySelectorAll('*[typeof^=mw:Image] 
img,*[typeof^=mw:Video] video');
+        const selection = doc.querySelectorAll(media.SELECTORS.join(','));
         if (!selection) {
             res.send({ items: [] });
             return;

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ie72746f7b78db92624a59e2ab1e45a06312b5fd3
Gerrit-PatchSet: 6
Gerrit-Project: mediawiki/services/mobileapps
Gerrit-Branch: master
Gerrit-Owner: Mholloway <[email protected]>
Gerrit-Reviewer: BearND <[email protected]>
Gerrit-Reviewer: Fjalapeno <[email protected]>
Gerrit-Reviewer: Jdlrobson <[email protected]>
Gerrit-Reviewer: Mholloway <[email protected]>
Gerrit-Reviewer: Mhurd <[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

Reply via email to