jenkins-bot has submitted this change and it was merged.

Change subject: Allow bold and italic text
......................................................................


Allow bold and italic text

Added mmv.HtmlUtils.htmlToTextWithTags()
  which is similar to htmlToTextWithLinks()
  but allows <b> and <i>
Added test for mmv.HtmlUtils.htmlToTextWithTags()
Most text fields now use htmlToTextWithTags()
  except Permission field which is not supposed to
  have HTML

Bug: T69887
Change-Id: I16833f218e2f64586aa13925356fa2b8b7ec3100
---
M resources/mmv/mmv.HtmlUtils.js
M resources/mmv/mmv.bootstrap.js
M resources/mmv/ui/mmv.ui.description.js
M resources/mmv/ui/mmv.ui.truncatableTextField.js
M tests/qunit/mmv/mmv.HtmlUtils.test.js
M tests/qunit/mmv/ui/mmv.ui.metadataPanel.test.js
6 files changed, 33 insertions(+), 7 deletions(-)

Approvals:
  Gergő Tisza: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/resources/mmv/mmv.HtmlUtils.js b/resources/mmv/mmv.HtmlUtils.js
index efb4465..216150f 100644
--- a/resources/mmv/mmv.HtmlUtils.js
+++ b/resources/mmv/mmv.HtmlUtils.js
@@ -23,11 +23,12 @@
         * @private
         * @static
         * Shared cache between HtmlUtils instances to store the results of 
