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

Change subject: Pull out size extraction from image handler
......................................................................


Pull out size extraction from image handler

 * To be used for videos as well.

 * Note the assertion of width/height from info.  It's unclear under
   what conditions those properties wouldn't be present.  Even in error
   scenarios, we're providing them.

Change-Id: If8a22e182e20eb02496aa0ad5b50357d9368c4ee
---
M lib/wt2html/tt/LinkHandler.js
1 file changed, 72 insertions(+), 85 deletions(-)

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



diff --git a/lib/wt2html/tt/LinkHandler.js b/lib/wt2html/tt/LinkHandler.js
index ca253ad..a0483f3 100644
--- a/lib/wt2html/tt/LinkHandler.js
+++ b/lib/wt2html/tt/LinkHandler.js
@@ -598,19 +598,25 @@
        cb({tokens: tokens});
 };
 
+/**
+ * Get the format for media.
+ */
+function getFormat(opts) {
+       if (opts.manualthumb) {
+               return "thumbnail";
+       }
+       return opts.format && opts.format.v;
+}
 
 /**
- * Extract the dimensions for an image
+ * Extract the dimensions for media.
  */
-function handleSize(info) {
-       var width, height;
-       if (info.height) {
-               height = info.height;
-       }
+function handleSize(env, opts, info) {
+       var height = info.height;
+       var width = info.width;
 
-       if (info.width) {
-               width = info.width;
-       }
+       console.assert(typeof height === 'number');
+       console.assert(typeof width === 'number');
 
        if (info.thumburl && info.thumbheight) {
                height = info.thumbheight;
@@ -619,20 +625,47 @@
        if (info.thumburl && info.thumbwidth) {
                width = info.thumbwidth;
        }
-       return {
-               height: height,
-               width: width,
-       };
-}
 
-/**
- * Get the format for an image.
- */
-function getFormat(opts) {
-       if (opts.manualthumb) {
-               return "thumbnail";
+       var mustRender;
+       if (info.mustRender !== undefined) {
+               mustRender = info.mustRender;
+       } else {
+               mustRender = info.mediatype !== 'BITMAP';
        }
-       return opts.format && opts.format.v;
+
+       // Handle client-side upscaling (including 'border')
+
+       // Calculate the scaling ratio from the user-specified width and height
+       var ratio = null;
+       if (opts.size.v.height) {
+               ratio = opts.size.v.height / info.height;
+       }
+       if (opts.size.v.width) {
+               var r = opts.size.v.width / info.width;
+               ratio = (ratio === null || r < ratio) ? r : ratio;
+       }
+
+       if (ratio !== null && ratio > 1) {
+               // If the user requested upscaling, then this is denied in the 
thumbnail
+               // and frameless format, except for files with mustRender.
+               var format = getFormat(opts);
+               if (!mustRender && (format === 'thumbnail' || format === 
'frameless')) {
+                       // Upscaling denied
+                       height = info.height;
+                       width = info.width;
+               } else {
+                       // Upscaling allowed
+                       // In the batch API, these will already be correct, but 
the non-batch
+                       // API returns the source width and height whenever 
client-side scaling
+                       // is requested.
+                       if (!env.conf.parsoid.useBatchAPI) {
+                               height = Math.round(info.height * ratio);
+                               width = Math.round(info.width * ratio);
+                       }
+               }
+       }
+
+       return { height: height, width: width };
 }
 
 /**
@@ -949,11 +982,6 @@
                img.addNormalizedAttribute('lang', opts.lang.v, opts.lang.src);
        }
 
-       // T110692: The batching API seems to return these as strings.
-       // Till that is fixed, let us make sure these are numbers.
-       info.height = Number(info.height);
-       info.width = Number(info.width);
-
        if (!dataMw.errors) {
                // Add (read-only) information about original file size (T64881)
                img.addAttribute('data-file-width', String(info.width));
@@ -961,68 +989,22 @@
                img.addAttribute('data-file-type', info.mediatype && 
info.mediatype.toLowerCase());
        }
 
-       var mustRender;
-       if (info.mustRender !== undefined) {
-               mustRender = info.mustRender;
-       } else {
-               mustRender = info.mediatype !== 'BITMAP';
-       }
+       var size = handleSize(this.env, opts, info);
+       img.addNormalizedAttribute('height', String(size.height));
+       img.addNormalizedAttribute('width', String(size.width));
 
-       var size = handleSize(info);
-
-       // Handle client-side upscaling (including 'border')
-       if (info.height && info.width) {
-               // Calculate the scaling ratio from the user-specified width 
and height
-               var ratio = null;
-               if (opts.size.v.height) {
-                       ratio = opts.size.v.height / info.height;
+       // Handle "responsive" images, i.e. srcset
+       if (info.responsiveUrls) {
+               var density;
+               var candidates = [];
+               for (density in info.responsiveUrls) {
+                       candidates.push(
+                               
info.responsiveUrls[density].replace(/^https?:\/\//, '//') +
+                               ' ' + density + 'x');
                }
-               if (opts.size.v.width) {
-                       var r = opts.size.v.width / info.width;
-                       ratio = (ratio === null || r < ratio) ? r : ratio;
+               if (candidates.length > 0) {
+                       img.addAttribute('srcset', candidates.join(', '));
                }
-
-               if (ratio !== null && ratio > 1) {
-                       // If the user requested upscaling, then this is denied 
in the thumbnail
-                       // and frameless format, except for files with 
mustRender.
-                       var format = getFormat(opts);
-                       if (!mustRender && (format === 'thumbnail' || format 
=== 'frameless')) {
-                               // Upscaling denied
-                               size.height = info.height;
-                               size.width = info.width;
-                       } else {
-                               // Upscaling allowed
-                               // In the batch API, these will already be 
correct, but the non-batch
-                               // API returns the source width and height 
whenever client-side scaling
-                               // is requested.
-                               if (!this.env.conf.parsoid.useBatchAPI) {
-                                       size.height = Math.round(info.height * 
ratio);
-                                       size.width = Math.round(info.width * 
ratio);
-                               }
-                       }
-               }
-
-               // Handle "responsive" images, i.e. srcset
-               if (info.responsiveUrls) {
-                       var density;
-                       var candidates = [];
-                       for (density in info.responsiveUrls) {
-                               candidates.push(
-                                       
info.responsiveUrls[density].replace(/^https?:\/\//, '//') +
-                                       ' ' + density + 'x');
-                       }
-                       if (candidates.length > 0) {
-                               img.addAttribute('srcset', candidates.join(', 
'));
-                       }
-               }
-       }
-
-       if (size.height) {
-               img.addNormalizedAttribute('height', String(size.height));
-       }
-
-       if (size.width) {
-               img.addNormalizedAttribute('width', String(size.width));
        }
 
        return {
@@ -1083,6 +1065,11 @@
                dataMw.errors = errs;
        }
 
+       // T110692: The batching API seems to return these as strings.
+       // Till that is fixed, let us make sure these are numbers.
+       info.height = Number(info.height);
+       info.width = Number(info.width);
+
        var o;
        switch (info.mediatype && info.mediatype.toLowerCase()) {
                default:

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

Gerrit-MessageType: merged
Gerrit-Change-Id: If8a22e182e20eb02496aa0ad5b50357d9368c4ee
Gerrit-PatchSet: 9
Gerrit-Project: mediawiki/services/parsoid
Gerrit-Branch: master
Gerrit-Owner: Arlolra <[email protected]>
Gerrit-Reviewer: Arlolra <[email protected]>
Gerrit-Reviewer: C. Scott Ananian <[email protected]>
Gerrit-Reviewer: Subramanya Sastry <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to