mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-11-15 10:35:48 +00:00
Merge "Fix class attribute preservation for images"
This commit is contained in:
commit
22db613b34
|
@ -50,6 +50,7 @@ ve.dm.MWBlockImageNode.static.toDataElement = function ( domElements, converter
|
||||||
$caption = $figure.children( 'figcaption' ).eq( 0 ),
|
$caption = $figure.children( 'figcaption' ).eq( 0 ),
|
||||||
typeofAttr = $figure.attr( 'typeof' ),
|
typeofAttr = $figure.attr( 'typeof' ),
|
||||||
classes = $figure.attr( 'class' ),
|
classes = $figure.attr( 'class' ),
|
||||||
|
recognizedClasses = [],
|
||||||
attributes = {
|
attributes = {
|
||||||
href: $a.attr( 'href' ),
|
href: $a.attr( 'href' ),
|
||||||
src: $img.attr( 'src' ),
|
src: $img.attr( 'src' ),
|
||||||
|
@ -60,8 +61,7 @@ ve.dm.MWBlockImageNode.static.toDataElement = function ( domElements, converter
|
||||||
};
|
};
|
||||||
|
|
||||||
// Extract individual classes
|
// Extract individual classes
|
||||||
classes = typeof classes === 'string' ?
|
classes = typeof classes === 'string' ? classes.trim().split( /\s+/ ) : [];
|
||||||
classes.replace( /\s{2,}/g, ' ' ).split( ' ' ) : [];
|
|
||||||
|
|
||||||
// Type
|
// Type
|
||||||
switch ( typeofAttr ) {
|
switch ( typeofAttr ) {
|
||||||
|
@ -82,12 +82,16 @@ ve.dm.MWBlockImageNode.static.toDataElement = function ( domElements, converter
|
||||||
// Horizontal alignment
|
// Horizontal alignment
|
||||||
if ( classes.indexOf( 'mw-halign-left' ) !== -1 ) {
|
if ( classes.indexOf( 'mw-halign-left' ) !== -1 ) {
|
||||||
attributes.align = 'left';
|
attributes.align = 'left';
|
||||||
|
recognizedClasses.push( 'mw-halign-left' );
|
||||||
} else if ( classes.indexOf( 'mw-halign-right' ) !== -1 ) {
|
} else if ( classes.indexOf( 'mw-halign-right' ) !== -1 ) {
|
||||||
attributes.align = 'right';
|
attributes.align = 'right';
|
||||||
|
recognizedClasses.push( 'mw-halign-right' );
|
||||||
} else if ( classes.indexOf( 'mw-halign-center' ) !== -1 ) {
|
} else if ( classes.indexOf( 'mw-halign-center' ) !== -1 ) {
|
||||||
attributes.align = 'center';
|
attributes.align = 'center';
|
||||||
|
recognizedClasses.push( 'mw-halign-center' );
|
||||||
} else if ( classes.indexOf( 'mw-halign-none' ) !== -1 ) {
|
} else if ( classes.indexOf( 'mw-halign-none' ) !== -1 ) {
|
||||||
attributes.align = 'none';
|
attributes.align = 'none';
|
||||||
|
recognizedClasses.push( 'mw-halign-none' );
|
||||||
} else {
|
} else {
|
||||||
attributes.align = 'default';
|
attributes.align = 'default';
|
||||||
}
|
}
|
||||||
|
@ -95,8 +99,12 @@ ve.dm.MWBlockImageNode.static.toDataElement = function ( domElements, converter
|
||||||
// Default-size
|
// Default-size
|
||||||
if ( classes.indexOf( 'mw-default-size' ) !== -1 ) {
|
if ( classes.indexOf( 'mw-default-size' ) !== -1 ) {
|
||||||
attributes.defaultSize = true;
|
attributes.defaultSize = true;
|
||||||
|
recognizedClasses.push( 'mw-default-size' );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Store unrecognized classes so we can restore them on the way out
|
||||||
|
attributes.unrecognizedClasses = ve.simpleArrayDifference( classes, recognizedClasses );
|
||||||
|
|
||||||
if ( $caption.length === 0 ) {
|
if ( $caption.length === 0 ) {
|
||||||
return [
|
return [
|
||||||
{ 'type': 'mwBlockImage', 'attributes': attributes },
|
{ 'type': 'mwBlockImage', 'attributes': attributes },
|
||||||
|
@ -161,7 +169,14 @@ ve.dm.MWBlockImageNode.static.toDomElements = function ( data, doc, converter )
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( originalClasses && ve.compare( originalClasses.split( ' ' ).sort(), classes.sort() ) ) {
|
if ( dataElement.attributes.unrecognizedClasses ) {
|
||||||
|
classes = ve.simpleArrayUnion( classes, dataElement.attributes.unrecognizedClasses );
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
originalClasses &&
|
||||||
|
ve.compare( originalClasses.trim().split( /\s+/ ).sort(), classes.sort() )
|
||||||
|
) {
|
||||||
figure.className = originalClasses;
|
figure.className = originalClasses;
|
||||||
} else if ( classes.length > 0 ) {
|
} else if ( classes.length > 0 ) {
|
||||||
figure.className = classes.join( ' ' );
|
figure.className = classes.join( ' ' );
|
||||||
|
|
|
@ -45,9 +45,11 @@ ve.dm.MWInlineImageNode.static.toDataElement = function ( domElements ) {
|
||||||
$img = $firstChild.children().first(),
|
$img = $firstChild.children().first(),
|
||||||
typeofAttr = $span.attr( 'typeof' ),
|
typeofAttr = $span.attr( 'typeof' ),
|
||||||
classes = $span.attr( 'class' ),
|
classes = $span.attr( 'class' ),
|
||||||
|
recognizedClasses = [],
|
||||||
attributes = {
|
attributes = {
|
||||||
src: $img.attr( 'src' ),
|
src: $img.attr( 'src' ),
|
||||||
resource: $img.attr( 'resource' )
|
resource: $img.attr( 'resource' ),
|
||||||
|
originalClasses: classes
|
||||||
},
|
},
|
||||||
width = $img.attr( 'width' ),
|
width = $img.attr( 'width' ),
|
||||||
height = $img.attr( 'height' );
|
height = $img.attr( 'height' );
|
||||||
|
@ -61,8 +63,7 @@ ve.dm.MWInlineImageNode.static.toDataElement = function ( domElements ) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract individual classes
|
// Extract individual classes
|
||||||
classes = typeof classes === 'string' ?
|
classes = typeof classes === 'string' ? classes.trim().split( /\s+/ ) : [];
|
||||||
classes.replace( /\s{2,}/g, ' ' ).split( ' ' ) : [];
|
|
||||||
|
|
||||||
// Type
|
// Type
|
||||||
switch ( typeofAttr ) {
|
switch ( typeofAttr ) {
|
||||||
|
@ -77,20 +78,28 @@ ve.dm.MWInlineImageNode.static.toDataElement = function ( domElements ) {
|
||||||
// Vertical alignment
|
// Vertical alignment
|
||||||
if ( classes.indexOf( 'mw-valign-middle' ) !== -1 ) {
|
if ( classes.indexOf( 'mw-valign-middle' ) !== -1 ) {
|
||||||
attributes.valign = 'middle';
|
attributes.valign = 'middle';
|
||||||
|
recognizedClasses.push( 'mw-valign-middle' );
|
||||||
} else if ( classes.indexOf( 'mw-valign-baseline' ) !== -1 ) {
|
} else if ( classes.indexOf( 'mw-valign-baseline' ) !== -1 ) {
|
||||||
attributes.valign = 'baseline';
|
attributes.valign = 'baseline';
|
||||||
|
recognizedClasses.push( 'mw-valign-baseline' );
|
||||||
} else if ( classes.indexOf( 'mw-valign-sub' ) !== -1 ) {
|
} else if ( classes.indexOf( 'mw-valign-sub' ) !== -1 ) {
|
||||||
attributes.valign = 'sub';
|
attributes.valign = 'sub';
|
||||||
|
recognizedClasses.push( 'mw-valign-sub' );
|
||||||
} else if ( classes.indexOf( 'mw-valign-super' ) !== -1 ) {
|
} else if ( classes.indexOf( 'mw-valign-super' ) !== -1 ) {
|
||||||
attributes.valign = 'super';
|
attributes.valign = 'super';
|
||||||
|
recognizedClasses.push( 'mw-valign-super' );
|
||||||
} else if ( classes.indexOf( 'mw-valign-top' ) !== -1 ) {
|
} else if ( classes.indexOf( 'mw-valign-top' ) !== -1 ) {
|
||||||
attributes.valign = 'top';
|
attributes.valign = 'top';
|
||||||
|
recognizedClasses.push( 'mw-valign-top' );
|
||||||
} else if ( classes.indexOf( 'mw-valign-text-top' ) !== -1 ) {
|
} else if ( classes.indexOf( 'mw-valign-text-top' ) !== -1 ) {
|
||||||
attributes.valign = 'text-top';
|
attributes.valign = 'text-top';
|
||||||
|
recognizedClasses.push( 'mw-valign-text-top' );
|
||||||
} else if ( classes.indexOf( 'mw-valign-bottom' ) !== -1 ) {
|
} else if ( classes.indexOf( 'mw-valign-bottom' ) !== -1 ) {
|
||||||
attributes.valign = 'bottom';
|
attributes.valign = 'bottom';
|
||||||
|
recognizedClasses.push( 'mw-valign-bottom' );
|
||||||
} else if ( classes.indexOf( 'mw-valign-text-bottom' ) !== -1 ) {
|
} else if ( classes.indexOf( 'mw-valign-text-bottom' ) !== -1 ) {
|
||||||
attributes.valign = 'text-bottom';
|
attributes.valign = 'text-bottom';
|
||||||
|
recognizedClasses.push( 'mw-valign-text-bottom' );
|
||||||
} else {
|
} else {
|
||||||
attributes.valign = 'default';
|
attributes.valign = 'default';
|
||||||
}
|
}
|
||||||
|
@ -98,19 +107,27 @@ ve.dm.MWInlineImageNode.static.toDataElement = function ( domElements ) {
|
||||||
// Border
|
// Border
|
||||||
if ( classes.indexOf( 'mw-image-border' ) !== -1 ) {
|
if ( classes.indexOf( 'mw-image-border' ) !== -1 ) {
|
||||||
attributes.border = true;
|
attributes.border = true;
|
||||||
|
recognizedClasses.push( 'mw-image-border' );
|
||||||
}
|
}
|
||||||
|
|
||||||
// Default-size
|
// Default-size
|
||||||
if ( classes.indexOf( 'mw-default-size' ) !== -1 ) {
|
if ( classes.indexOf( 'mw-default-size' ) !== -1 ) {
|
||||||
attributes.defaultSize = true;
|
attributes.defaultSize = true;
|
||||||
|
recognizedClasses.push( 'mw-default-size' );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Store unrecognized classes so we can restore them on the way out
|
||||||
|
attributes.unrecognizedClasses = ve.simpleArrayDifference( classes, recognizedClasses );
|
||||||
|
|
||||||
return { 'type': 'mwInlineImage', 'attributes': attributes };
|
return { 'type': 'mwInlineImage', 'attributes': attributes };
|
||||||
};
|
};
|
||||||
|
|
||||||
ve.dm.MWInlineImageNode.static.toDomElements = function ( data, doc ) {
|
ve.dm.MWInlineImageNode.static.toDomElements = function ( data, doc ) {
|
||||||
var span = doc.createElement( 'span' ),
|
var firstChild,
|
||||||
|
span = doc.createElement( 'span' ),
|
||||||
img = doc.createElement( 'img' ),
|
img = doc.createElement( 'img' ),
|
||||||
firstChild;
|
classes = [],
|
||||||
|
originalClasses = data.attributes.originalClasses;
|
||||||
|
|
||||||
ve.setDomAttributes( img, data.attributes, [ 'src', 'width', 'height', 'resource' ] );
|
ve.setDomAttributes( img, data.attributes, [ 'src', 'width', 'height', 'resource' ] );
|
||||||
|
|
||||||
|
@ -124,15 +141,28 @@ ve.dm.MWInlineImageNode.static.toDomElements = function ( data, doc ) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( data.attributes.defaultSize ) {
|
if ( data.attributes.defaultSize ) {
|
||||||
span.className += ' mw-default-size';
|
classes.push( 'mw-default-size' );
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( data.attributes.border ) {
|
if ( data.attributes.border ) {
|
||||||
span.className += ' mw-image-border';
|
classes.push( 'mw-image-border' );
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( data.attributes.valign && data.attributes.valign !== 'default' ) {
|
if ( data.attributes.valign && data.attributes.valign !== 'default' ) {
|
||||||
span.className += ' mw-image-' + data.attributes.valign;
|
classes.push( 'mw-valign-' + data.attributes.valign );
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( data.attributes.unrecognizedClasses ) {
|
||||||
|
classes = ve.simpleArrayUnion( classes, data.attributes.unrecognizedClasses );
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
originalClasses &&
|
||||||
|
ve.compare( originalClasses.trim().split( /\s+/ ).sort(), classes.sort() )
|
||||||
|
) {
|
||||||
|
span.className = originalClasses;
|
||||||
|
} else if ( classes.length > 0 ) {
|
||||||
|
span.className = classes.join( ' ' );
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( data.attributes.isLinked ) {
|
if ( data.attributes.isLinked ) {
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
*/
|
*/
|
||||||
ve.dm.mwExample = {};
|
ve.dm.mwExample = {};
|
||||||
|
|
||||||
ve.dm.mwExample.MWInlineImageHtml = '<span typeof="mw:Image" data-parsoid="{"tsr":[0,24],"optList":[{"ck":"width","ak":"500px"}],"cacheKey":"[[Image:Wiki.png|500px]]","img":{"h":155,"w":135,"wdset":true},"dsr":[0,24,null,null]}"><a href="./File:Wiki.png" data-parsoid="{"a":{"href":"./File:Wiki.png"}}"><img resource="./File:Wiki.png" src="http://upload.wikimedia.org/wikipedia/en/b/bc/Wiki.png" height="155" width="135" data-parsoid="{"a":{"resource":"./File:Wiki.png","width":"135"},"sa":{"resource":"Image:Wiki.png","width":"500"}}"></a></span>';
|
ve.dm.mwExample.MWInlineImageHtml = '<span typeof="mw:Image" class="foo mw-valign-text-top" data-parsoid="{"tsr":[0,24],"optList":[{"ck":"width","ak":"500px"}],"cacheKey":"[[Image:Wiki.png|500px]]","img":{"h":155,"w":135,"wdset":true},"dsr":[0,24,null,null]}"><a href="./File:Wiki.png" data-parsoid="{"a":{"href":"./File:Wiki.png"}}"><img resource="./File:Wiki.png" src="http://upload.wikimedia.org/wikipedia/en/b/bc/Wiki.png" height="155" width="135" data-parsoid="{"a":{"resource":"./File:Wiki.png","width":"135"},"sa":{"resource":"Image:Wiki.png","width":"500"}}"></a></span>';
|
||||||
ve.dm.mwExample.MWTransclusion = {
|
ve.dm.mwExample.MWTransclusion = {
|
||||||
'blockSpan': '<span about="#mwt1" typeof="mw:Transclusion" data-mw="{"target":{"wt":"Test"},"params":{"1":{"wt":"Hello, world!"}},"id":"mwt1"}" data-parsoid="{"tsr":[18,40],"src":"{{Test|Hello, world!}}","dsr":[18,40,null,null]}"></span>',
|
'blockSpan': '<span about="#mwt1" typeof="mw:Transclusion" data-mw="{"target":{"wt":"Test"},"params":{"1":{"wt":"Hello, world!"}},"id":"mwt1"}" data-parsoid="{"tsr":[18,40],"src":"{{Test|Hello, world!}}","dsr":[18,40,null,null]}"></span>',
|
||||||
'blockSpanModified': '<span about="#mwt1" typeof="mw:Transclusion" data-mw="{"id":"mwt1","target":{"wt":"Test"},"params":{"1":{"wt":"Hello, globe!"}}}" data-parsoid="{"tsr":[18,40],"src":"{{Test|Hello, world!}}","dsr":[18,40,null,null]}"></span>',
|
'blockSpanModified': '<span about="#mwt1" typeof="mw:Transclusion" data-mw="{"id":"mwt1","target":{"wt":"Test"},"params":{"1":{"wt":"Hello, globe!"}}}" data-parsoid="{"tsr":[18,40],"src":"{{Test|Hello, world!}}","dsr":[18,40,null,null]}"></span>',
|
||||||
|
@ -131,9 +131,11 @@ ve.dm.mwExample.domToDataCases = {
|
||||||
'width': 135,
|
'width': 135,
|
||||||
'height': 155,
|
'height': 155,
|
||||||
'isLinked': true,
|
'isLinked': true,
|
||||||
'valign': 'default',
|
'valign': 'text-top',
|
||||||
'resource': './File:Wiki.png',
|
'resource': './File:Wiki.png',
|
||||||
'type': 'inline'
|
'type': 'inline',
|
||||||
|
'originalClasses': 'foo mw-valign-text-top',
|
||||||
|
'unrecognizedClasses': ['foo']
|
||||||
},
|
},
|
||||||
'htmlAttributes': [
|
'htmlAttributes': [
|
||||||
{
|
{
|
||||||
|
@ -946,7 +948,7 @@ ve.dm.mwExample.domToDataCases = {
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
'thumb image': {
|
'thumb image': {
|
||||||
'html': '<body><figure typeof="mw:Image/Thumb" class="mw-halign-right"><a href="Foo"><img src="Bar" width="1" height="2" resource="FooBar"></a><figcaption>abc</figcaption></figure></body>',
|
'html': '<body><figure typeof="mw:Image/Thumb" class="mw-halign-right foobar"><a href="Foo"><img src="Bar" width="1" height="2" resource="FooBar"></a><figcaption>abc</figcaption></figure></body>',
|
||||||
'data': [
|
'data': [
|
||||||
{
|
{
|
||||||
'type': 'mwBlockImage',
|
'type': 'mwBlockImage',
|
||||||
|
@ -958,7 +960,8 @@ ve.dm.mwExample.domToDataCases = {
|
||||||
'width': '1',
|
'width': '1',
|
||||||
'height': '2',
|
'height': '2',
|
||||||
'resource': 'FooBar',
|
'resource': 'FooBar',
|
||||||
'originalClasses': 'mw-halign-right'
|
'originalClasses': 'mw-halign-right foobar',
|
||||||
|
'unrecognizedClasses': ['foobar']
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{ 'type': 'mwImageCaption' },
|
{ 'type': 'mwImageCaption' },
|
||||||
|
@ -984,7 +987,8 @@ ve.dm.mwExample.domToDataCases = {
|
||||||
'width': '1',
|
'width': '1',
|
||||||
'height': '2',
|
'height': '2',
|
||||||
'resource': 'FooBar',
|
'resource': 'FooBar',
|
||||||
'originalClasses': undefined
|
'originalClasses': undefined,
|
||||||
|
'unrecognizedClasses': []
|
||||||
},
|
},
|
||||||
'htmlAttributes': [ {
|
'htmlAttributes': [ {
|
||||||
'values': { 'data-parsoid': '{}' },
|
'values': { 'data-parsoid': '{}' },
|
||||||
|
|
Loading…
Reference in a new issue