mediawiki-extensions-Visual.../modules/ve-mw/dm/metaitems/ve.dm.MWCategoryMetaItem.js
Bartosz Dziewoński 362df66b47 Remove support for page title "hrefPrefix" other than './'
Parsoid does not use relative links anywhere anymore (T72743). There
is no reason for us to support this. And previous code allowed
"hrefPrefix" to be empty '' sometimes, which is scary, as it could
lead to XSS vulnerabilities if titles starting with 'JavaScript:' are
not handled correctly elsewhere.

Bug: T206357
Depends-On: I8728f63084902c76d1c61193be4367939b069f1a
Change-Id: I99be18877aae2b505cf261bd7cdef6cf0d7a8670
2019-04-03 20:10:05 +00:00

82 lines
2.4 KiB
JavaScript

/*!
* VisualEditor DataModel MWCategoryMetaItem class.
*
* @copyright 2011-2019 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* DataModel category meta item.
*
* @class
* @extends ve.dm.MetaItem
* @constructor
* @param {Object} element Reference to element in meta-linmod
*/
ve.dm.MWCategoryMetaItem = function VeDmMWCategoryMetaItem() {
// Parent constructor
ve.dm.MWCategoryMetaItem.super.apply( this, arguments );
};
/* Inheritance */
OO.inheritClass( ve.dm.MWCategoryMetaItem, ve.dm.MetaItem );
/* Static Properties */
ve.dm.MWCategoryMetaItem.static.name = 'mwCategory';
ve.dm.MWCategoryMetaItem.static.group = 'mwCategory';
ve.dm.MWCategoryMetaItem.static.matchTagNames = [ 'link' ];
ve.dm.MWCategoryMetaItem.static.matchRdfaTypes = [ 'mw:PageProp/Category' ];
ve.dm.MWCategoryMetaItem.static.toDataElement = function ( domElements ) {
var href = domElements[ 0 ].getAttribute( 'href' ),
data = ve.parseParsoidResourceName( href ),
rawTitleAndFragment = data.rawTitle.match( /^(.*?)(?:#(.*))?$/ ),
titleAndFragment = data.title.match( /^(.*?)(?:#(.*))?$/ );
return {
type: this.name,
attributes: {
category: titleAndFragment[ 1 ],
origCategory: rawTitleAndFragment[ 1 ],
sortkey: titleAndFragment[ 2 ] || '',
origSortkey: rawTitleAndFragment[ 2 ] || ''
}
};
};
ve.dm.MWCategoryMetaItem.static.toDomElements = function ( dataElement, doc ) {
var href, encodedCategory,
domElement = doc.createElement( 'link' ),
category = dataElement.attributes.category || '',
sortkey = dataElement.attributes.sortkey || '',
origCategory = dataElement.attributes.origCategory || '',
origSortkey = dataElement.attributes.origSortkey || '',
normalizedOrigCategory = ve.decodeURIComponentIntoArticleTitle( origCategory ),
normalizedOrigSortkey = ve.decodeURIComponentIntoArticleTitle( origSortkey );
if ( normalizedOrigSortkey === sortkey ) {
sortkey = origSortkey;
} else {
sortkey = encodeURIComponent( sortkey );
}
if ( normalizedOrigCategory === category ) {
encodedCategory = origCategory;
} else {
encodedCategory = encodeURIComponent( category );
}
domElement.setAttribute( 'rel', 'mw:PageProp/Category' );
href = './' + encodedCategory;
if ( sortkey !== '' ) {
href += '#' + sortkey;
}
domElement.setAttribute( 'href', href );
return [ domElement ];
};
/* Registration */
ve.dm.modelRegistry.register( ve.dm.MWCategoryMetaItem );