MediaWiki:Gadget-CSSValidate.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
// CSS validator
// Author: [[User:Kwj2772]] 
// License: Creative Commons Attribution-ShareAlike (BY-SA) 3.0 

if (
    // Only load on User/MediaWiki:*.css
    mw.config.get('wgTitle').match(/\.css/i)
    && (mw.config.get('wgNamespaceNumber') == 2 || mw.config.get('wgNamespaceNumber') == 8)
    // Support Safari <= 5, IE <= 11: Gracefully ignore
    && typeof URL !== 'undefined'
) {
  $(function () {
    // when there isn't a #ca-validatecss already
    if ($('#ca-validatecss').length) {
        return;
    }

    var validator = 'http://jigsaw.w3.org/css-validator/validator?&usermedium=all&warning=1&lang=en&uri='
    var uri = mw.util.getUrl( null, { action: 'raw', ctype: 'text/css' });
    // expand, since uri may be like "/w/..." or "//commons.wikimedia.org/w/...".
    // We need an absolute url with protocol and hostname.
    var urlObj = new URL(uri, location.href);
    var fullUri = urlObj.href;
    validator = validator + encodeURIComponent( fullUri ); 
    mw.util.addPortletLink('p-cactions', validator, 'Validate CSS', 'ca-validatecss', 'Validate CSS', 'v');
  });
}