Debian Codegraph @

vendor/wikimedia/parsoid/tests/MockEnv.js

raw
1 'use strict';
2
3 const { DOMDataUtils } = require('../lib/utils/DOMDataUtils.js');
4 const { DOMUtils } = require('../lib/utils/DOMUtils.js');
5
6 class MockEnv {
7 constructor(argv, pageSrc) {
8 // Hack in bswPagePropRegexp to support Util.js function "isBehaviorSwitch: function(... "
9 const bswRegexpSource = "\\/(?:NOGLOBAL|DISAMBIG|NOCOLLABORATIONHUBTOC|nocollaborationhubtoc|NOTOC|notoc|NOGALLERY|nogallery|FORCETOC|forcetoc|TOC|toc|NOEDITSECTION|noeditsection|NOTITLECONVERT|notitleconvert|NOTC|notc|NOCONTENTCONVERT|nocontentconvert|NOCC|nocc|NEWSECTIONLINK|NONEWSECTIONLINK|HIDDENCAT|INDEX|NOINDEX|STATICREDIRECT)";
10 this.page = {
11 src: pageSrc || "testing testing testing testing",
12 };
13 this.conf = {
14 wiki: {
15 bswPagePropRegexp: new RegExp(
16 '(?:^|\\s)mw:PageProp/' + bswRegexpSource + '(?=$|\\s)'
17 ),
18 magicWordCanonicalName: function() {
19 return "toc";
20 } // mock function returns string for BehaviorSwitchHandler
21 },
22 parsoid: {
23 debug: argv.debug,
24 },
25 };
26 this.log = argv.log ? this._log : this._emptyLog;
27 this.wrapSections = true; // always wrap sections!
28 }
29
30 _emptyLog() {}
31
32 _log() {
33 let output = arguments[0];
34 for (let i = 1; i < arguments.length; i++) {
35 if (typeof arguments[i] === 'function') {
36 output = output + ' ' + arguments[i]();
37 } else {
38 output = output + ' ' + arguments[i];
39 }
40 }
41 console.log(output);
42 }
43
44 // NOTE: Here's the spot to stuff references to $doc in the PHP port.
45 referenceDataObject(doc, bag) {
46 DOMDataUtils.setDocBag(doc, bag);
47 }
48
49 // NOTE: This a potential gotcha when it comes to the port.
50 // Much like `env.createDocument()`, a reference to $doc is going to have to
51 // be held onto so that the attached environment doesn't get GC'd.
52 createDocument(html) {
53 const doc = DOMUtils.parseHTML(html);
54 this.referenceDataObject(doc);
55 return doc;
56 }
57 }
58
59 if (typeof module === "object") {
60 module.exports.MockEnv = MockEnv;
61 }