jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/342204 )
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(-) Approvals: Robert Vogel: Looks good to me, approved jenkins-bot: Verified diff --git a/VisualEditor/resources/tiny_mce_plugins/bswikicode/plugin.js b/VisualEditor/resources/tiny_mce_plugins/bswikicode/plugin.js index e657193..f630e4c 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@@"); @@ -2306,6 +2306,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 @@ -2352,6 +2383,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/342204 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I6a63524576ebc0869bb77c9621106d563625f390 Gerrit-PatchSet: 2 Gerrit-Project: mediawiki/extensions/BlueSpiceExtensions Gerrit-Branch: master Gerrit-Owner: Mglaser <gla...@hallowelt.biz> Gerrit-Reviewer: Ljonka <l.verhovs...@gmail.com> Gerrit-Reviewer: Pwirth <wi...@hallowelt.biz> Gerrit-Reviewer: Robert Vogel <vo...@hallowelt.biz> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits