Emit CDATA more intelligently
authorAryeh Gregor <simetrical@users.mediawiki.org>
Sun, 23 Aug 2009 21:06:54 +0000 (21:06 +0000)
committerAryeh Gregor <simetrical@users.mediawiki.org>
Sun, 23 Aug 2009 21:06:54 +0000 (21:06 +0000)
This fixes some possible XML invalidity from r54767: CDATA stuff was
being added only if $wgHtml5 was false, instead of whenever
$wgWellFormedXml is true.  Also, it uses CDATA for script as well as
style, but in both cases only uses it if there's a & or < somewhere.

includes/HTMLForm.php
includes/Html.php

index 59429c9..35fb020 100644 (file)
@@ -1,7 +1,6 @@
 <?php
 
 class HTMLForm {
-
        static $jsAdded = false;
 
        /* The descriptor is an array of arrays.
index 1c762b6..62402f5 100644 (file)
@@ -256,11 +256,13 @@ class Html {
         * @return string Raw HTML
         */
        public static function inlineScript( $contents ) {
-               global $wgHtml5, $wgJsMimeType;
+               global $wgHtml5, $wgJsMimeType, $wgWellFormedXml;
 
                $attrs = array();
                if ( !$wgHtml5 ) {
                        $attrs['type'] = $wgJsMimeType;
+               }
+               if ( $wgWellFormedXml && preg_match( '/[<&]/', $contents ) ) {
                        $contents = "/*<![CDATA[*/$contents/*]]>*/";
                }
                return self::rawElement( 'script', $attrs, $contents );
@@ -294,15 +296,15 @@ class Html {
         * @return string Raw HTML
         */
        public static function inlineStyle( $contents, $media = null ) {
-               global $wgHtml5;
+               global $wgHtml5, $wgWellFormedXml;
 
                $attrs = array();
                if ( !$wgHtml5 ) {
-                       # Technically we should probably add CDATA stuff here like with
-                       # scripts, but in practice, stylesheets tend not to have
-                       # problematic characters anyway.
                        $attrs['type'] = 'text/css';
                }
+               if ( $wgWellFormedXml && preg_match( '/[<&]/', $contents ) ) {
+                       $contents = "/*<![CDATA[*/$contents/*]]>*/";
+               }
                if ( $media !== null ) {
                        $attrs['media'] = $media;
                }