jenkins-bot has submitted this change and it was merged.
Change subject: Remove the need of .html in article.getProcessedHtml
......................................................................
Remove the need of .html in article.getProcessedHtml
Instead of replacing all instances of the title in the extract -
'$1<b>$2</b>$3'
We now put symbolic strings there which we use to split the string
and then make an array of text and <b> elements that get appended
to $contentbox.
Bug: T76378
Change-Id: I02222bbff84532f63cac67af1bf889c328ec6ff2
---
M resources/ext.popups.renderer.article.js
M tests/qunit/ext.popups.renderer.article.test.js
2 files changed, 61 insertions(+), 24 deletions(-)
Approvals:
Werdna: Looks good to me, approved
jenkins-bot: Verified
diff --git a/resources/ext.popups.renderer.article.js
b/resources/ext.popups.renderer.article.js
index e31dbb2..895abea 100644
--- a/resources/ext.popups.renderer.article.js
+++ b/resources/ext.popups.renderer.article.js
@@ -100,8 +100,8 @@
$contentbox = $( '<a>' )
.attr( 'href', href )
.addClass( 'mwe-popups-extract' )
- .html(
- article.getProcessedHtml( page.extract,
page.title )
+ .append(
+ article.getProcessedElements(
page.extract, page.title )
),
thumbnail = page.thumbnail,
tall = thumbnail && thumbnail.height > thumbnail.width,
@@ -141,25 +141,43 @@
};
/**
- * Returns HTML extract after removing parentheses and making the title
in
- * the extract bold.
+ * Returns an array of elements to be appended after removing
parentheses
+ * and making the title in the extract bold.
*
- * @method getProcessedHtml
+ * @method getProcessedElements
* @param {String} extract
* @param {String} title
- * @return {String}
+ * @return {Array} of elements to appended
*/
- article.getProcessedHtml = function ( extract, title ) {
+ article.getProcessedElements = function ( extract, title ) {
extract = mw.html.escape( extract );
title = mw.html.escape( title );
title = title.replace( /([.?*+^$[\]\\(){}|-])/g, '\\$1' ); //
Escape RegExp elements
- var regExp = new RegExp( '(^|\\s)(' + title + ')(\\s|$)', 'ig'
);
- // Make title bold in the extract text
- extract = extract.replace( regExp, '$1<b>$2</b>$3' );
+
+ var elements = [],
+ regExp = new RegExp( '(^|\\s)(' + title + ')(\\s|$)',
'ig' ),
+ boldIdentifier = '<bi-' + Math.random() + '>',
+ snip = '<snip-' + Math.random() + '>';
+
// Remove text in parentheses along with the parentheses
extract = article.removeParensFromText( extract );
extract = extract.replace(/\s+/g, ' '); // Remove extra white
spaces
- return extract;
+
+ // Make title bold in the extract text
+ // As the extract is html escaped there can be no such string
in it
+ // Also, the title is escaped of RegExp elements thus can't
have "*"
+ extract = extract.replace( regExp, '$1' + snip + boldIdentifier
+ '$2' + snip + '$3' );
+ extract = extract.split( snip );
+
+ $.each( extract, function ( index, part ) {
+ if ( part.indexOf( boldIdentifier ) === 0 ) {
+ elements.push( $( '<b>' ).text( part.substring(
boldIdentifier.length ) ) );
+ } else {
+ elements.push( part );
+ }
+ } );
+
+ return elements;
};
/**
diff --git a/tests/qunit/ext.popups.renderer.article.test.js
b/tests/qunit/ext.popups.renderer.article.test.js
index 0aef5b9..6bca34e 100644
--- a/tests/qunit/ext.popups.renderer.article.test.js
+++ b/tests/qunit/ext.popups.renderer.article.test.js
@@ -1,49 +1,68 @@
( function ( $, mw ) {
QUnit.module( 'ext.popups' );
- QUnit.test( 'render.article.getProcessedHtml', function ( assert ) {
- QUnit.expect( 7 );
+ QUnit.test( 'render.article.getProcessedElements', function ( assert ) {
+ QUnit.expect( 9 );
- function test ( extract, title, expected ) {
- assert.equal(
- mw.popups.render.article.getProcessedHtml(
extract, title ),
- expected
+ function test ( extract, title, expected, msg ) {
+ var $div = $( '<div>' ).append(
+ mw.popups.render.article.getProcessedElements(
extract, title )
);
+ assert.equal( $div.html(), expected, msg );
}
test(
'Isaac Newton was born in', 'Isaac Newton',
- '<b>Isaac Newton</b> was born in'
+ '<b>Isaac Newton</b> was born in',
+ 'Title as first word'
);
test(
'The C* language not to be confused with C# or C', 'C*',
- 'The <b>C*</b> language not to be confused with C# or C'
+ 'The <b>C*</b> language not to be confused with C# or
C',
+ 'Title containing *'
);
test(
'Person (was born in Location) is good', 'Person',
- '<b>Person</b> is good'
+ '<b>Person</b> is good',
+ 'Extract with text in parentheses'
);
test(
'Person (was born in Location (at Time)) is good',
'Person',
- '<b>Person</b> is good'
+ '<b>Person</b> is good',
+ 'Extract with nested parentheses'
);
test(
'Person (was born in Location (at Time) ) is good',
'Person',
- '<b>Person</b> is good'
+ '<b>Person</b> is good',
+ 'Extract with nested parentheses and random spaces'
);
test(
'Brackets ) are funny ( when not used properly',
'Brackets',
- '<b>Brackets</b> ) are funny ( when not used properly'
+ '<b>Brackets</b> ) are funny ( when not used properly',
+ 'Extract with unbalanced parentheses'
);
test(
'Epic XSS <script>alert("XSS")</script> is epic', 'Epic
XSS',
- '<b>Epic XSS</b> <script>alert</script> is
epic'
+ '<b>Epic XSS</b> <script>alert</script> is
epic',
+ 'XSS Attack'
+ );
+
+ test(
+ 'Foo\'s pub is a pub in Bar', 'Foo\'s pub',
+ '<b>Foo&#039;s pub</b> is a pub in Bar',
+ 'Correct escaping'
+ );
+
+ test(
+ '*Testing if Things are correctly identified', 'Things',
+ '*Testing if <b>Things</b> are correctly identified',
+ 'Article that begins with asterisk'
);
} );
--
To view, visit https://gerrit.wikimedia.org/r/176908
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I02222bbff84532f63cac67af1bf889c328ec6ff2
Gerrit-PatchSet: 12
Gerrit-Project: mediawiki/extensions/Popups
Gerrit-Branch: master
Gerrit-Owner: Prtksxna <[email protected]>
Gerrit-Reviewer: AndyRussG <[email protected]>
Gerrit-Reviewer: CSteipp <[email protected]>
Gerrit-Reviewer: Krinkle <[email protected]>
Gerrit-Reviewer: MaxSem <[email protected]>
Gerrit-Reviewer: Prtksxna <[email protected]>
Gerrit-Reviewer: Werdna <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits