Debian Codegraph @

skins/MinervaNeue/resources/skins.minerva.scripts/page-issues/parser.js

raw symbols SCIP
1 /**
2 * @typedef PageIssue
3 * @ignore
4 * @property {string} severity A SEVERITY_LEVEL key.
5 * @property {boolean} grouped True if part of a group of multiple issues, false if singular.
6 * @property {Icon} icon
7 */
8 /**
9 * @typedef {Object} IssueSummary
10 * @ignore
11 * @property {PageIssue} issue
12 * @property {jQuery} $el where the issue was extracted from
13 * @property {string} iconString a string representation of icon.
14 * This is kept for template compatibility (our views do not yet support composition).
15 * @property {string} text HTML string.
16 */
17
18 // Icons are matching the type selector below use a TYPE_* icon. When unmatched, the icon is
19 // chosen by severity. Their color is always determined by severity, too.
20 const ICON_NAME = {
21 // Generic severity icons.
22 SEVERITY: {
23 DEFAULT: 'issue-generic',
24 LOW: 'issue-severity-low',
25 MEDIUM: 'issue-severity-medium',
26 HIGH: 'issue-generic'
27 },
28
29 // Icons customized by type.
30 TYPE: {
31 MOVE: 'issue-type-move',
32 POINT_OF_VIEW: 'issue-type-point-of-view'
33 }
34 };
35 const ICON_COLOR = {
36 DEFAULT: 'defaultColor',
37 LOW: 'lowColor',
38 MEDIUM: 'mediumColor',
39 HIGH: 'highColor'
40 };
41 // How severities order and compare from least to greatest. For the multiple issues
42 // template, severity should be considered the maximum of all its contained issues.
43 const SEVERITY_LEVEL = {
44 DEFAULT: 0,
45 LOW: 1,
46 MEDIUM: 2,
47 HIGH: 3
48 };
49 // Match the template's color CSS selector to a severity level concept. Derived via the
50 // Ambox templates and sub-templates for the top five wikis and tested on page issues
51 // inventory:
52 // - https://people.wikimedia.org/~jdrewniak/page_issues_inventory
53 // - https://en.wikipedia.org/wiki/Template:Ambox
54 // - https://es.wikipedia.org/wiki/Plantilla:Metaplantilla_de_avisos
55 // - https://ja.wikipedia.org/wiki/Template:Ambox
56 // - https://ru.wikipedia.org/wiki/Шаблон:Ambox
57 // - https://it.wikipedia.org/wiki/Template:Avviso
58 // Severity is the class associated with the color. The ResourceLoader config mimics the
59 // idea by using severity for color variants. Severity is determined independently of icons.
60 // These selectors should be migrated to their templates.
61 const SEVERITY_REGEX = {
62 // recommended (T206177), en, it
63 LOW: /mobile-issue-severity-low|ambox-style|avviso-stile/,
64 // recommended, en, it
65 MEDIUM: /mobile-issue-severity-medium|ambox-content|avviso-contenuto/,
66 // recommended, en, en, es / ru, it
67 HIGH: /mobile-issue-severity-high|ambox-speedy|ambox-delete|ambox-serious|avviso-importante/
68 // ..And everything else that doesn't match should be considered DEFAULT.
69 };
70 // As above but used to identify specific templates requiring icon customization.
71 const TYPE_REGEX = {
72 // recommended (opt-in) / en, es / ru, it (long term only recommended should be used)
73 MOVE: /mobile-issue-move|ambox-converted|ambox-move|ambox-merge|avviso-struttura/,
74
75 POINT_OF_VIEW: new RegExp( [
76 // recommended (opt-in)
77 'mobile-issue-pov',
78 // FIXME: en classes: plan to remove these provided can get adoption of recommended
79 'ambox-Advert',
80 'ambox-autobiography',
81 'ambox-believerpov',
82 'ambox-COI',
83 'ambox-coverage',
84 'ambox-criticism',
85 'ambox-fanpov',
86 'ambox-fringe-theories',
87 'ambox-geographical-imbalance',
88 'ambox-globalize',
89 'ambox-npov-language',
90 'ambox-POV',
91 'ambox-pseudo',
92 'ambox-systemic-bias',
93 'ambox-unbalanced',
94 'ambox-usgovtpov'
95 ].join( '|' ) )
96 // ..And everything else that doesn't match is mapped to a "SEVERITY" type.
97 };
98 const GROUPED_PARENT_REGEX = /mw-collapsible-content/;
99 // Variants supported by specific types. The "severity icon" supports all severities but the
100 // type icons only support one each by ResourceLoader.
101 const TYPE_SEVERITY = {
102 MOVE: 'DEFAULT',
103 POINT_OF_VIEW: 'MEDIUM'
104 };
105
106 /**
107 * @param {Element} box
108 * @return {string} An SEVERITY_SELECTOR key.
109 * @private
110 */
111 function parseSeverity( box ) {
112 let severity;
113 const identified = Object.keys( SEVERITY_REGEX ).some( ( key ) => {
114 const regex = SEVERITY_REGEX[ key ];
115 severity = key;
116 return regex.test( box.className );
117 } );
118 return identified ? severity : 'DEFAULT';
119 }
120
121 /**
122 * @param {Element} box
123 * @param {string} severity An SEVERITY_LEVEL key.
124 * @return {{name: string, severity: string}} An ICON_NAME.
125 * @private
126 */
127 function parseType( box, severity ) {
128 let identifiedType;
129 const identified = Object.keys( TYPE_REGEX ).some( ( type ) => {
130 const regex = TYPE_REGEX[ type ];
131 identifiedType = type;
132 return regex.test( box.className );
133 } );
134 return {
135 name: identified ? ICON_NAME.TYPE[ identifiedType ] : ICON_NAME.SEVERITY[ severity ],
136 severity: identified ? TYPE_SEVERITY[ identifiedType ] : severity
137 };
138 }
139
140 /**
141 * @param {Element} box
142 * @return {boolean} True if part of a group of multiple issues, false if singular.
143 * @private
144 */
145 function parseGroup( box ) {
146 return !!box.parentNode && GROUPED_PARENT_REGEX.test( box.parentNode.className );
147 }
148
149 /**
150 * @ignore
151 * @param {Element} box
152 * @param {string} severity An SEVERITY_LEVEL key.
153 * @return {string} A severity or type ISSUE_ICON.
154 */
155 function iconName( box, severity ) {
156 const nameSeverity = parseType( box, severity );
157 // The icon with color variant as expected by ResourceLoader,
158 // {iconName}-{severityColorVariant}.
159 return nameSeverity.name + '-' + ICON_COLOR[ nameSeverity.severity ];
160 }
161
162 /**
163 * @ignore
164 * @param {string[]} severityLevels an array of SEVERITY_KEY values.
165 * @return {string} The greatest SEVERITY_LEVEL key.
166 */
167 function maxSeverity( severityLevels ) {
168 return severityLevels.reduce( ( max, severity ) => SEVERITY_LEVEL[ max ] > SEVERITY_LEVEL[ severity ] ? max : severity, 'DEFAULT' );
169 }
170
171 /**
172 * @ignore
173 * @param {Element} box
174 * @return {PageIssue}
175 */
176 function parse( box ) {
177 const severity = parseSeverity( box );
178 const iconElement = document.createElement( 'div' );
179 iconElement.classList.add( `minerva-icon--${ iconName( box, severity ) }`, 'minerva-ambox-icon' );
180 return {
181 severity,
182 grouped: parseGroup( box ),
183 iconElement
184 };
185 }
186
187 /**
188 * Extract a summary message from a cleanup template generated element that is
189 * friendly for mobile display.
190 *
191 * @ignore
192 * @param {Object} $box element to extract the message from
193 * @return {IssueSummary}
194 */
195 function extract( $box ) {
196 const SELECTOR = '.mbox-text, .ambox-text';
197 const $container = $( '<div>' );
198
199 $box.find( SELECTOR ).each( ( _i, el ) => {
200 const $el = $( el );
201 // Clean up talk page boxes
202 $el.find( 'table, .noprint' ).remove();
203 const contents = $el.html();
204
205 if ( contents ) {
206 $( '<p>' ).html( contents ).appendTo( $container );
207 }
208 } );
209
210 const pageIssue = parse( $box.get( 0 ) );
211
212 return {
213 issue: pageIssue,
214 $el: $box,
215 text: $container.html()
216 };
217 }
218
219 module.exports = {
220 extract,
221 parse,
222 maxSeverity,
223 iconName,
224 test: {
225 parseSeverity,
226 parseType,
227 parseGroup
228 }
229 };