Accessors to find MWReferenceNode in the document

These are provides as a replacement for internalList indexes.

Bug: T336417
Change-Id: Ifd3a1b667369074e2cefa4d70e4c090e91b010b5
This commit is contained in:
Adam Wight 2024-02-06 17:01:13 +01:00
parent 74f66bc478
commit 0566a495f3

View file

@ -62,6 +62,71 @@ ve.dm.MWReferenceModel.static.newFromReferenceNode = function ( node ) {
return ref;
};
/**
* Scan through a document and return all reference nodes (footnote markers)
*
* This is part of the migration away from internalList, the functions derived
* from this make it possible to locate refs within a document without relying
* on a separate source of truth. See T356860.
*
* @param {ve.dm.Document} document Document to scan for reference nodes
*
* @return {ve.dm.MWReferenceNode[]} List of reference nodes in document order.
*/
ve.dm.MWReferenceModel.static.getDocumentRefs = function ( document ) {
return document.getNodesByType( 'mwReference' )
.filter( ( refNode ) =>
!refNode.findParent( ve.dm.MWReferencesListNode ) &&
!refNode.getAttribute( 'placeholder' )
);
};
/**
* Return all document refs, organized by ref group
*
* @param {ve.dm.Document} document Document to scan for reference nodes
*
* @return {Object.<string,ve.dm.MWReferenceNode[]>} Lists of ref nodes keyed by
* group. Keys take the long form like 'mwReference/groupName'.
*/
ve.dm.MWReferenceModel.static.getGroupedRefs = function ( document ) {
return this.constructor.static.getDocumentRefs( document )
// Object.groupBy
.reduce( ( groups, node ) => {
const group = node.getAttribute( 'listGroup' );
groups[ group ] = ( groups[ group ] || [] ).concat( [ node ] );
return groups;
}, {} );
};
/**
* Return all refs belonging to the given group
*
* @param {ve.dm.Document} document Document to scan for reference nodes
* @param {string} group Group in the long form like 'mwReference/groupName'.
* @return {ve.dm.MWReferenceNode[]} List of ref nodes
*/
ve.dm.MWReferenceModel.static.getRefsForGroup = function ( document, group ) {
return this.constructor.static.getDocumentRefs( document )
.filter( ( refNode ) => refNode.getAttribute( 'listGroup' ) === group );
};
/**
* Return all refs matching the given group and key
*
* Multiple matching refs may be returned in the case of reused references.
*
* @param {ve.dm.Document} document Document to scan for reference nodes
* @param {string} group Group in the long form like 'mwReference/groupName'.
* @param {string} key Reference key using the internal form like 'auto/2' or 'literal/refName'.
*
* @return {ve.dm.MWReferenceNode[]} List of ref nodes
*/
ve.dm.MWReferenceModel.static.getRefsForKey = function ( document, group, key ) {
return this.constructor.static.getRefsForGroup( document, group )
.filter( ( refNode ) => refNode.getAttribute( 'listKey' ) === key );
};
/* Methods */
/**