From dd73b873a84c5fe095ad9d84123571851df66518 Mon Sep 17 00:00:00 2001 From: Ed Sanders Date: Sat, 7 Sep 2013 03:11:07 -0700 Subject: [PATCH] Add alt attribute to core image nodes Also update and refactor tests. Change-Id: I96d08faed42118b713af8011c5d6befb760e65c9 --- modules/ve/ce/nodes/ve.ce.ImageNode.js | 1 + modules/ve/dm/nodes/ve.dm.ImageNode.js | 4 +- modules/ve/test/dm/ve.dm.Transaction.test.js | 10 +-- modules/ve/test/dm/ve.dm.example.js | 65 ++++++++------------ 4 files changed, 31 insertions(+), 49 deletions(-) diff --git a/modules/ve/ce/nodes/ve.ce.ImageNode.js b/modules/ve/ce/nodes/ve.ce.ImageNode.js index 790408ba23..c897ee6e39 100644 --- a/modules/ve/ce/nodes/ve.ce.ImageNode.js +++ b/modules/ve/ce/nodes/ve.ce.ImageNode.js @@ -37,6 +37,7 @@ ve.ce.ImageNode = function VeCeImageNode( model, config ) { // Initialization this.$image .addClass( 've-ce-imageNode' ) + .attr( 'alt', this.model.getAttribute( 'alt' ) ) .attr( 'src', this.model.getAttribute( 'src' ) ) .css( { 'width': this.model.getAttribute( 'width' ), diff --git a/modules/ve/dm/nodes/ve.dm.ImageNode.js b/modules/ve/dm/nodes/ve.dm.ImageNode.js index fd6bbe3385..2e567771dd 100644 --- a/modules/ve/dm/nodes/ve.dm.ImageNode.js +++ b/modules/ve/dm/nodes/ve.dm.ImageNode.js @@ -33,6 +33,7 @@ ve.dm.ImageNode.static.matchTagNames = [ 'img' ]; ve.dm.ImageNode.static.toDataElement = function ( domElements ) { var $node = $( domElements[0] ), + alt = $node.attr( 'alt' ), width = $node.attr( 'width' ), height = $node.attr( 'height' ); @@ -40,6 +41,7 @@ ve.dm.ImageNode.static.toDataElement = function ( domElements ) { 'type': 'image', 'attributes': { 'src': $node.attr( 'src' ), + 'alt': alt !== undefined ? alt : null, 'width': width !== undefined && width !== '' ? Number( width ) : null, 'height': height !== undefined && height !== '' ? Number( height ) : null } @@ -48,7 +50,7 @@ ve.dm.ImageNode.static.toDataElement = function ( domElements ) { ve.dm.ImageNode.static.toDomElements = function ( dataElement, doc ) { var domElement = doc.createElement( 'img' ); - ve.setDomAttributes( domElement, dataElement.attributes, [ 'src', 'width', 'height' ] ); + ve.setDomAttributes( domElement, dataElement.attributes, [ 'alt', 'src', 'width', 'height' ] ); return [ domElement ]; }; diff --git a/modules/ve/test/dm/ve.dm.Transaction.test.js b/modules/ve/test/dm/ve.dm.Transaction.test.js index 776b521bc0..67fce49f67 100644 --- a/modules/ve/test/dm/ve.dm.Transaction.test.js +++ b/modules/ve/test/dm/ve.dm.Transaction.test.js @@ -386,15 +386,7 @@ QUnit.test( 'newFromRemoval', function ( assert ) { 'type': 'replace', 'remove': [ 'h', - { - 'type': 'image', - 'attributes': { - 'src': ve.dm.example.imgSrc, - 'width': null, - 'height': null - }, - 'htmlAttributes': [ { 'values': { 'src': ve.dm.example.imgSrc } } ] - }, + ve.dm.example.image.data, { 'type': '/image' }, 'i' ], diff --git a/modules/ve/test/dm/ve.dm.example.js b/modules/ve/test/dm/ve.dm.example.js index b21fe09b88..785f6da35e 100644 --- a/modules/ve/test/dm/ve.dm.example.js +++ b/modules/ve/test/dm/ve.dm.example.js @@ -165,6 +165,25 @@ ve.dm.example.testDir = window.VE_TESTDIR || '.'; ve.dm.example.imgSrc = ve.dm.example.testDir + '/example.png'; +ve.dm.example.image = { + html: 'Example', + data: { + 'type': 'image', + 'attributes' : { + 'src': ve.dm.example.imgSrc, + 'alt': 'Example', + 'width': 100, + 'height': 50 + }, + 'htmlAttributes': [ { 'values': { + 'src': ve.dm.example.imgSrc, + 'alt': 'Example', + 'width': '100', + 'height': '50' + } } ] + } +}; + /** * Serialized HTML. * @@ -195,7 +214,7 @@ ve.dm.example.html = '' + '' + '' + - '
hi
'+ + '
h' + ve.dm.example.image.html + 'i
'+ '
' + '
' + '

j

' + @@ -305,15 +324,7 @@ ve.dm.example.data = [ // 38 - Plain "h" 'h', // 39 - Beginning of inline image - { - 'type': 'image', - 'attributes': { - 'src': ve.dm.example.imgSrc, - 'width': null, - 'height': null - }, - 'htmlAttributes': [ { 'values': { 'src': ve.dm.example.imgSrc } } ] - }, + ve.dm.example.image.data, // 40 - End of inline image { 'type': '/image' }, // 41 - Plain "i" @@ -650,15 +661,7 @@ ve.dm.example.complexTable = [ ve.dm.example.inlineAtEdges = [ { 'type': 'paragraph' }, - { - 'type': 'image', - 'attributes': { - 'src': ve.dm.example.imgSrc, - 'width': null, - 'height': null - }, - 'htmlAttributes': [ { 'values': { 'src': ve.dm.example.imgSrc } } ] - }, + ve.dm.example.image.data, { 'type': '/image' }, 'F', 'o', @@ -933,18 +936,10 @@ ve.dm.example.domToDataCases = { 'normalizedHtml': '

foo

bar

baz

 \tquux
' }, 'image': { - 'html': '', + 'html': '' + ve.dm.example.image.html + '', 'data': [ { 'type': 'paragraph', 'internal': { 'generated': 'wrapper' } }, - { - 'type': 'image', - 'attributes' : { - 'width': null, - 'height': null, - 'src': ve.dm.example.imgSrc - }, - 'htmlAttributes': [ { 'values': { 'src': ve.dm.example.imgSrc } } ] - }, + ve.dm.example.image.data, { 'type' : '/image' }, { 'type': '/paragraph' }, { 'type': 'internalList' }, @@ -1160,18 +1155,10 @@ ve.dm.example.domToDataCases = { ] }, 'wrapping of bare content starting with inline node': { - 'html': '12', + 'html': '' + ve.dm.example.image.html + '12', 'data': [ { 'type': 'paragraph', 'internal': { 'generated': 'wrapper' } }, - { - 'type': 'image', - 'attributes': { - 'src': ve.dm.example.imgSrc, - 'width': null, - 'height': null - }, - 'htmlAttributes': [ { 'values': { 'src': ve.dm.example.imgSrc } } ] - }, + ve.dm.example.image.data, { 'type': '/image' }, '1', '2',