Robert Vogel has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/344609 )
Change subject: VisualEditor: introducing DOM preprocessor ...................................................................... VisualEditor: introducing DOM preprocessor A DOM preprocessor is introduced which currently only replaces certain spans (underline and strike). This is neccessary because it is the correct way to handle nested spans, where regexs would fail. In the wake of this change, handling of external links was improved, as the html produced was a mess. Patch Set 2: Implemented CR Change-Id: I6a63524576ebc0869bb77c9621106d563625f390 --- M VisualEditor/resources/tiny_mce_plugins/bswikicode/plugin.js 1 file changed, 44 insertions(+), 11 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/BlueSpiceExtensions refs/changes/09/344609/1 diff --git a/VisualEditor/resources/tiny_mce_plugins/bswikicode/plugin.js b/VisualEditor/resources/tiny_mce_plugins/bswikicode/plugin.js index e655a45..e987169 100644 --- a/VisualEditor/resources/tiny_mce_plugins/bswikicode/plugin.js +++ b/VisualEditor/resources/tiny_mce_plugins/bswikicode/plugin.js @@ -337,7 +337,7 @@ } function _image2wiki(text) { - var images = text.match(/(<a([^>]*?)>)?<img([^>]*?)\/>(<\/a>)?/gi); + var images = text.match(/(<a([^>]*?)>)?<img([^>]*?)\/?>(<\/a>)?/gi); if (!images) return text; @@ -614,13 +614,15 @@ _externalLinkNo++; } linkHtml = anchorFormat.format( - encodeURI( linkTarget.replace( /%20/g, ' ' ) ), // href - linkLabel, // <a>linkLabel</a> - 'external_link', // data-bs-type - 'external bs-external-link bs-protocol-'+protocol, // class - $( '<div/>' ).text( link ).html(), // data-bs-wikitext - encodeURI( linkTarget.replace( /%20/g, ' ' ) ), // data-mce-href - $( '<div/>' ).text( linkLabel ).html() // title + encodeURI( linkTarget.replace( /%20/g, ' ' ) ), // href + linkLabel, // <a>linkLabel</a> + 'external_link', // data-bs-type + 'external bs-external-link bs-protocol-' + protocol, // class + // encoding neccessary, because this is possible: [http://www.hallowelt.biz <span style="color: #99cc00;">Hallo Welt!</span>] + encodeURI( $( '<div/>' ).text( link ).html() ), // data-bs-wikitext + encodeURI( linkTarget.replace( /%20/g, ' ' ) ), // data-mce-href + // encoding neccessary, because this is possible: [http://www.hallowelt.biz <span style="color: #99cc00;">Hallo Welt!</span>] + encodeURI( $( '<div/>' ).text( linkLabel ).html() ) // title ); text = text.replace("[" + linkNoWrap + "]", linkHtml); } @@ -1425,7 +1427,7 @@ $(document).trigger('BSVisualEditorBeforeHtmlToWiki', [textObject]); // get the text back text = textObject.text; - // Normalize UTF8 spaces as aof TinyMCE 3.4.9 + // Normalize UTF8 spaces as of TinyMCE 3.4.9 text = text.replace(/\u00a0/gi, ''); //Save content of pre tags text = _preservePres(text); @@ -1438,8 +1440,6 @@ text = text.replace(/<i>(.*?)<\/i>/gmi, "''$1''"); //underline needs no conversion text = text.replace(/<strike>(.*?)<\/strike>/gi, "<s>$1</s>"); - text = text.replace(/<span style="text-decoration: line-through;">(.*?)<\/span>/gi, "<s>$1</s>"); - text = text.replace(/<span style="text-decoration: underline;">(.*?)<\/span>/gi, "<u>$1</u>"); //sub and sup need no conversion text = text.replace(/<br class="bs_emptyline_first"[^>]*>/gmi, "@@br_emptyline_first@@"); @@ -2289,6 +2289,37 @@ } /** + * Preprocess HTML in DOM form. This is mainly used to replace tags + * @param {String} text + * @returns {String} + */ + function _preprocessHtml2Wiki( text ) { + // convert html text to DOM + var $dom = $( "<div id='tinywrapper'>" + text + "</div>" ); + + // perform the actual preprocessing + $dom.find( "span[style*='text-decoration: underline']" ).replaceWith( function() { + return "<u>" + $( this ).html() + "</u>"; + } ); + $dom.find( "span[style*='text-decoration: line-through']" ).replaceWith( function() { + return "<s>" + $( this ).html() + "</s>"; + } ); + + // convert DOM to html text + text = $dom.html(); + + //cleanup entities in attribtues + while ( text.match( /(\="[^"]*?)(<)([^"]*?")/gmi ) ) { + text = text.replace( /(\="[^"]*?)(<)([^"]*?")/g, '$1<$3' ); + } + while ( text.match( /(\="[^"]*?)(>)([^"]*?")/gmi ) ) { + text = text.replace( /(\="[^"]*?)(>)([^"]*?")/g, '$1>$3' ); + } + + return text; + } + + /** * Event handler for "beforeSetContent" * This is used to process the wiki code into html. * @param {tinymce.ContentEvent} e @@ -2335,6 +2366,8 @@ if (e.format != 'raw') e.format = 'wiki'; + e.content = _preprocessHtml2Wiki( e.content ); + // process the html to wikicode e.content = _html2wiki(e.content); -- To view, visit https://gerrit.wikimedia.org/r/344609 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I6a63524576ebc0869bb77c9621106d563625f390 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/BlueSpiceExtensions Gerrit-Branch: REL1_27 Gerrit-Owner: Robert Vogel <vo...@hallowelt.biz> Gerrit-Reviewer: Mglaser <gla...@hallowelt.biz> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits