| @@ -1,6 +1,37 @@ | ||
| 1 | 1 | /* global jetpackSlideshowSettings */ |
| 2 | 2 | |
| 3 | +/** | |
| 4 | + * Turn a gallery caption into the plain text it is meant to be. | |
| 5 | + * | |
| 6 | + * A caption is meant to read as text, but wptexturize() leaves HTML entities behind, so it needs | |
| 7 | + * decoding before it can be set as one. | |
| 8 | + * | |
| 9 | + * @param {*} caption - Caption value from the gallery data. | |
| 10 | + * @return {string} The decoded caption, as text. | |
| 11 | + */ | |
| 12 | +function jetpackSlideshowCaptionText( caption ) { | |
| 13 | + if ( typeof caption !== 'string' || '' === caption ) { | |
| 14 | + return ''; | |
| 15 | + } | |
| 16 | + | |
| 17 | + var parsed = new DOMParser().parseFromString( caption, 'text/html' ); | |
| 18 | + | |
| 19 | + // These land in the body when they appear mid-caption, and their source is not caption text. | |
| 20 | + parsed.body | |
| 21 | + .querySelectorAll( 'script, style, noscript, template' ) | |
| 22 | + .forEach( function ( element ) { | |
| 23 | + element.remove(); | |
| 24 | + } ); | |
| 25 | + | |
| 26 | + // Without a space in its place, a caption broken over two lines reads as "Ada LovelaceLondon". | |
| 27 | + parsed.body.querySelectorAll( 'br' ).forEach( function ( element ) { | |
| 28 | + element.replaceWith( ' ' ); | |
| 29 | + } ); | |
| 30 | + | |
| 31 | + return parsed.body.textContent; | |
| 32 | +} | |
| 33 | + | |
| 3 | 34 | function JetpackSlideshow( element, transition, autostart ) { |
| 4 | 35 | this.element = element; |
| 5 | 36 | this.images = []; |
| 6 | 37 | this.controls = {}; |
| @@ -38,9 +69,9 @@ | ||
| 38 | 69 | img.nopin = 'nopin'; |
| 39 | 70 | var caption = document.createElement( 'div' ); |
| 40 | 71 | caption.className = 'jetpack-slideshow-slide-caption'; |
| 41 | 72 | caption.setAttribute( 'itemprop', 'caption description' ); |
| 42 | - caption.innerHTML = imageInfo.caption; | |
| 73 | + caption.textContent = jetpackSlideshowCaptionText( imageInfo.caption ); | |
| 43 | 74 | var container = document.createElement( 'div' ); |
| 44 | 75 | container.className = 'jetpack-slideshow-slide'; |
| 45 | 76 | container.setAttribute( 'itemprop', 'associatedMedia' ); |
| 46 | 77 | container.setAttribute( 'itemscope', '' ); |