Making WordPress.org


Ignore:
Timestamp:
02/07/2024 05:58:19 AM (3 years ago)
Author:
dd32
Message:

Trac: Escape HTML within script tags, as DOMDocument doesn't like it. Remove CDATA entirely, I don't think it's needed.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/trac.wordpress.org/templates/update-headers.php

    r12855 r13181  
    77function domdocument_from_url( $url ) {
    88        $html = file_get_contents( $url );
     9
     10        /*
     11         * Escape HTML within Javascript strings.
     12         * DomDocument doesn't handle HTML tags within Javascript strings.
     13         * See https://stackoverflow.com/questions/40703313/php-domdocument-errors-while-parsing-unescaped-strings
     14         */
     15        $html = preg_replace_callback(
     16                '!<script([^>]+)>(.*?)</script>!ism',
     17                function( $m ) {
     18                        $escaped = $m[2];
     19                        $escaped = str_replace( array( '<', '>' ), array( '\x3C',  '\x3E' ), $escaped );
     20                        return "<script{$m[1]}>{$escaped}</script>";
     21                },
     22                $html
     23        );
    924
    1025        $doc = new DOMDocument();
     
    4257        $html = preg_replace( '#<style([^>]*)><!\[CDATA\[(.+?)\]\]></style>#ism', "<style$1>$2</style>", $html );
    4358
    44         // Escape CDATA tags in <script>
    45         $html = preg_replace( '#<script([^>]*)><!\[CDATA\[(.+?)\]\]></script>#ism', "<script$1>//<![CDATA[\n$2\n//]]></script>", $html );
     59        // Remove CDATA tags in <script>
     60        $html = preg_replace( '#<script([^>]*)><!\[CDATA\[(.+?)\]\]></script>#ism', "<script$1>$2</script>", $html );
    4661
    4762        // Remove trailing whitespace.
Note: See TracChangeset for help on using the changeset viewer.