From a9233045c7fba7a8c8b0116a14ddf4f6ee2d310a Mon Sep 17 00:00:00 2001 From: Ed Sanders Date: Mon, 3 Oct 2016 15:55:16 -0400 Subject: [PATCH] Detect format in wikitext Logically depends on I575d92c4fa. Should really use a registry, and also doesn't update when format is changed by deleting text, but is an improvement for now. Bug: T147218 Change-Id: I0cb87dbad2ab4269c4f89bdff8a4ccd9b35c8635 --- .../dm/ve.dm.MWWikitextSurfaceFragment.js | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/modules/ve-mw/dm/ve.dm.MWWikitextSurfaceFragment.js b/modules/ve-mw/dm/ve.dm.MWWikitextSurfaceFragment.js index 9d968ee681..316facdb7b 100644 --- a/modules/ve-mw/dm/ve.dm.MWWikitextSurfaceFragment.js +++ b/modules/ve-mw/dm/ve.dm.MWWikitextSurfaceFragment.js @@ -22,6 +22,49 @@ ve.dm.MWWikitextSurfaceFragment = function VeDmMwWikitextSurfaceFragment() { OO.inheritClass( ve.dm.MWWikitextSurfaceFragment, ve.dm.SourceSurfaceFragment ); +/* Methods */ + +/** + * @inheritdoc + */ +ve.dm.MWWikitextSurfaceFragment.prototype.hasMatchingAncestor = function ( type, attributes ) { + var i, len, text, command, + nodes = this.getSelectedLeafNodes(), + all = !!nodes.length; + + nodes = this.getSelectedLeafNodes(); + all = !!nodes.length; + for ( i = 0, len = nodes.length; i < len; i++ ) { + text = this.document.data.getText( false, nodes[ i ].getRange() ); + // TODO: Use a registry to do this matching + switch ( type ) { + case 'paragraph': + all = !text.match( /^ |^=|^
/ ); + break; + case 'mwPreformatted': + all = text.slice( 0, 1 ) === ' '; + break; + case 'blockquote': + all = text.slice( 0, 12 ) === '
'; + break; + case 'mwHeading': + command = ve.ui.wikitextCommandRegistry.lookup( 'heading' + attributes.level ); + if ( text.indexOf( command.args[ 0 ] ) !== 0 || text.indexOf( command.args[ 1 ] ) !== text.length - command.args[ 1 ].length ) { + all = false; + } + break; + default: + all = false; + break; + } + if ( !all ) { + break; + } + } + + return all; +}; + /** * Wrap a text selection. *