ve.dm.MWTransclusionModel: Don't create undefined normalisation target

The API gives us normalisation information about all titles,
including titles of which it couldn't provide any template data.

Spec storage should only have properties with valid values
(has property means to have a valid value, like database). The
absence of this check was causing it to turn this:

{
  pages: {
    'Template:Unsigned': { params: { .. } }
  },
  normalised: [
    { from: 'foo bar', to: 'Foo_bar' },
    { from: 'template: unsigned', to: 'Template:Unsigned' }
  ]
}

into:

{
  pages: {
    'foo bar': undefined
  },
  normalised: [
    { from: 'foo bar', to: 'Foo_bar' }
  ]
}

Bug: 49493
Change-Id: I0b661744d0388345561897c9631f15e691737031
This commit is contained in:
Timo Tijhof 2013-06-20 15:09:42 -07:00
parent 4b74101e2d
commit 8f9093ea07

View file

@ -7,6 +7,9 @@
/*global mw */
( function () {
var hasOwn = Object.hasOwnProperty;
/**
* MediaWiki transclusion model.
*
@ -128,13 +131,17 @@ ve.dm.MWTransclusionModel.prototype.fetchSpecs = function ( templates ) {
// Cross-reference under normalized titles
if ( data.normalized ) {
for ( i = 0, len = data.normalized.length; i < len; i++ ) {
specs[data.normalized[i].from] = specs[data.normalized[i].to];
// Only define the alias if the target exists, otherwise
// we create a new property with an invalid "undefined" value.
if ( hasOwn.call( specs, data.normalized[i].to ) ) {
specs[data.normalized[i].from] = specs[data.normalized[i].to];
}
}
}
// Load into existing templates
for ( i = 0, len = templates.length; i < len; i++ ) {
title = templates[i].getTitle();
if ( specs.hasOwnProperty( title ) ) {
if ( hasOwn.call( specs, title ) ) {
templates[i].getSpec().extend( specs[title] );
}
}
@ -228,7 +235,7 @@ ve.dm.MWTransclusionModel.prototype.addTemplate = function ( target, index ) {
var part = new ve.dm.MWTemplateModel( this, target ),
title = part.getTitle();
if ( this.specs.hasOwnProperty( title ) ) {
if ( hasOwn.call( this.specs, title ) ) {
part.getSpec().extend( this.specs[title] );
}
this.addPart( part, index );
@ -321,3 +328,5 @@ ve.dm.MWTransclusionModel.prototype.getPartFromId = function ( id ) {
ve.dm.MWTransclusionModel.prototype.getTemplateSpec = function ( name ) {
return this.specs[name];
};
}() );