Catrope has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/70241


Change subject: Preserve the class attribute for block images
......................................................................

Preserve the class attribute for block images

Build the new set of classes as an array (avoiding the leading space),
and only set it if it differs from what was there before. This avoids
reordering the class attribute.

Change-Id: I863ec8de2e7637f76690c4e0b7db5e153fb7e408
---
M modules/ve/dm/nodes/ve.dm.MWBlockImageNode.js
M modules/ve/test/dm/ve.dm.example.js
2 files changed, 21 insertions(+), 11 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/VisualEditor 
refs/changes/41/70241/1

diff --git a/modules/ve/dm/nodes/ve.dm.MWBlockImageNode.js 
b/modules/ve/dm/nodes/ve.dm.MWBlockImageNode.js
index 3e43fa0..936c2e2 100644
--- a/modules/ve/dm/nodes/ve.dm.MWBlockImageNode.js
+++ b/modules/ve/dm/nodes/ve.dm.MWBlockImageNode.js
@@ -54,7 +54,8 @@
                        src: $img.attr( 'src' ),
                        width: $img.attr( 'width' ),
                        height: $img.attr( 'height' ),
-                       resource: $img.attr( 'resource' )
+                       resource: $img.attr( 'resource' ),
+                       originalClasses: classes
                };
 
        // Extract individual classes
@@ -114,7 +115,9 @@
                figure = doc.createElement( 'figure' ),
                a = doc.createElement( 'a' ),
                img = doc.createElement( 'img' ),
-               wrapper = doc.createElement( 'div' );
+               wrapper = doc.createElement( 'div' ),
+               classes = [],
+               originalClasses = dataElement.attributes.originalClasses;
 
        // Type
        switch ( dataElement.attributes.type ) {
@@ -131,25 +134,30 @@
 
        // Default-size
        if ( dataElement.attributes.defaultSize === true ) {
-               figure.className += ' mw-default-size';
+               classes.push( 'mw-default-size' );
        }
 
        // Horizontal alignment
        switch ( dataElement.attributes.align ) {
                case 'left':
-                       figure.className += ' mw-halign-left';
+                       classes.push( 'mw-halign-left' );
                        break;
                case 'right':
-                       figure.className += ' mw-halign-right';
+                       classes.push( 'mw-halign-right' );
                        break;
                case 'center':
-                       figure.className += ' mw-halign-center';
+                       classes.push( 'mw-halign-center' );
                        break;
                case 'none':
-                       figure.className += ' mw-halign-none';
+                       classes.push( 'mw-halign-none' );
                        break;
        }
 
+       if ( originalClasses && ve.compare( originalClasses.split( ' ' 
).sort(), classes.sort() ) ) {
+               figure.className = originalClasses;
+       } else if ( classes.length > 0 ) {
+               figure.className = classes.join( ' ' );
+       }
        a.setAttribute( 'rel', 'mw:thumb' );
        a.setAttribute( 'href', dataElement.attributes.href );
        img.setAttribute( 'src', dataElement.attributes.src );
diff --git a/modules/ve/test/dm/ve.dm.example.js 
b/modules/ve/test/dm/ve.dm.example.js
index bcb1336..f351465 100644
--- a/modules/ve/test/dm/ve.dm.example.js
+++ b/modules/ve/test/dm/ve.dm.example.js
@@ -3388,18 +3388,19 @@
                ]
        },
        'thumb image': {
-               'html': '<body><figure typeof="mw:Image/Thumb"><a 
rel="mw:thumb" href="Foo"><img src="Bar" width="1" height="2" 
resource="FooBar"></a><figcaption 
class="mw-figcaption">abc</figcaption></figure></body>',
+               'html': '<body><figure typeof="mw:Image/Thumb" 
class="mw-halign-right"><a rel="mw:thumb" href="Foo"><img src="Bar" width="1" 
height="2" resource="FooBar"></a><figcaption 
class="mw-figcaption">abc</figcaption></figure></body>',
                'data': [
                        {
                                'type': 'mwBlockImage',
                                'attributes': {
                                        'type': 'thumb',
-                                       'align': 'default',
+                                       'align': 'right',
                                        'href': 'Foo',
                                        'src': 'Bar',
                                        'width': '1',
                                        'height': '2',
-                                       'resource': 'FooBar'
+                                       'resource': 'FooBar',
+                                       'originalClasses': 'mw-halign-right'
                                }
                        },
                        { 'type': 'mwImageCaption' },
@@ -3424,7 +3425,8 @@
                                        'src': 'Bar',
                                        'width': '1',
                                        'height': '2',
-                                       'resource': 'FooBar'
+                                       'resource': 'FooBar',
+                                       'originalClasses': undefined
                                },
                                'htmlAttributes': [ {
                                        'values': { 'data-parsoid': '{}' },

-- 
To view, visit https://gerrit.wikimedia.org/r/70241
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I863ec8de2e7637f76690c4e0b7db5e153fb7e408
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Catrope <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to