From 0566a495f3029b5d968520d28cfa8efcd43891f2 Mon Sep 17 00:00:00 2001 From: Adam Wight Date: Tue, 6 Feb 2024 17:01:13 +0100 Subject: [PATCH] Accessors to find MWReferenceNode in the document These are provides as a replacement for internalList indexes. Bug: T336417 Change-Id: Ifd3a1b667369074e2cefa4d70e4c090e91b010b5 --- modules/ve-cite/ve.dm.MWReferenceModel.js | 65 +++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/modules/ve-cite/ve.dm.MWReferenceModel.js b/modules/ve-cite/ve.dm.MWReferenceModel.js index 9ae0e69df..f22a9737f 100644 --- a/modules/ve-cite/ve.dm.MWReferenceModel.js +++ b/modules/ve-cite/ve.dm.MWReferenceModel.js @@ -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.} 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 */ /**