Skip to content

Commit d224e8a

Browse files
committed
Merge pull request #3218 from Automattic/fix/null-uri-on-canonical
PostNorm: Make images safe before we choose a first pass canonical.
2 parents 3ec623b + fb71651 commit d224e8a

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

client/lib/feed-post-store/normalization-rules.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ const fastPostNormalizationRules = [
2929
postNormalizer.preventWidows,
3030
postNormalizer.makeSiteIDSafeForAPI,
3131
postNormalizer.pickPrimaryTag,
32-
postNormalizer.firstPassCanonicalImage,
3332
postNormalizer.safeImageProperties( READER_CONTENT_WIDTH ),
33+
postNormalizer.firstPassCanonicalImage,
3434
postNormalizer.withContentDOM( [
3535
postNormalizer.content.removeStyles,
3636
postNormalizer.content.safeContentImages( READER_CONTENT_WIDTH ),

client/lib/post-normalizer/index.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,11 @@ var assign = require( 'lodash/object/assign' ),
2828
var formatting = require( 'lib/formatting' ),
2929
safeImageURL = require( 'lib/safe-image-url' );
3030

31-
3231
const DEFAULT_PHOTON_QUALITY = 80, // 80 was chosen after some heuristic testing as the best blend of size and quality
3332
READING_WORDS_PER_SECOND = 250 / 60; // Longreads says that people can read 250 words per minute. We want the rate in words per second.
3433

3534
const imageScaleFactor = ( typeof window !== 'undefined' && window.devicePixelRatio && window.devicePixelRatio > 1 ) ? 2 : 1,
36-
TRANSPARENT_GIF = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7';
35+
TRANSPARENT_GIF = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7';
3736

3837
function debugForPost( post ) {
3938
return function( msg ) {
@@ -57,8 +56,6 @@ function stripAutoPlays( query ) {
5756
return query;
5857
}
5958

60-
61-
6259
/**
6360
* Asynchronously normalizes an object shaped like a post. Works on a copy of the post and does not mutate the original post.
6461
* @param {object} post A post shaped object, generally returned by the API
@@ -331,6 +328,13 @@ normalizePost.safeImageProperties = function( maxWidth ) {
331328
if ( post.canonical_image && post.canonical_image.type === 'image' ) {
332329
makeImageURLSafe( post.canonical_image, 'uri', maxWidth, post.URL );
333330
}
331+
if ( post.attachments ) {
332+
forOwn( post.attachments, function( attachment ) {
333+
if ( startsWith( attachment.mime_type, 'image/' ) ) {
334+
makeImageURLSafe( attachment, 'URL', maxWidth, post.URL );
335+
}
336+
} );
337+
}
334338

335339
callback();
336340
};

client/lib/post-normalizer/test/post-normalizer-test.js

+12
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,24 @@ describe( 'post-normalizer', function() {
216216
featured_media: {
217217
uri: 'http://example.com/media.jpg',
218218
type: 'image'
219+
},
220+
attachments: {
221+
1234: {
222+
mime_type: 'image/png',
223+
URL: 'http://example.com/media.jpg'
224+
},
225+
3456: {
226+
mime_type: 'text/text',
227+
URL: 'http://example.com/media.jpg'
228+
}
219229
}
220230
};
221231
normalizer( post, [ normalizer.safeImageProperties( 200 ) ], function( err, normalized ) {
222232
assert.strictEqual( normalized.author.avatar_URL, 'http://example.com/me.jpg-SAFE?w=200&quality=80&strip=info' );
223233
assert.strictEqual( normalized.featured_image, 'http://foo.bar/-SAFE?w=200&quality=80&strip=info' );
224234
assert.strictEqual( normalized.featured_media.uri, 'http://example.com/media.jpg-SAFE?w=200&quality=80&strip=info' );
235+
assert.strictEqual( normalized.attachments['1234'].URL, 'http://example.com/media.jpg-SAFE?w=200&quality=80&strip=info' )
236+
assert.strictEqual( normalized.attachments['3456'].URL, 'http://example.com/media.jpg' );
225237
done( err );
226238
} );
227239
} );

client/lib/posts/utils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ var utils = {
117117
[
118118
postNormalizer.decodeEntities,
119119
postNormalizer.stripHTML,
120-
postNormalizer.firstPassCanonicalImage,
121120
postNormalizer.safeImageProperties( imageWidth ),
121+
postNormalizer.firstPassCanonicalImage,
122122
postNormalizer.withContentDOM( [
123123
postNormalizer.content.removeStyles,
124124
postNormalizer.content.safeContentImages( imageWidth )

0 commit comments

Comments
 (0)