| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
const ICON_NAME = { |
| 21 |
|
| 22 |
SEVERITY: { |
| 23 |
DEFAULT: 'issue-generic', |
| 24 |
LOW: 'issue-severity-low', |
| 25 |
MEDIUM: 'issue-severity-medium', |
| 26 |
HIGH: 'issue-generic' |
| 27 |
}, |
| 28 |
|
| 29 |
|
| 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 |
|
| 42 |
|
| 43 |
const SEVERITY_LEVEL = { |
| 44 |
DEFAULT: 0, |
| 45 |
LOW: 1, |
| 46 |
MEDIUM: 2, |
| 47 |
HIGH: 3 |
| 48 |
}; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
:Ambox |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
const SEVERITY_REGEX = { |
| 62 |
|
| 63 |
LOW: /mobile-issue-severity-low|ambox-style|avviso-stile/, |
| 64 |
|
| 65 |
MEDIUM: /mobile-issue-severity-medium|ambox-content|avviso-contenuto/, |
| 66 |
|
| 67 |
HIGH: /mobile-issue-severity-high|ambox-speedy|ambox-delete|ambox-serious|avviso-importante/ |
| 68 |
|
| 69 |
}; |
| 70 |
|
| 71 |
const TYPE_REGEX = { |
| 72 |
|
| 73 |
MOVE: /mobile-issue-move|ambox-converted|ambox-move|ambox-merge|avviso-struttura/, |
| 74 |
|
| 75 |
POINT_OF_VIEW: new RegExp( [ |
| 76 |
|
| 77 |
'mobile-issue-pov', |
| 78 |
|
| 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 |
|
| 97 |
}; |
| 98 |
const GROUPED_PARENT_REGEX = /mw-collapsible-content/; |
| 99 |
|
| 100 |
|
| 101 |
const TYPE_SEVERITY = { |
| 102 |
MOVE: 'DEFAULT', |
| 103 |
POINT_OF_VIEW: 'MEDIUM' |
| 104 |
}; |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 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 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 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 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
function parseGroup( box ) { |
| 146 |
return !!box.parentNode && GROUPED_PARENT_REGEX.test( box.parentNode.className ); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
function iconName( box, severity ) { |
| 156 |
const nameSeverity = parseType( box, severity ); |
| 157 |
|
| 158 |
|
| 159 |
return nameSeverity.name + '-' + ICON_COLOR[ nameSeverity.severity ]; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
function maxSeverity( severityLevels ) { |
| 168 |
return severityLevels.reduce( ( max, severity ) => SEVERITY_LEVEL[ max ] > SEVERITY_LEVEL[ severity ] ? max : severity, 'DEFAULT' ); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 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 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 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 |
|
| 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 |
}; |