r92923 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r92922‎ | r92923 | r92924 >
Date:00:44, 23 July 2011
Author:brion
Status:ok (Comments)
Tags:
Comment:
Followup r86622: add initial QUnit test cases for jquery.textSelection module.

* tests .textSelection()'s encapsulateText method mostly (also uses setSelection, getContents, and getSelection)
* examples from WikiEditor toolbar: sig, bold, h2 (ownline), ulist (ownline & splitlines)
* confirms that splitlines works more or less as expected, at least for basic single-line, single split-line, and multi-line cases

Doesn't test the WikiEditor iframe mode since that's in a separate extension; when it's possible to test those things too, that'll need to be run there.

One of the h2 tests fails in IE6, returning selected text that's offset by one character from what's expected. I'm not sure whether it's actually selecting the wrong text or whether it's returning the wrong text -- needs further investigation.

Also note that there's no setContents submethod in textSelection, despite there being some notes about one.
Modified paths:
  • /trunk/phase3/tests/qunit/index.html (modified) (history)
  • /trunk/phase3/tests/qunit/suites/resources/jquery/jquery.textSelection.js (added) (history)

Diff [purge]

Index: trunk/phase3/tests/qunit/index.html
@@ -47,6 +47,7 @@
4848 <script src="../../resources/jquery/jquery.localize.js"></script>
4949 <script src="../../resources/jquery/jquery.tabIndex.js"></script>
5050 <script src="../../resources/jquery/jquery.tablesorter.js"></script>
 51+ <script src="../../resources/jquery/jquery.textSelection.js"></script>
5152 <script src="../../resources/mediawiki/mediawiki.Title.js"></script>
5253 <script src="../../resources/mediawiki.special/mediawiki.special.js"></script>
5354 <script src="../../resources/mediawiki.special/mediawiki.special.recentchanges.js"></script>
@@ -73,6 +74,7 @@
7475 <script src="suites/resources/jquery/jquery.localize.js"></script>
7576 <script src="suites/resources/jquery/jquery.tabIndex.js"></script>
7677 <script src="suites/resources/jquery/jquery.tablesorter.test.js" charset="UTF-8"></script>
 78+ <script src="suites/resources/jquery/jquery.textSelection.js" charset="UTF-8"></script>
