[MediaWiki-commits] [Gerrit] VisualEditor/VisualEditor[master]: [BREAKING CHANGE] Parse selection before applying source tools
jenkins-bot has submitted this change and it was merged. Change subject: [BREAKING CHANGE] Parse selection before applying source tools .. [BREAKING CHANGE] Parse selection before applying source tools If I select 'foo' and apply a link or trigger any other tool, it should parse that selection into DM, instead of treating is a plain text, otherwise it will get escaped. Change-Id: Iaa41c49e2f8e28af28a8a64cb6915f705fa35e8c --- M src/dm/ve.dm.SourceSurfaceFragment.js M src/ui/actions/ve.ui.WindowAction.js 2 files changed, 112 insertions(+), 91 deletions(-) Approvals: Jforrester: Looks good to me, approved jenkins-bot: Verified diff --git a/src/dm/ve.dm.SourceSurfaceFragment.js b/src/dm/ve.dm.SourceSurfaceFragment.js index 72f39da..daebdb4 100644 --- a/src/dm/ve.dm.SourceSurfaceFragment.js +++ b/src/dm/ve.dm.SourceSurfaceFragment.js @@ -26,30 +26,21 @@ * @inheritdoc */ ve.dm.SourceSurfaceFragment.prototype.annotateContent = function () { - var fragment, tempDocument, rangeInDocument, tempSurfaceModel, - originalDocument = this.getDocument(), - coveringRange = this.getSelection().getCoveringRange(); + var tempFragment, tempSurfaceModel, + args = arguments, + fragment = this, + text = this.getText( true ); - if ( !coveringRange.isCollapsed() ) { - tempDocument = originalDocument.shallowCloneFromRange( coveringRange ); - rangeInDocument = tempDocument.originalRange; - } else { - tempDocument = new ve.dm.Document( - [ - { type: 'paragraph', internal: { generated: 'wrapper' } }, { type: '/paragraph' }, - { type: 'internalList' }, { type: '/internalList' } - ], - null, null, null, null, - originalDocument.getLang(), - originalDocument.getDir() + this.convertFromSource( text ).then( function ( selectionDocument ) { + tempSurfaceModel = new ve.dm.Surface( selectionDocument ); + tempFragment = tempSurfaceModel.getLinearFragment( + // TODO: Find content offsets + new ve.Range( 0, selectionDocument.getInternalList().getListNode().getOuterRange().start ) ); - rangeInDocument = new ve.Range( 1 ); - } - tempSurfaceModel = new ve.dm.Surface( tempDocument ); - fragment = tempSurfaceModel.getLinearFragment( rangeInDocument ); - fragment.annotateContent.apply( fragment, arguments ); + tempFragment.annotateContent.apply( tempFragment, args ); - this.insertDocument( fragment.getDocument() ); + fragment.insertDocument( tempFragment.getDocument() ); + } ); return this; }; @@ -89,7 +80,7 @@ return ve.dm.SourceSurfaceFragment.super.prototype.insertContent.call( this, doc.data.getDataSlice( newDocRange ) ); } - /* conversionPromise = */ this.convertDocument( doc ) + this.convertToSource( doc ) .done( function ( source ) { fragment.removeContent(); @@ -156,7 +147,7 @@ }; /** - * Convert sub document to source text + * Convert model slice to source text * * The default implementation converts to HTML synchronously. * @@ -166,7 +157,7 @@ * @param {ve.dm.Document} doc Document * @return {jQuery.Promise} Promise with resolves with source, or rejects */ -ve.dm.SourceSurfaceFragment.prototype.convertDocument = function ( doc ) { +ve.dm.SourceSurfaceFragment.prototype.convertToSource = function ( doc ) { if ( !doc.data.hasContent() ) { return $.Deferred().reject().promise(); } else { @@ -177,3 +168,38 @@ ).promise(); } }; + +/** + * Convert source text to model slice + * + * The default implementation converts from HTML synchronously. + * + * If the conversion is asynchornous it should lock the surface + * until complete. + * + * @param {string} source Source text + * @return {jQuery.Promise} Promise with resolves with source + */ +ve.dm.SourceSurfaceFragment.prototype.convertFromSource = function ( source ) { + var lang = this.getDocument().getLang(), + dir = this.getDocument().getDir(); + + if ( !source ) { + return $.Deferred().resolve( + new ve.dm.Document( + [ + { type: 'paragraph', internal: { generated: 'wrapper' } }, { type: '/paragraph' }, + { type: 'internalList' }, { type: '/internalList' } + ], + null, null, null, null, + lang, dir +
[MediaWiki-commits] [Gerrit] VisualEditor/VisualEditor[master]: [BREAKING CHANGE] Parse selection before applying source tools
Esanders has uploaded a new change for review. https://gerrit.wikimedia.org/r/324777 Change subject: [BREAKING CHANGE] Parse selection before applying source tools .. [BREAKING CHANGE] Parse selection before applying source tools If I select 'foo' and apply a link or trigger any other tool, it should parse that selection into DM, instead of treating is a plain text, otherwise it will get escaped. Change-Id: Iaa41c49e2f8e28af28a8a64cb6915f705fa35e8c --- M src/dm/ve.dm.SourceSurfaceFragment.js M src/ui/actions/ve.ui.WindowAction.js 2 files changed, 112 insertions(+), 141 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/VisualEditor/VisualEditor refs/changes/77/324777/1 diff --git a/src/dm/ve.dm.SourceSurfaceFragment.js b/src/dm/ve.dm.SourceSurfaceFragment.js index 72f39da..a4b4b77 100644 --- a/src/dm/ve.dm.SourceSurfaceFragment.js +++ b/src/dm/ve.dm.SourceSurfaceFragment.js @@ -26,30 +26,21 @@ * @inheritdoc */ ve.dm.SourceSurfaceFragment.prototype.annotateContent = function () { - var fragment, tempDocument, rangeInDocument, tempSurfaceModel, - originalDocument = this.getDocument(), - coveringRange = this.getSelection().getCoveringRange(); + var tempFragment, tempSurfaceModel, + args = arguments, + fragment = this, + text = this.getText( true ); - if ( !coveringRange.isCollapsed() ) { - tempDocument = originalDocument.shallowCloneFromRange( coveringRange ); - rangeInDocument = tempDocument.originalRange; - } else { - tempDocument = new ve.dm.Document( - [ - { type: 'paragraph', internal: { generated: 'wrapper' } }, { type: '/paragraph' }, - { type: 'internalList' }, { type: '/internalList' } - ], - null, null, null, null, - originalDocument.getLang(), - originalDocument.getDir() + this.convertFromSource( text ).then( function ( selectionDocument ) { + tempSurfaceModel = new ve.dm.Surface( selectionDocument ); + tempFragment = tempSurfaceModel.getLinearFragment( + // TODO: Find content offsets + new ve.Range( 0, selectionDocument.getInternalList().getListNode().getOuterRange().start ) ); - rangeInDocument = new ve.Range( 1 ); - } - tempSurfaceModel = new ve.dm.Surface( tempDocument ); - fragment = tempSurfaceModel.getLinearFragment( rangeInDocument ); - fragment.annotateContent.apply( fragment, arguments ); + tempFragment.annotateContent.apply( tempFragment, args ); - this.insertDocument( fragment.getDocument() ); + fragment.insertDocument( tempFragment.getDocument() ); + } ); return this; }; @@ -89,7 +80,7 @@ return ve.dm.SourceSurfaceFragment.super.prototype.insertContent.call( this, doc.data.getDataSlice( newDocRange ) ); } - /* conversionPromise = */ this.convertDocument( doc ) + this.convertToSource( doc ) .done( function ( source ) { fragment.removeContent(); @@ -106,57 +97,7 @@ }; /** - * @inheritdoc - */ -ve.dm.SourceSurfaceFragment.prototype.wrapAllNodes = function ( wrapOuter, wrapEach ) { - var i, node, nodes, - content, - range = this.getSelection().getCoveringRange(); - - if ( !range ) { - return this; - } - - function getOpening( element ) { - element.internal = { - whitespace: [ '\n', '\n', '\n', '\n' ] - }; - return element; - } - - function getClosing( element ) { - return { type: '/' + element.type }; - } - - if ( !Array.isArray( wrapOuter ) ) { - wrapOuter = [ wrapOuter ]; - } - - wrapEach = wrapEach || []; - - if ( !Array.isArray( wrapEach ) ) { - wrapEach = [ wrapEach ]; - } - - nodes = this.getSelectedLeafNodes(); - - content = wrapOuter.map( getOpening ); - for ( i = 0; i < nodes.length; i++ ) { - node = nodes[ i ]; - content = content - .concat( wrapEach.map( getOpening ) ) - .concat( this.getSurface().getLinearFragment( node.getRange() ).getText().split( '' ) ) - .concat( wrapEach.reverse().map( getClosing ) ); - } - content = content.concat( wrapOuter.reverse().map( getClosing ) ); - - this.insertContent( content ); - - return this; -}; - -/** - * Convert sub document to source text + * Convert model slice to source text * * The default implementation converts to HT