MediaWiki:Gadget-AbsyFancyBoxy.js: различия между версиями
Ланселот (обсуждение | вклад) Нет описания правки |
Ланселот (обсуждение | вклад) А если через утилиту /n? |
||
| Строка 37: | Строка 37: | ||
if ( w === 0 && h === 0 ) return false; | if ( w === 0 && h === 0 ) return false; | ||
return w < MIN_IMAGE_SIZE && h < MIN_IMAGE_SIZE; | return w < MIN_IMAGE_SIZE && h < MIN_IMAGE_SIZE; | ||
} | |||
// Извлекаем текст подписи, сохраняя <br> как \n (без innerHTML) | |||
function getCaptionText( el ) { | |||
var clone = el.cloneNode( true ); | |||
// Заменяем <br> на текстовый маркер переноса | |||
clone.querySelectorAll( 'br' ).forEach( function ( br ) { | |||
br.replaceWith( document.createTextNode( '\n' ) ); | |||
} ); | |||
return clone.textContent | |||
.replace( /\s+/g, ' ' ) | |||
.replace( /\s*\n\s*/g, '\n' ) | |||
.trim(); | |||
} | } | ||
| Строка 44: | Строка 57: | ||
var galleryText = galleryItem.querySelector( '.gallerytext' ); | var galleryText = galleryItem.querySelector( '.gallerytext' ); | ||
if ( galleryText && galleryText.textContent.trim() ) { | if ( galleryText && galleryText.textContent.trim() ) { | ||
return galleryText | return getCaptionText( galleryText ); | ||
} | } | ||
} | } | ||
| Строка 51: | Строка 64: | ||
var thumbCaption = container.querySelector( '.thumbcaption, figcaption' ); | var thumbCaption = container.querySelector( '.thumbcaption, figcaption' ); | ||
if ( thumbCaption && thumbCaption.textContent.trim() ) { | if ( thumbCaption && thumbCaption.textContent.trim() ) { | ||
return thumbCaption | return getCaptionText( thumbCaption ); | ||
} | } | ||
} | } | ||
| Строка 265: | Строка 278: | ||
imgElement.alt = item.caption || ''; | imgElement.alt = item.caption || ''; | ||
// Подпись: | // Подпись: переносы \n превращаем в настоящие <br> (безопасно) | ||
captionElement.textContent = ''; | captionElement.textContent = ''; | ||
var captionText = item.caption || ''; | var captionText = item.caption || ''; | ||
if ( captionText ) { | if ( captionText ) { | ||
var parts = captionText.split( | var parts = captionText.split( '\n' ); | ||
parts.forEach( function ( part, i ) { | parts.forEach( function ( part, i ) { | ||
if ( i > 0 ) captionElement.appendChild( document.createElement( 'br' ) ); | if ( i > 0 ) captionElement.appendChild( document.createElement( 'br' ) ); | ||