7779 <script src="suites/resources/mediawiki/mediawiki.Title.js"></script>
7880 <script src="suites/resources/mediawiki.special/mediawiki.special.recentchanges.js"></script>
7981 </head>
Index: trunk/phase3/tests/qunit/suites/resources/jquery/jquery.textSelection.js
@@ -0,0 +1,216 @@
 2+module( 'jquery.textSelection.js' );
 3+
 4+test( '-- Initial check', function() {
 5+ expect(1);
 6+ ok( $.fn.textSelection, 'jQuery.fn.textSelection defined' );
 7+} );
 8+
 9+/**
 10+ * Test factory for $.fn.textSelection('encapsulateText')
 11+ *
 12+ * @param options {object} associative array containing:
 13+ * description {string}
 14+ * input {string}
 15+ * output {string}
 16+ * start {int} starting char for selection
 17+ * end {int} ending char for selection
 18+ * params {object} add'l parameters for $().textSelection('encapsulateText')
 19+ */
 20+var encapsulateTest = function( options ) {
 21+ var opt = $.extend({
 22+ description: '',
 23+ before: {},
 24+ after: {},
 25+ replace: {}
 26+ }, options);
 27+
 28+ opt.before = $.extend({
 29+ text: '',
 30+ start: 0,
 31+ end: 0
 32+ }, opt.before);
 33+ opt.after = $.extend({
 34+ text: '',
 35+ selected: null
 36+ }, opt.after);
 37+
 38+ test( opt.description, function() {
 39+ var tests = 1;
 40+ if (opt.after.selected !== null) {
 41+ tests++;
 42+ }
 43+ expect(tests);
 44+
 45+ var $fixture = $('<div id="qunit-fixture"></div>');
 46+ var $textarea = $('<textarea>');
 47+
 48+ $fixture.append($textarea);
 49+ $('body').append($fixture);
 50+
 51+ //$textarea.textSelection( 'setContents', opt.before.text); // this method is actually missing atm...
 52+ $textarea.val( opt.before.text ); // won't work with the WikiEditor iframe?
 53+
 54+ $textarea.textSelection( 'setSelection', {
 55+ start: opt.before.start,
 56+ end: opt.before.end
 57+ });
 58+
 59+ $textarea.textSelection( 'encapsulateSelection', opt.replace );
 60+
 61+ var text = $textarea.textSelection( 'getContents' );
 62+
 63+ equal( text, opt.after.text, 'Checking full text after encapsulation' );
 64+
 65+ if (opt.after.selected !== null) {
 66+ var selected = $textarea.textSelection( 'getSelection' );
 67+ equal( selected, opt.after.selected, 'Checking selected text after encapsulation.' );
 68+ }
 69+
 70+ } );
 71+};
 72+
 73+var sig = {
 74+ 'pre': "--~~~~"
 75+}, bold = {
 76+ pre: "'''",
 77+ peri: 'Bold text',
 78+ post: "'''"
 79+}, h2 = {
 80+ 'pre': '== ',
 81+ 'peri': 'Heading 2',
 82+ 'post': ' ==',
 83+ 'regex': /^(\s*)(={1,6})(.*?)\2(\s*)$/,
 84+ 'regexReplace': "\$1==\$3==\$4",
 85+ 'ownline': true
 86+}, ulist = {
 87+ 'pre': "* ",
 88+ 'peri': 'Bulleted list item',
 89+ 'post': "",
 90+ 'ownline': true,
 91+ 'splitlines': true
 92+};
 93+
 94+encapsulateTest({
 95+ description: "Adding sig to end of text",
 96+ before: {
 97+ text: "Wikilove dude! ",
 98+ start: 15,
 99+ end: 15
 100+ },
 101+ after: {
 102+ text: "Wikilove dude! --~~~~",
 103+ selected: ""
 104+ },
 105+ replace: sig
 106+});
 107+
 108+encapsulateTest({
 109+ description: "Adding bold to empty",
 110+ before: {
 111+ text: "",
 112+ start: 0,
 113+ end: 0
 114+ },
 115+ after: {
 116+ text: "'''Bold text'''",
 117+ selected: "Bold text" // selected because it's the default
 118+ },
 119+ replace: bold
 120+});
 121+
 122+encapsulateTest({
 123+ description: "Adding bold to existing text",
 124+ before: {
 125+ text: "Now is the time for all good men to come to the aid of their country",
 126+ start: 20,
 127+ end: 32
 128+ },
 129+ after: {
 130+ text: "Now is the time for '''all good men''' to come to the aid of their country",
 131+ selected: "" // empty because it's not the default'
 132+ },
 133+ replace: bold
 134+});
 135+
 136+encapsulateTest({
 137+ description: "ownline option: adding new h2",
 138+ before: {
 139+ text:"Before\nAfter",
 140+ start: 7,
 141+ end: 7
 142+ },
 143+ after: {
 144+ text: "Before\n== Heading 2 ==\nAfter",
 145+ selected: "Heading 2"
 146+ },
 147+ replace: h2
 148+});
 149+
 150+encapsulateTest({
 151+ description: "ownline option: turn a whole line into new h2",
 152+ before: {
 153+ text:"Before\nMy heading\nAfter",
 154+ start: 7,
 155+ end: 17
 156+ },
 157+ after: {
 158+ text: "Before\n== My heading ==\nAfter",
 159+ selected: ""
 160+ },
 161+ replace: h2
 162+});
 163+
 164+
 165+encapsulateTest({
 166+ description: "ownline option: turn a partial line into new h2",
 167+ before: {
 168+ text:"BeforeMy headingAfter",
 169+ start: 6,
 170+ end: 16
 171+ },
 172+ after: {
 173+ text: "Before\n== My heading ==\nAfter",
 174+ selected: ""
 175+ },
 176+ replace: h2
 177+});
 178+
 179+
 180+encapsulateTest({
 181+ description: "splitlines option: no selection, insert new list item",
 182+ before: {
 183+ text: "Before\nAfter",
 184+ start: 7,
 185+ end: 7
 186+ },
 187+ after: {
 188+ text: "Before\n* Bulleted list item\nAfter"
 189+ },
 190+ replace: ulist
 191+});
 192+
 193+encapsulateTest({
 194+ description: "splitlines option: single partial line selection, insert new list item",
 195+ before: {
 196+ text: "BeforeMy List ItemAfter",
 197+ start: 6,
 198+ end: 18
 199+ },
 200+ after: {
 201+ text: "Before\n* My List Item\nAfter"
 202+ },
 203+ replace: ulist
 204+});
 205+
 206+encapsulateTest({
 207+ description: "splitlines option: multiple lines",
 208+ before: {
 209+ text: "Before\nFirst\nSecond\nThird\nAfter",
 210+ start: 7,
 211+ end: 25
 212+ },
 213+ after: {
 214+ text: "Before\n* First\n* Second\n* Third\nAfter"
 215+ },
 216+ replace: ulist
 217+});
Property changes on: trunk/phase3/tests/qunit/suites/resources/jquery/jquery.textSelection.js
___________________________________________________________________
Added: svn:eol-style
1218 + native

Follow-up revisions

RevisionCommit summaryAuthorDate
r110717[tests] use core qunit-fixture properly...krinkle12:27, 5 February 2012

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r86622(bug 22209, bug 22574) Make indent, bullet list and numbered list buttons in ...catrope13:42, 21 April 2011
r89403(bug 29105) Splitlines completely messes up selection of inserted text. This ...catrope10:08, 3 June 2011

Comments

#Comment by DieBuche (talk | contribs)   18:53, 25 July 2011
#Comment by Catrope (talk | contribs)   09:54, 11 August 2011
+ * @param options {object} associative array containing:
+ *   description {string}
+ *   input {string}
+ *   output {string}
+ *   start {int} starting char for selection
+ *   end {int} ending char for selection
+ *   params {object} add'l parameters for $().textSelection('encapsulateText')

This bit of documentation is almost completely wrong, probably outdated.

OK otherwise. All the broken tests have been fixed in the meantime.

#Comment by Krinkle (talk | contribs)   12:27, 5 February 2012
+		var $fixture = $('<div id="qunit-fixture"></div>');
+		var $textarea = $('<textarea>');
+
+		$fixture.append($textarea);
+		$('body').append($fixture);

This was causing several <div id="qunit-fixture"></div> elements to be appended to the DOM and causing other tests to randomly fail due to ID conflicts. The QUnit test suite provides this by default, and when it doesn't it should atleast be removed/clean up after.

Fixed in r110717.

Status & tagging log