Skip to content

Commit

Permalink
Set relativeLinkPrefix before setting titlePageURI
Browse files Browse the repository at this point in the history
* Fixes the inconsistency reported in T155070 where internal cite
  links were missing the "./" prefix.

* Since parser tests cannot easily verify this expectation (which is
  how we couldn't catch this in the first place), added a mocha test
  as a regression spec to ensure that internal cite links continue
  to use "./" prefixed urls.

Change-Id: Id028ebcd496563fc77b85700780c572b3cc267f2
  • Loading branch information
subbuss committed Jan 20, 2017
1 parent 882e23d commit a391693
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/config/MWParserEnvironment.js
Expand Up @@ -370,12 +370,17 @@ MWParserEnvironment.prototype.initializeForPageName = function(pageName, dontRes
var title = this.makeTitleFromURLDecodedStr(pageName);
this.page.ns = title.getNamespace()._id;
this.page.title = title;
this.page.titleURI = this.makeLink(title);
this.page.name = pageName;

// Always prefix a ./ so that we don't have to escape colons. Those
// would otherwise fool browsers into treating namespaces (like File:)
// as protocols.
this.page.relativeLinkPrefix = "./";

// makeLink uses the relative link prefix => this should always
// be done after that initialization.
this.page.titleURI = this.makeLink(title);

if (!dontReset) {
this.initUID();
}
Expand Down
35 changes: 35 additions & 0 deletions tests/mocha/regression.specs.js
@@ -0,0 +1,35 @@
'use strict';
/*global describe, it*/

require('../../core-upgrade.js');
require("chai").should();
var ParsoidConfig = require('../../lib/config/ParsoidConfig.js').ParsoidConfig;
var helpers = require('./test.helpers.js');

// FIXME: MWParserEnvironment.getParserEnv and switchToConfig both require
// mwApiMap to be setup. This forces us to load WMF config. Fixing this
// will require some changes to ParsoidConfig and MWParserEnvironment.
var parsoidConfig = new ParsoidConfig(null, { loadWMF: true, defaultWiki: 'enwiki' });
var parse = function(src, options) {
return helpers.parse(parsoidConfig, src, options).then(function(ret) {
return ret.doc;
});
};

// These are regression specs for when we fix bugs that cannot be easily
// verified with the parser tests framework
describe('Regression Specs', function() {

// Wikilinks use ./ prefixed urls. For reasons of consistency,
// we should use a similar format for internal cite urls.
// This spec ensures that we don't inadvertently break that requirement.
it('should use ./ prefixed urls for cite links', function() {
return parse('a [[Foo]] <ref>b</ref>').then(function(result) {
result.body.querySelector(".mw-ref a").getAttribute('href').
should.equal('./Main_Page#cite_note-1');
result.body.querySelector("#cite_note-1 a").getAttribute('href').
should.equal('./Main_Page#cite_ref-1');
});
});

});

0 comments on commit a391693

Please sign in to comment.