expensive text operations.
-        * @type {{text: Object.<string, string>, textWithLinks: 
Object.<string, string>}}
+        * @type {{text: Object.<string, string>, textWithLinks: 
Object.<string, string>, textWithTags: Object.<string, string>}}
         */
        cache = {
                text: {},
-               textWithLinks: {}
+               textWithLinks: {},
+               textWithTags: {}
        };
 
        /**
@@ -203,6 +204,24 @@
        };
 
        /**
+        * Returns the text content of a html string, with the `<a>`, `<i>`, 
`<b>` tags left intact.
+        * Tries to give an approximation of what would be visible if the HTML 
would be displayed.
+        * @param {string} html
+        * @return {string}
+        */
+       HUP.htmlToTextWithTags = function ( html ) {
+               var $html;
+               if ( !cache.textWithTags[html] ) {
+                       $html = this.wrapAndJquerify( html );
+                       this.filterInvisible( $html );
+                       this.appendWhitespaceToBlockElements( $html );
+                       this.whitelistHtml( $html, 'a, span, i, b' );
+                       cache.textWithTags[html] = this.mergeWhitespace( 
$html.html() );
+               }
+               return cache.textWithTags[html];
+       };
+
+       /**
         * Returns the text content of a html string, with the `<a>` tags left 
intact.
         * Tries to give an approximation of what would be visible if the HTML 
would be displayed.
         * @param {string} html
diff --git a/resources/mmv/mmv.bootstrap.js b/resources/mmv/mmv.bootstrap.js
index 64c45f5..f76613a 100644
--- a/resources/mmv/mmv.bootstrap.js
+++ b/resources/mmv/mmv.bootstrap.js
@@ -355,7 +355,7 @@
                                        .find( '.gallerytext' )
                                        .clone();
                        }
-                       return this.htmlUtils.htmlToTextWithLinks( 
$thumbCaption.html() || '' );
+                       return this.htmlUtils.htmlToTextWithTags( 
$thumbCaption.html() || '' );
                } else if ( $link.prop( 'title' ) ) {
                        return $link.prop( 'title' );
                }
diff --git a/resources/mmv/ui/mmv.ui.description.js 
b/resources/mmv/ui/mmv.ui.description.js
index a552478..7073963 100644
--- a/resources/mmv/ui/mmv.ui.description.js
+++ b/resources/mmv/ui/mmv.ui.description.js
@@ -48,7 +48,7 @@
         */
        Description.prototype.set = function ( description, caption ) {
                if ( caption && description ) { // panel header shows the 
caption - show description here
-                       this.$imageDesc.html( 
this.htmlUtils.htmlToTextWithLinks( description ) );
+                       this.$imageDesc.html( 
this.htmlUtils.htmlToTextWithTags( description ) );
                        this.$imageDescDiv.removeClass( 'empty' );
                } else { // either there is no description or the paner header 
already shows it - nothing to do here
                        this.empty();
diff --git a/resources/mmv/ui/mmv.ui.truncatableTextField.js 
b/resources/mmv/ui/mmv.ui.truncatableTextField.js
index 07076da..8da95c9 100644
--- a/resources/mmv/ui/mmv.ui.truncatableTextField.js
+++ b/resources/mmv/ui/mmv.ui.truncatableTextField.js
@@ -116,7 +116,7 @@
         * @param {string} value Warning - unsafe HTML is allowed here.
         */
        TTFP.set = function ( value ) {
-               this.$element.empty().append( 
this.htmlUtils.htmlToTextWithLinks( value ) );
+               this.$element.empty().append( 
this.htmlUtils.htmlToTextWithTags( value ) );
                this.changeStyle();
                this.$container.toggleClass( 'empty', !value );
                this.$ellipsis.hide();
diff --git a/tests/qunit/mmv/mmv.HtmlUtils.test.js 
b/tests/qunit/mmv/mmv.HtmlUtils.test.js
index 79d643d..bee5c01 100644
--- a/tests/qunit/mmv/mmv.HtmlUtils.test.js
+++ b/tests/qunit/mmv/mmv.HtmlUtils.test.js
@@ -161,11 +161,18 @@
 
        QUnit.test( 'htmlToTextWithLinks()', 1, function ( assert ) {
                var utils = new mw.mmv.HtmlUtils(),
-                       html = '<table><tr><td>Foo</td><td><a>bar</a></td><td 
style="display: none">baz</td></tr></table>';
+                       html = 
'<table><tr><td><b>F</b>o<i>o</i></td><td><a>bar</a></td><td style="display: 
none">baz</td></tr></table>';
 
                assert.strictEqual( utils.htmlToTextWithLinks( html ), 'Foo 
<a>bar</a>', 'works' );
        } );
 
+       QUnit.test( 'htmlToTextWithTags()', 1, function ( assert ) {
+               var utils = new mw.mmv.HtmlUtils(),
+                       html = 
'<table><tr><td><b>F</b>o<i>o</i></td><td><a>bar</a></td><td style="display: 
none">baz</td></tr></table>';
+
+               assert.strictEqual( utils.htmlToTextWithTags( html ), 
'<b>F</b>o<i>o</i> <a>bar</a>', 'works' );
+       } );
+
        QUnit.test( 'isJQueryOrHTMLElement()', 3, function ( assert ) {
                var utils = new mw.mmv.HtmlUtils();
 
diff --git a/tests/qunit/mmv/ui/mmv.ui.metadataPanel.test.js 
b/tests/qunit/mmv/ui/mmv.ui.metadataPanel.test.js
index 11ba434..b209914 100644
--- a/tests/qunit/mmv/ui/mmv.ui.metadataPanel.test.js
+++ b/tests/qunit/mmv/ui/mmv.ui.metadataPanel.test.js
@@ -150,7 +150,7 @@
                assert.ok( !panel.$credit.hasClass( 'empty' ), 'Credit is not 
empty' );
                assert.ok( !panel.$datetimeLi.hasClass( 'empty' ), 'Date/Time 
is not empty' );
                assert.strictEqual( panel.creditField.$element.find( 
'.mw-mmv-author' ).text(), imageData.author, 'Author text is correctly set' );
-               assert.strictEqual( panel.creditField.$element.find( 
'.mw-mmv-source' ).html(), 'Lost<a href="foo">Bar</a>', 'Source text is 
correctly set' );
+               assert.strictEqual( panel.creditField.$element.find( 
'.mw-mmv-source' ).html(), '<b>Lost</b><a href="foo">Bar</a>', 'Source text is 
correctly set' );
                assert.strictEqual( panel.creditField.$element.attr( 
'original-title' ), 'Author and source information', 'Source tooltip is 
correctly set' );
                assert.ok( panel.$datetime.text().indexOf( 'August 26, 2013' ) 
> 0, 'Correct date is displayed' );
                assert.strictEqual( panel.$license.text(), 'CC BY 2.0', 
'License is correctly set' );

-- 
To view, visit https://gerrit.wikimedia.org/r/182292
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I16833f218e2f64586aa13925356fa2b8b7ec3100
Gerrit-PatchSet: 8
Gerrit-Project: mediawiki/extensions/MultimediaViewer
Gerrit-Branch: master
Gerrit-Owner: Sn1per <[email protected]>
Gerrit-Reviewer: GergÅ‘ Tisza <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to