2021-04-08 13:46:09 +00:00
|
|
|
var utils = require( 'ext.discussionTools.init' ).utils;
|
2020-05-22 17:09:21 +00:00
|
|
|
|
2019-10-24 11:41:25 +00:00
|
|
|
module.exports = {};
|
|
|
|
|
2019-10-24 12:59:42 +00:00
|
|
|
/**
|
|
|
|
* Override mw.config with the given data. Used for testing different languages etc.
|
Change CommentParser into a service
Goal:
-----
To have a method like CommentParser::parse(), which just takes a node
to parse and a title and returns plain data, so that we don't need to
keep track of the config to construct a CommentParser object (the
required config like content language is provided by services) and
we don't need to keep that object around after parsing.
Changes:
--------
CommentParser.php:
* …is now a service. Constructor only takes services as arguments.
The node and title are passed to a new parse() method.
* parse() should return plain data, but I split this part to a separate
patch for ease of review: I49bfe019aa460651447fd383f73eafa9d7180a92.
* CommentParser still cheats and accesses global state in a few places,
e.g. calling Title::makeTitleSafe or CommentUtils::getTitleFromUrl,
so we can't turn its tests into true unit tests. This work is left
for future commits.
LanguageData.php:
* …is now a service, instead of a static class.
Parser.js:
* …is not a real service, but it's changed to behave in a similar way.
Constructor takes only the required config as argument,
and node and title are instead passed to a new parse() method.
CommentParserTest.php:
parser.test.js:
* Can be simplified, now that we don't need a useless node and title
to test internal methods that don't use them.
testUtils.js:
* Can be simplified, now that we don't need to override internal
ResourceLoader stuff just to change the parser config.
Change-Id: Iadb7757debe000025e52770ca51ebcf24ca8ee66
2022-02-19 02:43:21 +00:00
|
|
|
* (Automatically restored after every test by QUnit.newMwEnvironment.)
|
2019-10-24 12:59:42 +00:00
|
|
|
*
|
|
|
|
* @param {Object} config
|
|
|
|
*/
|
|
|
|
module.exports.overrideMwConfig = function ( config ) {
|
|
|
|
$.extend(
|
|
|
|
mw.config.values,
|
|
|
|
config
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the offset path from ancestor to offset in descendant
|
|
|
|
*
|
|
|
|
* @copyright 2011-2019 VisualEditor Team and others; see http://ve.mit-license.org
|
|
|
|
*
|
|
|
|
* @param {Node} ancestor The ancestor node
|
|
|
|
* @param {Node} node The descendant node
|
|
|
|
* @param {number} nodeOffset The offset in the descendant node
|
|
|
|
* @return {number[]} The offset path
|
|
|
|
*/
|
|
|
|
function getOffsetPath( ancestor, node, nodeOffset ) {
|
|
|
|
var path = [ nodeOffset ];
|
|
|
|
while ( node !== ancestor ) {
|
|
|
|
if ( node.parentNode === null ) {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.log( node, 'is not a descendant of', ancestor );
|
|
|
|
throw new Error( 'Not a descendant' );
|
|
|
|
}
|
2020-05-22 17:09:21 +00:00
|
|
|
path.unshift( utils.childIndexOf( node ) );
|
2019-10-24 12:59:42 +00:00
|
|
|
node = node.parentNode;
|
|
|
|
}
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Massage comment data to make it serializable as JSON.
|
|
|
|
*
|
2020-07-19 19:47:44 +00:00
|
|
|
* @param {CommentItem} parent Comment item; modified in-place
|
2019-10-24 12:59:42 +00:00
|
|
|
* @param {Node} root Ancestor node of all comments
|
|
|
|
*/
|
|
|
|
module.exports.serializeComments = function ( parent, root ) {
|
2020-10-01 19:36:11 +00:00
|
|
|
if ( !parent.range.startContainer ) {
|
|
|
|
// Already done as part of a different thread
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-24 12:59:42 +00:00
|
|
|
// Can't serialize circular structures to JSON
|
|
|
|
delete parent.parent;
|
|
|
|
|
|
|
|
// Can't serialize the DOM nodes involved in the range,
|
|
|
|
// instead use their offsets within their parent nodes
|
|
|
|
parent.range = [
|
|
|
|
getOffsetPath( root, parent.range.startContainer, parent.range.startOffset ).join( '/' ),
|
|
|
|
getOffsetPath( root, parent.range.endContainer, parent.range.endOffset ).join( '/' )
|
|
|
|
];
|
2020-03-04 16:14:47 +00:00
|
|
|
if ( parent.signatureRanges ) {
|
|
|
|
parent.signatureRanges = parent.signatureRanges.map( function ( range ) {
|
|
|
|
return [
|
|
|
|
getOffsetPath( root, range.startContainer, range.startOffset ).join( '/' ),
|
|
|
|
getOffsetPath( root, range.endContainer, range.endOffset ).join( '/' )
|
|
|
|
];
|
|
|
|
} );
|
|
|
|
}
|
2019-10-24 12:59:42 +00:00
|
|
|
|
2020-07-29 23:57:51 +00:00
|
|
|
// Unimportant
|
|
|
|
delete parent.rootNode;
|
|
|
|
|
2022-02-19 06:31:34 +00:00
|
|
|
delete parent.legacyId;
|
|
|
|
|
2019-10-24 12:59:42 +00:00
|
|
|
parent.replies.forEach( function ( comment ) {
|
|
|
|
module.exports.serializeComments( comment, root );
|
|
|
|
} );
|
|
|
|
};
|