Index: trunk/extensions/CSS/CSS.php |
— | — | @@ -1,70 +1,63 @@ |
2 | 2 | <?php |
3 | 3 | /** |
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. |
5 | 6 | * |
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. |
7 | 9 | * |
8 | 10 | * @file |
9 | 11 | * @ingroup Extensions |
| 12 | + * @version 2.0 |
10 | 13 | * @author Aran Dunkley [http://www.organicdesign.co.nz/nad User:Nad] |
| 14 | + * @author Rusty Burchfield |
11 | 15 | * @copyright © 2007-2010 Aran Dunkley |
| 16 | + * @copyright © 2011 Rusty Burchfield |
12 | 17 | * @licence GNU General Public Licence 2.0 or later |
13 | 18 | */ |
14 | 19 | |
15 | 20 | if ( !defined( 'MEDIAWIKI') ) die('Not an entry point.' ); |
16 | 21 | |
17 | | -define( 'CSS_VERSION', '1.0.7, 2010-10-20' ); |
| 22 | +define( 'CSS_VERSION', '2.0, 2011-10-27' ); |
18 | 23 | |
19 | | -$wgCSSMagic = "css"; |
| 24 | +$wgCSSMagic = 'css'; |
| 25 | +$wgCSSPath = false; |
| 26 | + |
20 | 27 | $wgHooks['ParserFirstCallInit'][] = 'wfCSSParserFirstCallInit'; |
21 | 28 | $wgHooks['LanguageGetMagic'][] = 'wfCSSLanguageGetMagic'; |
22 | 29 | |
23 | 30 | $wgExtensionCredits['parserhook'][] = array( |
24 | 31 | 'path' => __FILE__, |
25 | 32 | 'name' => 'CSS', |
26 | | - 'author' => '[http://www.organicdesign.co.nz/nad User:Nad]', |
| 33 | + 'author' => array ( 'Aran Dunkley', 'Rusty Burchfield' ), |
27 | 34 | 'descriptionmsg' => 'css-desc', |
28 | 35 | 'url' => 'http://www.mediawiki.org/wiki/Extension:CSS', |
29 | 36 | 'version' => CSS_VERSION, |
30 | 37 | ); |
31 | 38 | |
32 | | -$dir = dirname( __FILE__ ) . '/'; |
33 | | -$wgExtensionMessagesFiles['CSS'] = $dir . 'CSS.i18n.php'; |
| 39 | +$wgExtensionMessagesFiles['CSS'] = dirname( __FILE__ ) . '/' . 'CSS.i18n.php'; |
34 | 40 | |
35 | 41 | function wfCSSRender( &$parser, $css ) { |
36 | | - global $wgOut, $wgRequest; |
| 42 | + global $wgCSSPath, $wgScriptPath; |
37 | 43 | |
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 ); |
52 | 50 | } 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 ); |
57 | 54 | } 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( "&", "&", $url ); |
64 | | - } |
| 55 | + # Inline CSS |
| 56 | + $headItem = HTML::inlineStyle( Sanitizer::checkCss( $css ) ); |
65 | 57 | } |
66 | 58 | |
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 ); |
69 | 62 | } |
70 | 63 | |
71 | 64 | function wfCSSParserFirstCallInit( $parser ) { |