Make CodeSniffer checks pass on includes/specialpages/
[lhc/web/wiklou.git] / includes / HtmlFormatter.php
index 5f97140..c06fbbc 100644 (file)
@@ -65,13 +65,19 @@ class HtmlFormatter {
                if ( !$this->doc ) {
                        $html = mb_convert_encoding( $this->html, 'HTML-ENTITIES', 'UTF-8' );
 
+                       // Workaround for bug that caused spaces before references
+                       // to disappear during processing:
                        // https://bugzilla.wikimedia.org/show_bug.cgi?id=53086
+                       //
+                       // Please replace with a better fix if one can be found.
                        $html = str_replace( ' <', '&#32;<', $html );
 
                        libxml_use_internal_errors( true );
+                       $loader = libxml_disable_entity_loader();
                        $this->doc = new DOMDocument();
                        $this->doc->strictErrorChecking = false;
                        $this->doc->loadHTML( $html );
+                       libxml_disable_entity_loader( $loader );
                        libxml_use_internal_errors( false );
                        $this->doc->encoding = 'UTF-8';
                }
@@ -87,7 +93,14 @@ class HtmlFormatter {
        }
 
        /**
-        * Adds one or more selector of content to remove
+        * Adds one or more selector of content to remove. A subset of CSS selector
+        * syntax is supported:
+        *
+        *   <tag>
+        *   <tag>.class
+        *   .<class>
+        *   #<id>
+        *
         * @param Array|string $selectors: Selector(s) of stuff to remove
         */
        public function remove( $selectors ) {
@@ -97,6 +110,10 @@ class HtmlFormatter {
        /**
         * Adds one or more element name to the list to flatten (remove tag, but not its content)
         * Can accept undelimited regexes
+        *
+        * Note this interface may fail in surprising unexpected ways due to usage of regexes,
+        * so should not be relied on for HTML markup security measures.
+        *
         * @param Array|string $elements: Name(s) of tag(s) to flatten
         */
        public function flatten( $elements ) {
@@ -118,6 +135,7 @@ class HtmlFormatter {
                $removals = $this->parseItemsToRemove();
 
                if ( !$removals ) {
+                       wfProfileOut( __METHOD__ );
                        return;
                }
 
@@ -256,6 +274,11 @@ class HtmlFormatter {
                        $html = $this->html;
                }
                if ( wfIsWindows() ) {
+                       // Appears to be cleanup for CRLF misprocessing of unknown origin
+                       // when running server on Windows platform.
+                       //
+                       // If this error continues in the future, please track it down in the
+                       // XML code paths if possible and fix there.
                        $html = str_replace( '&#13;', '', $html );
                }
                $html = preg_replace( '/<!--.*?-->|^.*?<body>|<\/body>.*$/s', '', $html );
@@ -283,14 +306,10 @@ class HtmlFormatter {
                } elseif ( strpos( $selector, '#' ) === 0 ) {
                        $type = 'ID';
                        $rawName = substr( $selector, 1 );
-               } elseif ( strpos( $selector, '.' ) !== 0 &&
-                       strpos( $selector, '.' ) !== false )
-               {
+               } elseif ( strpos( $selector, '.' ) !== 0 && strpos( $selector, '.' ) !== false ) {
                        $type = 'TAG_CLASS';
                        $rawName = $selector;
-               } elseif ( strpos( $selector, '[' ) === false
-                       && strpos( $selector, ']' ) === false )
-               {
+               } elseif ( strpos( $selector, '[' ) === false && strpos( $selector, ']' ) === false ) {
                        $type = 'TAG';
                        $rawName = $selector;
                } else {