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
|
|
|
var utils = require( 'ext.discussionTools.init' ).utils;
|
2021-01-08 19:20:33 +00:00
|
|
|
|
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
|
|
|
QUnit.module( 'mw.dt.utils', QUnit.newMwEnvironment() );
|
2021-01-08 19:20:33 +00:00
|
|
|
|
2024-04-18 18:37:58 +00:00
|
|
|
QUnit.test( '#linearWalk', ( assert ) => {
|
2021-01-08 19:20:33 +00:00
|
|
|
var cases = require( '../cases/linearWalk.json' );
|
|
|
|
|
2024-04-18 18:37:58 +00:00
|
|
|
cases.forEach( ( caseItem ) => {
|
2021-01-08 19:20:33 +00:00
|
|
|
var
|
2022-10-12 20:03:23 +00:00
|
|
|
doc = ve.createDocumentFromHtml( require( '../' + caseItem.dom ) ),
|
2021-04-08 13:46:09 +00:00
|
|
|
expected = require( caseItem.expected );
|
2021-01-08 19:20:33 +00:00
|
|
|
|
2021-04-08 13:46:09 +00:00
|
|
|
var actual = [];
|
2024-04-18 18:37:58 +00:00
|
|
|
utils.linearWalk( doc, ( event, node ) => {
|
2021-01-08 19:20:33 +00:00
|
|
|
actual.push( event + ' ' + node.nodeName.toLowerCase() + '(' + node.nodeType + ')' );
|
|
|
|
} );
|
|
|
|
|
2021-04-08 13:46:09 +00:00
|
|
|
var actualBackwards = [];
|
2024-04-18 18:37:58 +00:00
|
|
|
utils.linearWalkBackwards( doc, ( event, node ) => {
|
2021-01-26 18:58:58 +00:00
|
|
|
actualBackwards.push( event + ' ' + node.nodeName.toLowerCase() + '(' + node.nodeType + ')' );
|
|
|
|
} );
|
|
|
|
|
2021-01-08 19:20:33 +00:00
|
|
|
assert.deepEqual( actual, expected, caseItem.name );
|
|
|
|
|
2024-04-18 18:37:58 +00:00
|
|
|
var expectedBackwards = expected.slice().reverse().map( ( a ) => {
|
2021-11-08 19:03:40 +00:00
|
|
|
return ( a.slice( 0, 5 ) === 'enter' ? 'leave' : 'enter' ) + a.slice( 5 );
|
2021-01-26 18:58:58 +00:00
|
|
|
} );
|
|
|
|
assert.deepEqual( actualBackwards, expectedBackwards, caseItem.name + ' (backwards)' );
|
|
|
|
|
2021-01-08 19:20:33 +00:00
|
|
|
// Uncomment this to get updated content for the JSON files, for copy/paste:
|
|
|
|
// console.log( JSON.stringify( actual, null, 2 ) );
|
|
|
|
} );
|
|
|
|
} );
|