Merge "Simplify conversion of images"

This commit is contained in:
jenkins-bot 2018-06-07 20:51:51 +00:00 committed by Gerrit Code Review
commit c04e934361
5 changed files with 39 additions and 61 deletions

View file

@ -25,7 +25,7 @@ ve.ce.MWInlineImageNode = function VeCeMWInlineImageNode( model, config ) {
.text( model.getFilename() );
$image = $( [] );
} else {
if ( model.getAttribute( 'isLinked' ) ) {
if ( model.getAttribute( 'href' ) ) {
this.$element = $( '<a>' ).addClass( 'image' );
$image = $( '<img>' ).appendTo( this.$element );
} else {

View file

@ -572,11 +572,6 @@ ve.dm.MWImageModel.prototype.getUpdatedAttributes = function () {
attrs.href = this.getImageHref();
attrs.resource = this.getImageResourceName();
// If converting from block to inline, set isLinked=true to avoid |link=
if ( origAttrs.isLinked === undefined && this.getImageNodeType() === 'mwInlineImage' ) {
attrs.isLinked = true;
}
return attrs;
};

View file

@ -68,26 +68,17 @@ ve.dm.MWBlockImageNode.static.classAttributes = {
ve.dm.MWBlockImageNode.static.toDataElement = function ( domElements, converter ) {
var dataElement, newDimensions, attributes,
figure, imgWrapper, img, caption,
classAttr, typeofAttrs, errorIndex, width, height, altText, types;
// Workaround for jQuery's .children() being expensive due to
// https://github.com/jquery/sizzle/issues/311
function findChildren( parent, nodeNames ) {
return Array.prototype.filter.call( parent.childNodes, function ( element ) {
return nodeNames.indexOf( element.nodeName.toLowerCase() ) !== -1;
} );
}
classAttr, typeofAttrs, errorIndex, width, height, types;
figure = domElements[ 0 ];
imgWrapper = findChildren( figure, [ 'a', 'span' ] )[ 0 ] || null;
img = imgWrapper && findChildren( imgWrapper, [ 'img', 'video' ] )[ 0 ] || null;
caption = findChildren( figure, [ 'figcaption' ] )[ 0 ] || null;
imgWrapper = figure.children[ 0 ]; // <a> or <span>
img = imgWrapper.children[ 0 ]; // <img> or <video>
caption = figure.children[ 1 ]; // <figcaption> or undefined
classAttr = figure.getAttribute( 'class' );
typeofAttrs = figure.getAttribute( 'typeof' ).split( ' ' );
errorIndex = typeofAttrs.indexOf( 'mw:Error' );
width = img && img.getAttribute( 'width' );
height = img && img.getAttribute( 'height' );
altText = img && img.getAttribute( 'alt' );
width = img.getAttribute( 'width' );
height = img.getAttribute( 'height' );
if ( errorIndex !== -1 ) {
typeofAttrs.splice( errorIndex, 1 );
@ -98,25 +89,19 @@ ve.dm.MWBlockImageNode.static.toDataElement = function ( domElements, converter
attributes = {
mediaClass: types.mediaClass,
type: types.frameType,
href: ( imgWrapper && imgWrapper.getAttribute( 'href' ) ) || '',
src: ( img && ( img.getAttribute( 'src' ) || img.getAttribute( 'poster' ) ) ) || '',
resource: img && img.getAttribute( 'resource' )
src: img.getAttribute( 'src' ) || img.getAttribute( 'poster' ),
href: imgWrapper.getAttribute( 'href' ),
resource: img.getAttribute( 'resource' ),
width: width !== null && width !== '' ? +width : null,
height: height !== null && height !== '' ? +height : null,
alt: img.getAttribute( 'alt' ),
isError: errorIndex !== -1
};
if ( altText !== null ) {
attributes.alt = altText;
}
if ( errorIndex !== -1 ) {
attributes.isError = true;
}
this.setClassAttributes( attributes, classAttr );
attributes.align = attributes.align || 'default';
attributes.width = width !== null && width !== '' ? Number( width ) : null;
attributes.height = height !== null && height !== '' ? Number( height ) : null;
// Default-size
if ( attributes.defaultSize ) {
// Force wiki-default size for thumb and frameless
@ -166,7 +151,7 @@ ve.dm.MWBlockImageNode.static.toDomElements = function ( data, doc, converter )
dataElement = data[ 0 ],
mediaClass = dataElement.attributes.mediaClass,
figure = doc.createElement( 'figure' ),
imgWrapper = doc.createElement( dataElement.attributes.href !== '' ? 'a' : 'span' ),
imgWrapper = doc.createElement( dataElement.attributes.href ? 'a' : 'span' ),
img = doc.createElement( mediaClass === 'Image' ? 'img' : 'video' ),
wrapper = doc.createElement( 'div' ),
classAttr = this.getClassAttrFromAttributes( dataElement.attributes ),
@ -179,7 +164,7 @@ ve.dm.MWBlockImageNode.static.toDomElements = function ( data, doc, converter )
figure.className = classAttr;
}
if ( dataElement.attributes.href !== '' ) {
if ( dataElement.attributes.href ) {
imgWrapper.setAttribute( 'href', dataElement.attributes.href );
}
@ -200,7 +185,7 @@ ve.dm.MWBlockImageNode.static.toDomElements = function ( data, doc, converter )
img.setAttribute( 'width', width );
img.setAttribute( 'height', height );
img.setAttribute( 'resource', dataElement.attributes.resource );
if ( dataElement.attributes.alt !== undefined ) {
if ( typeof dataElement.attributes.alt === 'string' ) {
img.setAttribute( 'alt', dataElement.attributes.alt );
}
figure.appendChild( imgWrapper );

View file

@ -52,15 +52,15 @@ ve.dm.MWInlineImageNode.static.blacklistedAnnotationTypes = [ 'link' ];
ve.dm.MWInlineImageNode.static.toDataElement = function ( domElements, converter ) {
var dataElement, attributes, types,
$figureInline = $( domElements[ 0 ] ),
$firstChild = $figureInline.children().first(), // could be <span> or <a>
$img = $firstChild.children().first(),
typeofAttrs = $figureInline.attr( 'typeof' ).split( ' ' ),
classes = $figureInline.attr( 'class' ),
figureInline = domElements[ 0 ],
imgWrapper = figureInline.children[ 0 ], // could be <span> or <a>
img = imgWrapper.children[ 0 ],
typeofAttrs = ( figureInline.getAttribute( 'typeof' ) || '' ).split( ' ' ),
classes = figureInline.getAttribute( 'class' ),
recognizedClasses = [],
errorIndex = typeofAttrs.indexOf( 'mw:Error' ),
width = $img.attr( 'width' ),
height = $img.attr( 'height' );
width = img.getAttribute( 'width' ),
height = img.getAttribute( 'height' );
if ( errorIndex !== -1 ) {
typeofAttrs.splice( errorIndex, 1 );
@ -71,23 +71,16 @@ ve.dm.MWInlineImageNode.static.toDataElement = function ( domElements, converter
attributes = {
mediaClass: types.mediaClass,
type: types.frameType,
src: $img.attr( 'src' ),
resource: $img.attr( 'resource' ),
originalClasses: classes
src: img.getAttribute( 'src' ),
href: imgWrapper.getAttribute( 'href' ),
resource: img.getAttribute( 'resource' ),
originalClasses: classes,
width: width !== null && width !== '' ? +width : null,
height: height !== null && height !== '' ? +height : null,
alt: img.getAttribute( 'alt' ),
isError: errorIndex !== -1
};
if ( errorIndex !== -1 ) {
attributes.isError = true;
}
attributes.width = width !== undefined && width !== '' ? Number( width ) : null;
attributes.height = height !== undefined && height !== '' ? Number( height ) : null;
attributes.isLinked = $firstChild.is( 'a' );
if ( attributes.isLinked ) {
attributes.href = $firstChild.attr( 'href' );
}
// Extract individual classes
classes = typeof classes === 'string' ? classes.trim().split( /\s+/ ) : [];
@ -169,7 +162,7 @@ ve.dm.MWInlineImageNode.static.toDomElements = function ( data, doc ) {
figureInline.className = classes.join( ' ' );
}
if ( data.attributes.isLinked ) {
if ( data.attributes.href ) {
firstChild = doc.createElement( 'a' );
firstChild.setAttribute( 'href', data.attributes.href );
} else {

View file

@ -267,6 +267,8 @@ ve.dm.mwExample.MWBlockImage = {
src: ve.ce.minImgDataUri,
width: 1,
height: 2,
alt: null,
isError: false,
resource: 'FooBar',
originalClasses: 'mw-halign-right foobar',
unrecognizedClasses: [ 'foobar' ]
@ -299,7 +301,8 @@ ve.dm.mwExample.MWInlineImage = {
mediaClass: 'Image',
width: 135,
height: 155,
isLinked: true,
alt: null,
isError: false,
valign: 'text-top',
resource: './File:Wiki.png',
type: 'none',
@ -1506,6 +1509,8 @@ ve.dm.mwExample.domToDataCases = {
src: ve.ce.minImgDataUri,
width: 1,
height: 2,
alt: null,
isError: false,
resource: 'FooBar'
}
},