Debian Codegraph @

extensions/DiscussionTools/tests/qunit/testUtils.js

raw symbols SCIP
1 const utils = require( 'ext.discussionTools.init' ).utils;
2
3 module.exports = {};
4
5 /**
6 * Override mw.config with the given data. Used for testing different languages etc.
7 * (Automatically restored after every test by QUnit.newMwEnvironment.)
8 *
9 * @param {Object} config
10 */
11 module.exports.overrideMwConfig = function ( config ) {
12 Object.assign(
13 mw.config.values,
14 config
15 );
16 };
17
18 /**
19 * Return the node that is expected to contain thread items.
20 *
21 * @param {Document} doc
22 * @return {Element}
23 */
24 module.exports.getThreadContainer = function ( doc ) {
25 // In tests created from Parsoid output, comments are contained directly in <body>.
26 // In tests created from old parser output, comments are contained in <div class="mw-parser-output">.
27 const body = doc.body;
28 const wrapper = body.querySelector( 'div.mw-parser-output' );
29 return wrapper || body;
30 };
31
32 /**
33 * Get the offset path from ancestor to offset in descendant
34 *
35 * @param {Node} ancestor The ancestor node
36 * @param {Node} node The descendant node
37 * @param {number} nodeOffset The offset in the descendant node
38 * @return {string} The offset path
39 */
40 function getOffsetPath( ancestor, node, nodeOffset ) {
41 const path = [ nodeOffset ];
42 while ( node !== ancestor ) {
43 if ( node.parentNode === null ) {
44 // eslint-disable-next-line no-console
45 console.log( node, 'is not a descendant of', ancestor );
46 throw new Error( 'Not a descendant' );
47 }
48 path.unshift( utils.childIndexOf( node ) );
49 node = node.parentNode;
50 }
51 return path.join( '/' );
52 }
53
54 function getPathsFromRange( root, range ) {
55 return [
56 getOffsetPath( root, range.startContainer, range.startOffset ),
57 getOffsetPath( root, range.endContainer, range.endOffset )
58 ];
59 }
60
61 /**
62 * Massage comment data to make it serializable as JSON.
63 *
64 * @param {CommentItem} parent Comment item; modified in-place
65 * @param {Node} root Ancestor node of all comments
66 */
67 module.exports.serializeComments = function ( parent, root ) {
68 if ( !parent.range.startContainer ) {
69 // Already done as part of a different thread
70 return;
71 }
72
73 // Can't serialize circular structures to JSON
74 delete parent.parent;
75
76 // Can't serialize the DOM nodes involved in the range,
77 // instead use their offsets within their parent nodes
78 parent.range = getPathsFromRange( root, parent.range );
79 if ( parent.signatureRanges ) {
80 parent.signatureRanges = parent.signatureRanges.map( ( range ) => getPathsFromRange( root, range ) );
81 }
82 if ( parent.timestampRanges ) {
83 parent.timestampRanges = parent.timestampRanges.map( ( range ) => getPathsFromRange( root, range ) );
84 }
85 if ( parent.timestamp ) {
86 parent.timestamp = parent.getTimestampString();
87 }
88 if ( !parent.displayName ) {
89 delete parent.displayName;
90 }
91
92 // Unimportant
93 delete parent.rootNode;
94
95 // Ignore generated properties
96 delete parent.authors;
97 delete parent.commentCount;
98 delete parent.oldestReply;
99 delete parent.latestReply;
100
101 parent.replies.forEach( ( comment ) => {
102 module.exports.serializeComments( comment, root );
103 } );
104 };