r101141 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r101140‎ | r101141 | r101142 >
Date:15:32, 28 October 2011
Author:johnduhart
Status:ok (Comments)
Tags:
Comment:
Committing patch by Rusty Burchfield

Bug 31996 - CSS extension cleanup
Modified paths:
  • /trunk/extensions/CSS/CSS.php (modified) (history)

Diff [purge]

Index: trunk/extensions/CSS/CSS.php
@@ -1,70 +1,63 @@
22 <?php
33 /**
4 - * CSS extension - A parser-function for adding CSS files, article or inline rules to articles
 4+ * CSS extension - A parser-function for adding CSS to articles via file,
 5+ * article or inline rules.
56 *
6 - * See http://www.mediawiki.org/wiki/Extension:CSS for installation and usage details
 7+ * See http://www.mediawiki.org/wiki/Extension:CSS for installation and usage
 8+ * details.
79 *
810 * @file
911 * @ingroup Extensions
 12+ * @version 2.0
1013 * @author Aran Dunkley [http://www.organicdesign.co.nz/nad User:Nad]
 14+ * @author Rusty Burchfield
1115 * @copyright © 2007-2010 Aran Dunkley
 16+ * @copyright © 2011 Rusty Burchfield
1217 * @licence GNU General Public Licence 2.0 or later
1318 */
1419
1520 if ( !defined( 'MEDIAWIKI') ) die('Not an entry point.' );
1621
17 -define( 'CSS_VERSION', '1.0.7, 2010-10-20' );
 22+define( 'CSS_VERSION', '2.0, 2011-10-27' );
1823
19 -$wgCSSMagic = "css";
 24+$wgCSSMagic = 'css';
 25+$wgCSSPath = false;
 26+
2027 $wgHooks['ParserFirstCallInit'][] = 'wfCSSParserFirstCallInit';
2128 $wgHooks['LanguageGetMagic'][] = 'wfCSSLanguageGetMagic';
2229
2330 $wgExtensionCredits['parserhook'][] = array(
2431 'path' => __FILE__,
2532 'name' => 'CSS',
26 - 'author' => '[http://www.organicdesign.co.nz/nad User:Nad]',
 33+ 'author' => array ( 'Aran Dunkley', 'Rusty Burchfield' ),
2734 'descriptionmsg' => 'css-desc',
2835 'url' => 'http://www.mediawiki.org/wiki/Extension:CSS',
2936 'version' => CSS_VERSION,
3037 );
3138
32 -$dir = dirname( __FILE__ ) . '/';
33 -$wgExtensionMessagesFiles['CSS'] = $dir . 'CSS.i18n.php';
 39+$wgExtensionMessagesFiles['CSS'] = dirname( __FILE__ ) . '/' . 'CSS.i18n.php';
3440
3541 function wfCSSRender( &$parser, $css ) {
36 - global $wgOut, $wgRequest;
 42+ global $wgCSSPath, $wgScriptPath;
3743
38 - $parser->mOutput->mCacheTime = -1;
39 - $url = false;
40 - if( preg_match( '|\\{|', $css ) ) {
41 -
42 - # Inline CSS
43 - $css = htmlspecialchars( trim( Sanitizer::checkCss( $css ) ) );
44 - $parser->mOutput->addHeadItem( <<<EOT
45 -<style type="text/css">
46 -/*<![CDATA[*/
47 -{$css}
48 -/*]]>*/
49 -</style>
50 -EOT
51 - );
 44+ $css = trim( $css );
 45+ $title = Title::newFromText( $css );
 46+ if ( is_object( $title ) && $title->isKnown() ) {
 47+ # "blue link" Article
 48+ $url = $title->getLocalURL( 'action=raw&ctype=text/css' );
 49+ $headItem = HTML::linkedStyle( $url );
5250 } elseif ( $css{0} == '/' ) {
53 -
54 - # File
55 - $url = $css;
56 -
 51+ # Regular file
 52+ $base = $wgCSSPath === false ? $wgScriptPath : $wgCSSPath;
 53+ $headItem = HTML::linkedStyle( $base . $css );
5754 } else {
58 -
59 - # Article?
60 - $title = Title::newFromText( $css );
61 - if( is_object( $title ) ) {
62 - $url = $title->getLocalURL( 'action=raw&ctype=text/css' );
63 - $url = str_replace( "&", "&amp;", $url );
64 - }
 55+ # Inline CSS
 56+ $headItem = HTML::inlineStyle( Sanitizer::checkCss( $css ) );
6557 }
6658
67 - if( $url ) $wgOut->addScript( "<link rel=\"stylesheet\" type=\"text/css\" href=\"$url\" />" );
68 - return '';
 59+ $headItem = FormatJson::encode( $headItem );
 60+ $script = HTML::inlineScript( "$('head').append( $headItem );" );
 61+ return array( $script, 'isHTML' => true );
6962 }
7063
7164 function wfCSSParserFirstCallInit( $parser ) {

Follow-up revisions

RevisionCommit summaryAuthorDate
r101562Followup r101141, fix depcreated syntaxjohnduhart00:15, 2 November 2011

Comments

#Comment by 😂 (talk | contribs)   00:10, 2 November 2011

Minor nitpick. Don't use curly braces for string index access, they've been deprecated. Just use square brackets instead.

#Comment by Saper (talk | contribs)   20:01, 5 February 2013

After this I get an exception (.cssExtensionDataURIFallback undefined) and JS stops working. Custom CSS from this exception works, though.

I have file a quick fix in the Gerrit change https://gerrit.wikimedia.org/r/#/c/47614/

#Comment by Saper (talk | contribs)   20:06, 5 February 2013

I meant "Custom CSS from this extension works"

Status & tagging log