Mwjames has uploaded a new change for review. https://gerrit.wikimedia.org/r/62536
Change subject: Add smw.dataItem.wikiPage.getText() ...................................................................... Add smw.dataItem.wikiPage.getText() * wikiPage.getText() outputs text without hash fragment allowing in-page objects such as subobjects (Foo#_QUERY9a665a578eb95c1) be represented by its normal wikipage (Foo) * Fix some jsduck docs ext.smw.dataItem.wikiPage.test covers now 73 tests Change-Id: I0a355ebd7ee03ab30467ef510ff97da34ac2877a --- M resources/smw/data/ext.smw.dataItem.wikiPage.js M tests/qunit/smw/data/ext.smw.dataItem.wikiPage.test.js 2 files changed, 151 insertions(+), 47 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/SemanticMediaWiki refs/changes/36/62536/1 diff --git a/resources/smw/data/ext.smw.dataItem.wikiPage.js b/resources/smw/data/ext.smw.dataItem.wikiPage.js index 22b94a2..d7bd9e2 100644 --- a/resources/smw/data/ext.smw.dataItem.wikiPage.js +++ b/resources/smw/data/ext.smw.dataItem.wikiPage.js @@ -1,37 +1,56 @@ /** - * SMW wikiPage DataItem JavaScript representation + * This file is part of the Semantic MediaWiki Extension + * @see https://semantic-mediawiki.org/ * - * @see SMW\DIWikiPage + * @section LICENSE + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * * @since 1.9 * - * @file * @ingroup SMW * - * @licence GNU GPL v2 or later + * @license GNU GPL v2+ * @author mwjames */ ( function( $, mw, smw ) { 'use strict'; + /** + * Helper method for generating proper html + * + * @ignore + */ var html = mw.html; /** - * Inheritance class + * Inheritance class for the smw.dataItem constructor * - * @type object + * @since 1.9 + * + * @class smw.dataItem + * @abstract */ smw.dataItem = smw.dataItem || {}; /** - * wikiPage constructor + * Initializes the constructor * - * @since 1.9 - * - * @param {String} fulltext - * @param {String} fullurl - * @param {Integer} namespace - * @return {Object} this + * @param {string} fulltext + * @param {string} fullurl + * @param {number} namespace + * @return {this} */ var wikiPage = function ( fulltext, fullurl, namespace, exists ) { this.fulltext = fulltext !== ''&& fulltext !== undefined ? fulltext : null; @@ -48,9 +67,14 @@ }; /** - * Constructor + * A class that includes methods to create a wikiPage dataItem representation + * in JavaScript that corresponds to the SMW\DIWikiPage object in PHP * - * @var Object + * @since 1.9 + * + * @class + * @constructor + * @extends smw.dataItem */ smw.dataItem.wikiPage = function( fulltext, fullurl, namespace, exists ) { if ( $.type( fulltext ) === 'string' && $.type( fullurl ) === 'string' ) { @@ -61,9 +85,7 @@ }; /** - * Creates an object with methods related to the wikiPage dataItem - * - * @see SMW\DIWikiPage + * Specifies methods related to the wikiPage dataItem representation * * @since 1.9 * @@ -85,10 +107,8 @@ }, /** - * Returns wikiPage text title - * - * Get full name in text form, like "File:Foo bar.jpg" due to fact - * that name is serialized in fulltext + * Returns wikiPage text title as full name like "File:Foo bar.jpg" + * due to fact that the name is serialized in fulltext * * @since 1.9 * @@ -96,6 +116,17 @@ */ getFullText: function() { return this.fulltext; + }, + + /** + * Returns main part of the title without any fragment + * + * @since 1.9 + * + * @return {string} + */ + getText: function() { + return this.fulltext && this.fulltext.split( '#' )[0]; }, /** @@ -154,9 +185,9 @@ getHtml: function( linker ) { if ( linker && this.fullurl !== null ){ var attributes = this.exists ? { 'href': this.fullurl } : { 'href': this.fullurl, 'class': 'new' }; - return html.element( 'a', attributes , this.fulltext ); + return html.element( 'a', attributes , this.getText() ); } - return this.fulltext; + return this.getText(); } }; diff --git a/tests/qunit/smw/data/ext.smw.dataItem.wikiPage.test.js b/tests/qunit/smw/data/ext.smw.dataItem.wikiPage.test.js index 1fa7e56..0f4f1a1 100644 --- a/tests/qunit/smw/data/ext.smw.dataItem.wikiPage.test.js +++ b/tests/qunit/smw/data/ext.smw.dataItem.wikiPage.test.js @@ -1,12 +1,27 @@ /** - * QUnit tests + * This file is part of the Semantic MediaWiki Extension + * @see https://semantic-mediawiki.org/ + * + * @section LICENSE + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * * @since 1.9 * - * @file - * @ingroup SMW + * @ingroup Tests * - * @licence GNU GPL v2 or later + * @license GNU GPL v2+ * @author mwjames */ ( function ( $, mw, smw ) { @@ -18,13 +33,46 @@ // Data provider var testCases = [ - { test: [ '', '', '' ], expected: [ null, null, 0 , null, true] } , - { test: [ 'foo', '', '' ], expected: [ 'foo', null, 0, 'foo', true] }, - { test: [ 'bar', '', 6 ], expected: [ 'bar', null, 6, 'bar', true ] }, - { test: [ 'bar', '', 0, true ], expected: [ 'bar', null, 0, 'bar', true ] }, - { test: [ 'bar', '', 2, false ], expected: [ 'bar', null, 2, 'bar', false ] }, - { test: [ 'fooBar', 'http://fooBar', 0, false ], expected: [ 'fooBar', 'http://fooBar', 0, '<a href=\"http://fooBar\" class=\"new\">fooBar</a>', false ] }, - { test: [ 'fooBar', 'http://fooBar', 0, true ], expected: [ 'fooBar', 'http://fooBar', 0, '<a href=\"http://fooBar\">fooBar</a>', true ] } + { + test: [ '', '', '' ], + expected: { 'name': null, 'text': null, 'uri': null, 'ns': 0, 'html': null, 'known': true } + } , + { + test: [ 'foo', '', '' ], + expected: { 'name': 'foo', 'text': 'foo', 'uri': null, 'ns': 0, 'html': 'foo', 'known': true } + }, + { + test: [ 'bar', '', 6 ], + expected: { 'name': 'bar', 'text': 'bar', 'uri': null, 'ns': 6, 'html': 'bar', 'known': true } + }, + { + test: [ 'bar', '', 0, true ], + expected: { 'name': 'bar', 'text': 'bar', 'uri': null, 'ns': 0, 'html': 'bar', 'known': true } + }, + { + test: [ 'bar', '', 2, false ], + expected: { 'name': 'bar', 'text': 'bar', 'uri': null, 'ns': 2, 'html': 'bar', 'known': false } + }, + { + test: [ 'fooBar', 'http://fooBar', 0, false ], + expected: { 'name': 'fooBar', 'text': 'fooBar', 'uri': 'http://fooBar', 'ns': 0, 'html': '<a href=\"http://fooBar\" class=\"new\">fooBar</a>', 'known': false } + }, + { + test: [ 'fooBar#_9a0c8abb8ef729b5c7', 'http://fooBar#_9a0c8abb8ef729b5c7', 0, false ], + expected: { 'name': 'fooBar#_9a0c8abb8ef729b5c7', 'text': 'fooBar', 'uri': 'http://fooBar#_9a0c8abb8ef729b5c7', 'ns': 0, 'html': '<a href=\"http://fooBar#_9a0c8abb8ef729b5c7\" class=\"new\">fooBar</a>', 'known': false } + }, + { + test: [ 'fooBar', 'http://fooBar', 0, true ], + expected: { 'name': 'fooBar', 'text': 'fooBar', 'uri': 'http://fooBar', 'ns': 0, 'html': '<a href=\"http://fooBar\">fooBar</a>', 'known': true } + }, + { + test: [ 'Foo#_QUERY9a665a578eb95c1', 'http://Foo#_QUERY9a665a578eb95c1', 0, true ], + expected: { 'name': 'Foo#_QUERY9a665a578eb95c1', 'text': 'Foo', 'uri': 'http://Foo#_QUERY9a665a578eb95c1', 'ns': 0, 'html': '<a href=\"http://Foo#_QUERY9a665a578eb95c1\">Foo</a>', 'known': true } + }, + { + test: [ 'Foo#_QUERY#9a665a578eb95c1', 'http://Foo#_QUERY#9a665a578eb95c1', 0, true ], + expected: { 'name': 'Foo#_QUERY#9a665a578eb95c1', 'text': 'Foo', 'uri': 'http://Foo#_QUERY#9a665a578eb95c1', 'ns': 0, 'html': '<a href=\"http://Foo#_QUERY#9a665a578eb95c1\">Foo</a>', 'known': true } + } ]; /** @@ -52,16 +100,31 @@ } ); /** - * Test getName + * Test getPrefixedText * * @since: 1.9 */ - QUnit.test( 'getName', 7, function ( assert ) { + QUnit.test( 'getPrefixedText', 10, function ( assert ) { var result; $.map( testCases, function ( testCase ) { result = new smw.dataItem.wikiPage( testCase.test[0], testCase.test[1], testCase.test[2] ); - assert.equal( result.getName(), testCase.expected[0] , pass + 'returned ' + testCase.expected[0] ); + assert.equal( result.getPrefixedText(), testCase.expected.name , pass + 'returned ' + testCase.expected.name ); + } ); + + } ); + + /** + * Test getText + * + * @since: 1.9 + */ + QUnit.test( 'getText', 10, function ( assert ) { + var result; + + $.map( testCases, function ( testCase ) { + result = new smw.dataItem.wikiPage( testCase.test[0], testCase.test[1], testCase.test[2] ); + assert.equal( result.getText(), testCase.expected.text , pass + 'returned ' + testCase.expected.text ); } ); } ); @@ -71,12 +134,12 @@ * * @since: 1.9 */ - QUnit.test( 'getUri', 7, function ( assert ) { + QUnit.test( 'getUri', 10, function ( assert ) { var result; $.map( testCases, function ( testCase ) { result = new smw.dataItem.wikiPage( testCase.test[0], testCase.test[1], testCase.test[2] ); - assert.equal( result.getUri(), testCase.expected[1] , pass + 'returned ' + testCase.expected[1] ); + assert.equal( result.getUri(), testCase.expected.uri , pass + 'returned ' + testCase.expected.uri ); } ); } ); @@ -86,12 +149,12 @@ * * @since: 1.9 */ - QUnit.test( 'getNamespace', 7, function ( assert ) { + QUnit.test( 'getNamespace', 10, function ( assert ) { var result; $.map( testCases, function ( testCase ) { result = new smw.dataItem.wikiPage( testCase.test[0], testCase.test[1], testCase.test[2] ); - assert.equal( result.getNamespace(), testCase.expected[2] , pass + 'returned ' + testCase.expected[2] ); + assert.equal( result.getNamespace(), testCase.expected.ns , pass + 'returned ' + testCase.expected.ns ); } ); } ); @@ -115,12 +178,12 @@ * * @since: 1.9 */ - QUnit.test( 'isKnown', 7, function ( assert ) { + QUnit.test( 'isKnown', 10, function ( assert ) { var result; $.map( testCases, function ( testCase ) { result = new smw.dataItem.wikiPage( testCase.test[0], testCase.test[1], testCase.test[2], testCase.test[3] ); - assert.equal( result.isKnown(), testCase.expected[4] , pass + 'returned ' + testCase.expected[4] ); + assert.equal( result.isKnown(), testCase.expected.known , pass + 'returned ' + testCase.expected.known ); } ); } ); @@ -130,17 +193,27 @@ * * @since: 1.9 */ - QUnit.test( 'getHtml', 14, function ( assert ) { + QUnit.test( 'getHtml( false )', 10, function ( assert ) { var result; $.map( testCases, function ( testCase ) { result = new smw.dataItem.wikiPage( testCase.test[0], testCase.test[1], testCase.test[2] ); - assert.equal( result.getHtml(), testCase.expected[0] , pass + 'returned ' + testCase.expected[0] ); + assert.equal( result.getHtml( false ), testCase.expected.text , pass + 'returned ' + testCase.expected.text ); } ); + + } ); + + /** + * Test getHtml + * + * @since: 1.9 + */ + QUnit.test( 'getHtml( true )', 10, function ( assert ) { + var result; $.map( testCases, function ( testCase ) { result = new smw.dataItem.wikiPage( testCase.test[0], testCase.test[1], testCase.test[2], testCase.test[3] ); - assert.equal( result.getHtml( true ), testCase.expected[3] , pass + 'returned ' + testCase.expected[3] ); + assert.equal( result.getHtml( true ), testCase.expected.html , pass + 'returned ' + testCase.expected.html ); } ); } ); -- To view, visit https://gerrit.wikimedia.org/r/62536 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I0a355ebd7ee03ab30467ef510ff97da34ac2877a Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/SemanticMediaWiki Gerrit-Branch: master Gerrit-Owner: Mwjames <[email protected]> _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
