2016-06-14 22:27:44 +00:00
|
|
|
/*!
|
2020-03-20 17:08:24 +00:00
|
|
|
* Parsoid utilities.
|
2016-06-14 22:27:44 +00:00
|
|
|
*
|
2023-12-01 16:06:11 +00:00
|
|
|
* @copyright See AUTHORS.txt
|
2016-06-14 22:27:44 +00:00
|
|
|
*/
|
|
|
|
|
2020-03-20 17:08:24 +00:00
|
|
|
mw.libs.ve = mw.libs.ve || {};
|
|
|
|
|
2016-06-14 22:27:44 +00:00
|
|
|
/**
|
|
|
|
* Decode a URI component into a mediawiki article title
|
|
|
|
*
|
|
|
|
* N.B. Illegal article titles can result from fairly reasonable input (e.g. "100%25beef");
|
|
|
|
* see https://phabricator.wikimedia.org/T137847 .
|
|
|
|
*
|
|
|
|
* @param {string} s String to decode
|
2022-02-14 15:18:57 +00:00
|
|
|
* @param {boolean} [preserveUnderscores=false] Don't convert underscores to spaces
|
2016-06-14 22:27:44 +00:00
|
|
|
* @return {string} Decoded string, or original string if decodeURIComponent failed
|
|
|
|
*/
|
2020-03-20 17:08:24 +00:00
|
|
|
mw.libs.ve.decodeURIComponentIntoArticleTitle = function ( s, preserveUnderscores ) {
|
2016-06-14 22:27:44 +00:00
|
|
|
try {
|
|
|
|
s = decodeURIComponent( s );
|
|
|
|
} catch ( e ) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
if ( preserveUnderscores ) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
return s.replace( /_/g, ' ' );
|
|
|
|
};
|
2017-10-26 19:27:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Unwrap Parsoid sections
|
|
|
|
*
|
2023-01-31 15:14:16 +00:00
|
|
|
* data-mw-section-id attributes are copied to the first child (the heading) during
|
|
|
|
* this step so that we can place the cursor in the correct place when section editing.
|
|
|
|
* These attributes **must be removed** before being sent back to Parsoid to avoid
|
|
|
|
* unnecessary re-serialization.
|
|
|
|
*
|
2017-10-26 19:27:21 +00:00
|
|
|
* @param {HTMLElement} element Parent element, e.g. document body
|
2020-03-31 20:04:30 +00:00
|
|
|
* @param {string} [keepSection] Section to keep
|
2017-10-26 19:27:21 +00:00
|
|
|
*/
|
2020-03-20 17:08:24 +00:00
|
|
|
mw.libs.ve.unwrapParsoidSections = function ( element, keepSection ) {
|
2024-04-30 16:44:25 +00:00
|
|
|
Array.prototype.forEach.call( element.querySelectorAll( 'section[data-mw-section-id]' ), ( section ) => {
|
2024-05-21 14:22:56 +00:00
|
|
|
const parent = section.parentNode,
|
2019-02-13 13:21:26 +00:00
|
|
|
sectionId = section.getAttribute( 'data-mw-section-id' );
|
|
|
|
// Copy section ID to first child (should be a heading)
|
2020-05-11 17:04:37 +00:00
|
|
|
// Pseudo-sections (with negative section IDs) may not have a heading
|
|
|
|
if ( sectionId !== null && +sectionId > 0 ) {
|
2019-02-13 13:21:26 +00:00
|
|
|
section.firstChild.setAttribute( 'data-mw-section-id', sectionId );
|
|
|
|
}
|
2020-03-31 20:04:30 +00:00
|
|
|
if ( keepSection !== undefined && sectionId === keepSection ) {
|
2019-02-13 13:21:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-10-26 19:27:21 +00:00
|
|
|
while ( section.firstChild ) {
|
|
|
|
parent.insertBefore( section.firstChild, section );
|
|
|
|
}
|
|
|
|
parent.removeChild( section );
|
|
|
|
} );
|
|
|
|
};
|
2017-12-06 20:24:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Strip legacy (non-HTML5) IDs; typically found as section IDs inside
|
|
|
|
* headings.
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} element Parent element, e.g. document body
|
|
|
|
*/
|
2020-03-20 17:08:24 +00:00
|
|
|
mw.libs.ve.stripParsoidFallbackIds = function ( element ) {
|
2024-04-30 16:44:25 +00:00
|
|
|
Array.prototype.forEach.call( element.querySelectorAll( 'span[typeof="mw:FallbackId"][id]:empty' ), ( legacySpan ) => {
|
2017-12-06 20:24:49 +00:00
|
|
|
legacySpan.parentNode.removeChild( legacySpan );
|
|
|
|
} );
|
|
|
|
};
|
2017-08-13 19:22:04 +00:00
|
|
|
|
2020-06-10 19:31:16 +00:00
|
|
|
mw.libs.ve.restbaseIdRegExp = /^mw[a-zA-Z0-9\-_]{2,6}$/;
|
|
|
|
|
|
|
|
mw.libs.ve.stripRestbaseIds = function ( doc ) {
|
2024-05-21 14:22:56 +00:00
|
|
|
const restbaseIdRegExp = mw.libs.ve.restbaseIdRegExp;
|
2024-04-30 16:44:25 +00:00
|
|
|
Array.prototype.forEach.call( doc.querySelectorAll( '[id^="mw"]' ), ( element ) => {
|
2022-01-13 11:10:02 +00:00
|
|
|
if ( restbaseIdRegExp.test( element.id ) ) {
|
2020-06-10 19:31:16 +00:00
|
|
|
element.removeAttribute( 'id' );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2022-01-24 21:11:29 +00:00
|
|
|
/**
|
|
|
|
* Re-duplicate deduplicated TemplateStyles, for correct rendering when editing a section or
|
|
|
|
* when templates are removed during the edit.
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} element Parent element, e.g. document body
|
|
|
|
*/
|
|
|
|
mw.libs.ve.reduplicateStyles = function ( element ) {
|
2024-04-30 16:44:25 +00:00
|
|
|
Array.prototype.forEach.call( element.querySelectorAll( 'link[rel~="mw-deduplicated-inline-style"]' ), ( link ) => {
|
2024-05-21 14:22:56 +00:00
|
|
|
const href = link.getAttribute( 'href' );
|
2022-01-24 21:11:29 +00:00
|
|
|
if ( !href || href.slice( 0, 'mw-data:'.length ) !== 'mw-data:' ) {
|
|
|
|
return;
|
|
|
|
}
|
2024-05-21 14:22:56 +00:00
|
|
|
const key = href.slice( 'mw-data:'.length );
|
|
|
|
const style = element.querySelector( 'style[data-mw-deduplicate="' + key + '"]' );
|
2022-01-24 21:11:29 +00:00
|
|
|
if ( !style ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-05-21 14:22:56 +00:00
|
|
|
const newStyle = link.ownerDocument.createElement( 'style' );
|
2022-01-24 21:11:29 +00:00
|
|
|
newStyle.setAttribute( 'data-mw-deduplicate', key );
|
|
|
|
|
|
|
|
// Copy content from the old `style` node (for rendering)
|
2024-05-21 14:22:56 +00:00
|
|
|
for ( let i = 0; i < style.childNodes.length; i++ ) {
|
2022-01-24 21:11:29 +00:00
|
|
|
newStyle.appendChild( style.childNodes[ i ].cloneNode( true ) );
|
|
|
|
}
|
|
|
|
// Copy attributes from the old `link` node (for selser)
|
2024-04-30 16:44:25 +00:00
|
|
|
Array.prototype.forEach.call( link.attributes, ( attr ) => {
|
2022-01-24 21:11:29 +00:00
|
|
|
if ( attr.name !== 'rel' && attr.name !== 'href' ) {
|
|
|
|
newStyle.setAttribute( attr.name, attr.value );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
link.parentNode.replaceChild( newStyle, link );
|
|
|
|
} );
|
|
|
|
|
2024-04-30 16:44:25 +00:00
|
|
|
Array.prototype.forEach.call( element.querySelectorAll( 'style[data-mw-deduplicate]:empty' ), ( style ) => {
|
2024-05-21 14:22:56 +00:00
|
|
|
const key = style.getAttribute( 'data-mw-deduplicate' );
|
|
|
|
const firstStyle = element.querySelector( 'style[data-mw-deduplicate="' + key + '"]' );
|
2022-01-24 21:11:29 +00:00
|
|
|
if ( !firstStyle || firstStyle === style ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy content from the first matching `style` node (for rendering)
|
2024-05-21 14:22:56 +00:00
|
|
|
for ( let i = 0; i < firstStyle.childNodes.length; i++ ) {
|
2022-01-24 21:11:29 +00:00
|
|
|
style.appendChild( firstStyle.childNodes[ i ].cloneNode( true ) );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* De-duplicate TemplateStyles, like Parsoid does.
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} element Parent element, e.g. document body
|
|
|
|
*/
|
|
|
|
mw.libs.ve.deduplicateStyles = function ( element ) {
|
|
|
|
/**
|
|
|
|
* Check whether `node` is in a fosterable position. (Nodes in these positions may be moved
|
|
|
|
* elsewhere in the DOM by the HTML5 parsing algorithm, if they don't have the right tag name.)
|
|
|
|
* https://html.spec.whatwg.org/#appropriate-place-for-inserting-a-node
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {Node|null} node
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
function isFosterablePosition( node ) {
|
2024-05-21 14:22:56 +00:00
|
|
|
const fosterablePositions = [ 'table', 'thead', 'tbody', 'tfoot', 'tr' ];
|
2022-01-24 21:11:29 +00:00
|
|
|
return node && fosterablePositions.indexOf( node.parentNode.nodeName.toLowerCase() ) !== -1;
|
|
|
|
}
|
|
|
|
|
2024-05-21 14:22:56 +00:00
|
|
|
const styleTagKeys = {};
|
2022-01-24 21:11:29 +00:00
|
|
|
|
2024-04-30 16:44:25 +00:00
|
|
|
Array.prototype.forEach.call( element.querySelectorAll( 'style[data-mw-deduplicate]' ), ( style ) => {
|
2024-05-21 14:22:56 +00:00
|
|
|
const key = style.getAttribute( 'data-mw-deduplicate' );
|
2022-01-24 21:11:29 +00:00
|
|
|
|
|
|
|
if ( !styleTagKeys[ key ] ) {
|
|
|
|
// Not a dupe
|
|
|
|
styleTagKeys[ key ] = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !isFosterablePosition( style ) ) {
|
|
|
|
// Dupe - replace with a placeholder <link> reference
|
2024-05-21 14:22:56 +00:00
|
|
|
const link = style.ownerDocument.createElement( 'link' );
|
2022-01-24 21:11:29 +00:00
|
|
|
link.setAttribute( 'rel', 'mw-deduplicated-inline-style' );
|
|
|
|
link.setAttribute( 'href', 'mw-data:' + key );
|
|
|
|
|
|
|
|
// Copy attributes from the old `link` node (for selser)
|
2024-04-30 16:44:25 +00:00
|
|
|
Array.prototype.forEach.call( style.attributes, ( attr ) => {
|
2022-01-24 21:11:29 +00:00
|
|
|
if ( attr.name !== 'rel' && attr.name !== 'data-mw-deduplicate' ) {
|
|
|
|
link.setAttribute( attr.name, attr.value );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
style.parentNode.replaceChild( link, style );
|
|
|
|
} else {
|
|
|
|
// Duplicate style tag found in fosterable position.
|
|
|
|
// Not deduping it (to avoid corruption when the resulting HTML is parsed: T299767),
|
|
|
|
// but emptying out the style tag for consistency with Parsoid.
|
|
|
|
// Parsoid says it does this for performance reasons.
|
|
|
|
style.innerHTML = '';
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2019-01-03 17:11:29 +00:00
|
|
|
/**
|
|
|
|
* Fix fragment links which should be relative to the current document
|
|
|
|
*
|
|
|
|
* This prevents these links from trying to navigate to another page,
|
|
|
|
* or open in a new window.
|
|
|
|
*
|
|
|
|
* Call this after ve.targetLinksToNewWindow, as it removes the target attribute.
|
|
|
|
* Call this after LinkCache.styleParsoidElements, as it breaks that method by including the query string.
|
|
|
|
*
|
2019-04-16 15:17:29 +00:00
|
|
|
* @param {HTMLElement} container Parent element, e.g. document body
|
|
|
|
* @param {mw.Title} docTitle Current title, only links to this title will be normalized
|
2019-01-03 17:11:29 +00:00
|
|
|
* @param {string} [prefix] Prefix to add to fragment and target ID to avoid collisions
|
|
|
|
*/
|
2020-03-20 17:08:24 +00:00
|
|
|
mw.libs.ve.fixFragmentLinks = function ( container, docTitle, prefix ) {
|
2024-05-21 14:22:56 +00:00
|
|
|
const docTitleText = docTitle.getPrefixedText();
|
2019-01-03 17:11:29 +00:00
|
|
|
prefix = prefix || '';
|
2024-04-30 16:44:25 +00:00
|
|
|
Array.prototype.forEach.call( container.querySelectorAll( 'a[href*="#"]' ), ( el ) => {
|
2024-05-21 14:22:56 +00:00
|
|
|
let fragment = null;
|
2022-06-27 15:29:24 +00:00
|
|
|
if ( el.getAttribute( 'href' )[ 0 ] === '#' ) {
|
2022-12-12 19:49:26 +00:00
|
|
|
// Legacy parser
|
2022-06-27 15:29:24 +00:00
|
|
|
fragment = el.getAttribute( 'href' ).slice( 1 );
|
|
|
|
} else {
|
|
|
|
// Parsoid HTML
|
2024-05-21 14:22:56 +00:00
|
|
|
const targetData = mw.libs.ve.getTargetDataFromHref( el.href, el.ownerDocument );
|
2019-01-03 17:11:29 +00:00
|
|
|
|
2022-06-27 15:29:24 +00:00
|
|
|
if ( targetData.isInternal ) {
|
2024-05-21 14:22:56 +00:00
|
|
|
const title = mw.Title.newFromText( targetData.title );
|
2022-06-27 15:29:24 +00:00
|
|
|
if ( title && title.getPrefixedText() === docTitleText ) {
|
2022-12-12 19:49:26 +00:00
|
|
|
fragment = new URL( el.href ).hash.slice( 1 );
|
2019-01-03 17:11:29 +00:00
|
|
|
}
|
2022-06-27 15:29:24 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-03 17:11:29 +00:00
|
|
|
|
2022-06-27 15:29:24 +00:00
|
|
|
if ( fragment !== null ) {
|
|
|
|
if ( !fragment ) {
|
|
|
|
// Special case for empty fragment, even if prefix set
|
|
|
|
el.setAttribute( 'href', '#' );
|
|
|
|
} else {
|
|
|
|
if ( prefix ) {
|
2024-05-21 14:22:56 +00:00
|
|
|
const target = container.querySelector( '#' + $.escapeSelector( fragment ) );
|
2022-06-27 15:29:24 +00:00
|
|
|
// There may be multiple links to a specific target, so check the target
|
|
|
|
// hasn't already been fixed (in which case it would be null)
|
|
|
|
if ( target ) {
|
|
|
|
target.setAttribute( 'id', prefix + fragment );
|
|
|
|
target.setAttribute( 'data-mw-id-fixed', '' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
el.setAttribute( 'href', '#' + prefix + fragment );
|
2019-01-03 17:11:29 +00:00
|
|
|
}
|
2022-06-27 15:29:24 +00:00
|
|
|
el.removeAttribute( 'target' );
|
2019-01-03 17:11:29 +00:00
|
|
|
}
|
|
|
|
} );
|
2022-01-19 01:26:14 +00:00
|
|
|
// Remove any section heading anchors which weren't fixed above (T218492)
|
2024-04-30 16:44:25 +00:00
|
|
|
Array.prototype.forEach.call( container.querySelectorAll( 'h1, h2, h3, h4, h5, h6' ), ( el ) => {
|
2022-01-19 01:26:14 +00:00
|
|
|
if ( el.hasAttribute( 'id' ) && !el.hasAttribute( 'data-mw-id-fixed' ) ) {
|
|
|
|
el.removeAttribute( 'id' );
|
|
|
|
}
|
|
|
|
} );
|
2019-01-03 17:11:29 +00:00
|
|
|
};
|
|
|
|
|
2024-04-29 18:14:26 +00:00
|
|
|
/**
|
|
|
|
* @typedef {Object} TargetData
|
|
|
|
* @memberof mw.libs.ve
|
|
|
|
* @property {string} title The title of the internal link (if the href is internal)
|
|
|
|
* @property {boolean} isInternal True if the href pointed to the local wiki, false if href is external
|
|
|
|
*/
|
|
|
|
|
2020-03-20 17:08:24 +00:00
|
|
|
/**
|
|
|
|
* Parse URL to get title it points to.
|
|
|
|
*
|
|
|
|
* @param {string} href
|
2022-12-14 20:42:15 +00:00
|
|
|
* @param {HTMLDocument} doc Document whose base URL to use
|
2024-04-29 18:14:26 +00:00
|
|
|
* @return {mw.libs.ve.TargetData} Information about the given href
|
2020-03-20 17:08:24 +00:00
|
|
|
*/
|
|
|
|
mw.libs.ve.getTargetDataFromHref = function ( href, doc ) {
|
|
|
|
function regexEscape( str ) {
|
|
|
|
return str.replace( /([.?*+^$[\]\\(){}|-])/g, '\\$1' );
|
|
|
|
}
|
|
|
|
|
2023-01-30 14:57:56 +00:00
|
|
|
function returnExternalData() {
|
|
|
|
return { isInternal: false };
|
|
|
|
}
|
|
|
|
|
|
|
|
function returnInternalData( titleish ) {
|
|
|
|
// This value doesn't necessarily come from Parsoid (and it might not have the "./" prefix), but
|
|
|
|
// this method will work fine.
|
2024-05-21 14:22:56 +00:00
|
|
|
const data = mw.libs.ve.parseParsoidResourceName( titleish );
|
2023-01-30 14:57:56 +00:00
|
|
|
data.isInternal = true;
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2024-05-21 14:22:56 +00:00
|
|
|
const url = new URL( href, doc.baseURI );
|
2020-03-20 17:08:24 +00:00
|
|
|
|
2022-03-30 12:34:11 +00:00
|
|
|
// Equivalent to `ve.init.platform.getExternalLinkUrlProtocolsRegExp()`, which can not be called here
|
2024-05-21 14:22:56 +00:00
|
|
|
const externalLinkUrlProtocolsRegExp = new RegExp( '^(' + mw.config.get( 'wgUrlProtocols' ) + ')', 'i' );
|
2023-01-30 14:57:56 +00:00
|
|
|
// We don't want external links that don't start with a registered external URL protocol
|
|
|
|
// (to avoid generating 'javascript:' URLs), so treat it as internal
|
|
|
|
if ( !externalLinkUrlProtocolsRegExp.test( url.toString() ) ) {
|
|
|
|
return returnInternalData( url.toString() );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Strip red link query parameters
|
|
|
|
if ( url.searchParams.get( 'action' ) === 'edit' && url.searchParams.get( 'redlink' ) === '1' ) {
|
|
|
|
url.searchParams.delete( 'action' );
|
|
|
|
url.searchParams.delete( 'redlink' );
|
|
|
|
}
|
|
|
|
// Count remaining query parameters
|
2024-05-21 14:22:56 +00:00
|
|
|
const keys = [];
|
2024-04-30 16:44:25 +00:00
|
|
|
url.searchParams.forEach( ( val, key ) => {
|
2023-01-30 14:57:56 +00:00
|
|
|
keys.push( key );
|
|
|
|
} );
|
2024-05-21 14:22:56 +00:00
|
|
|
const queryLength = keys.length;
|
2023-01-30 14:57:56 +00:00
|
|
|
|
2024-05-21 14:22:56 +00:00
|
|
|
const relativeHref = url.toString().replace( /^https?:/i, '' );
|
2023-01-30 14:57:56 +00:00
|
|
|
// Check if this matches the server's script path (as used by red links)
|
2024-05-21 14:22:56 +00:00
|
|
|
const scriptBase = new URL( mw.config.get( 'wgScript' ), doc.baseURI ).toString().replace( /^https?:/i, '' );
|
2023-01-30 14:57:56 +00:00
|
|
|
if ( relativeHref.indexOf( scriptBase ) === 0 ) {
|
|
|
|
if ( queryLength === 1 && url.searchParams.get( 'title' ) ) {
|
|
|
|
return returnInternalData( url.searchParams.get( 'title' ) + url.hash );
|
2022-03-10 00:02:35 +00:00
|
|
|
}
|
2020-03-20 17:08:24 +00:00
|
|
|
}
|
|
|
|
|
2023-01-30 14:57:56 +00:00
|
|
|
// Check if this matches the server's article path
|
2024-05-21 14:22:56 +00:00
|
|
|
const articleBase = new URL( mw.config.get( 'wgArticlePath' ), doc.baseURI ).toString().replace( /^https?:/i, '' );
|
|
|
|
const articleBaseRegex = new RegExp( regexEscape( articleBase ).replace( regexEscape( '$1' ), '(.*)' ) );
|
|
|
|
const matches = relativeHref.match( articleBaseRegex );
|
2023-01-30 14:57:56 +00:00
|
|
|
if ( matches ) {
|
|
|
|
if ( queryLength === 0 && matches && matches[ 1 ].split( '#' )[ 0 ].indexOf( '?' ) === -1 ) {
|
|
|
|
// Take the relative path
|
|
|
|
return returnInternalData( matches[ 1 ] );
|
|
|
|
}
|
2022-12-14 20:42:15 +00:00
|
|
|
}
|
|
|
|
|
2023-01-30 14:57:56 +00:00
|
|
|
// Doesn't match any of the known URL patterns, or has extra parameters
|
|
|
|
return returnExternalData();
|
2020-03-20 17:08:24 +00:00
|
|
|
};
|
|
|
|
|
2022-12-21 15:49:20 +00:00
|
|
|
/**
|
|
|
|
* Encode a page title into a Parsoid resource name.
|
|
|
|
*
|
|
|
|
* @param {string} title
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
mw.libs.ve.encodeParsoidResourceName = function ( title ) {
|
|
|
|
// Parsoid: Sanitizer::sanitizeTitleURI, Env::makeLink
|
2024-05-21 14:22:56 +00:00
|
|
|
const idx = title.indexOf( '#' );
|
|
|
|
let anchor = null;
|
2022-12-21 15:49:20 +00:00
|
|
|
if ( idx !== -1 ) {
|
|
|
|
anchor = title.slice( idx + 1 );
|
|
|
|
title = title.slice( 0, idx );
|
|
|
|
}
|
2024-05-21 14:22:56 +00:00
|
|
|
let encodedTitle = title.replace( /[%? [\]#|<>]/g, ( match ) => mw.util.wikiUrlencode( match ) );
|
2022-12-21 15:49:20 +00:00
|
|
|
if ( anchor !== null ) {
|
|
|
|
encodedTitle += '#' + mw.util.escapeIdForLink( anchor );
|
|
|
|
}
|
|
|
|
return './' + encodedTitle;
|
|
|
|
};
|
|
|
|
|
2024-04-29 18:14:26 +00:00
|
|
|
/**
|
|
|
|
* @typedef ParsedResource
|
|
|
|
* @memberof mw.libs.ve
|
|
|
|
* @property {string} title Full page title in text form (with namespace, and spaces instead of underscores)
|
|
|
|
*/
|
|
|
|
|
2018-02-28 02:01:05 +00:00
|
|
|
/**
|
|
|
|
* Split Parsoid resource name into the href prefix and the page title.
|
|
|
|
*
|
|
|
|
* @param {string} resourceName Resource name, from a `href` or `resource` attribute
|
2024-04-29 18:14:26 +00:00
|
|
|
* @return {mw.libs.ve.ParsedResource} Parsed resource name
|
2018-02-28 02:01:05 +00:00
|
|
|
*/
|
2020-03-20 17:08:24 +00:00
|
|
|
mw.libs.ve.parseParsoidResourceName = function ( resourceName ) {
|
2018-02-28 02:01:05 +00:00
|
|
|
// Resource names are always prefixed with './' to prevent the MediaWiki namespace from being
|
2019-04-03 19:17:19 +00:00
|
|
|
// interpreted as a URL protocol, consider e.g. 'href="./File:Foo.png"'.
|
2018-02-28 02:01:05 +00:00
|
|
|
// (We accept input without the prefix, so this can also take plain page titles.)
|
2024-05-21 14:22:56 +00:00
|
|
|
const matches = resourceName.match( /^(\.\/|)(.*)$/ );
|
2018-02-28 02:01:05 +00:00
|
|
|
return {
|
|
|
|
// '%' and '?' are valid in page titles, but normally URI-encoded. This also changes underscores
|
|
|
|
// to spaces.
|
2022-12-21 15:49:20 +00:00
|
|
|
title: mw.libs.ve.decodeURIComponentIntoArticleTitle( matches[ 2 ] )
|
2018-02-28 02:01:05 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extract the page title from a Parsoid resource name.
|
|
|
|
*
|
|
|
|
* @param {string} resourceName Resource name, from a `href` or `resource` attribute
|
|
|
|
* @return {string} Full page title in text form (with namespace, and spaces instead of underscores)
|
|
|
|
*/
|
2020-03-20 17:08:24 +00:00
|
|
|
mw.libs.ve.normalizeParsoidResourceName = function ( resourceName ) {
|
|
|
|
return mw.libs.ve.parseParsoidResourceName( resourceName ).title;
|
2018-02-28 02:01:05 +00:00
|
|
|
};
|