Skip to content

代码高亮功能会导致页面显示异常 #97

@xxNull-lsk

Description

@xxNull-lsk
    function dangopress_esc_html($content)
    {
        if (!is_feed() || !is_robots()) {
            $content = preg_replace('/<code(.*)>/i', "<code class=\"prettyprint\" \$1>", $content);
        }
        $regex = '/(<code.*?>)(.*?)(<\/code>)/sim';
        return preg_replace_callback($regex, 'dangopress_esc_callback', $content);
    }

其中,给code加class的方法过于简单粗暴。如果原来的code中已经有class,就会带来问题。

简单的修改方法为:

    function dangopress_esc_html($content)
    {
        if (!is_feed() || !is_robots()) {
            $content = preg_replace('/<code(.*)>/i', "<code class=\"prettyprint\" \$1>", $content);
        }
        $regex = '/(<code.*?>)(.*?)(<\/code>)/sim';
        return preg_replace_callback($regex, 'dangopress_esc_callback', $content);
    }

    function dangopress_esc_callback($matches)
    {
        $tag_open = $matches[1];
        if ( substr_count($tag_open, "class=") > 1 ){
            $tag_open = str_replace(' class="prettyprint" ', " ", $tag_open);
        }
        $content = $matches[2];
        $tag_close = $matches[3];
        //$content = htmlspecialchars($content, ENT_NOQUOTES, get_bloginfo('charset'));
        $content = esc_html($content);

        return $tag_open . $content . $tag_close;
    }

也就是在dangopress_esc_callback中再修正回来。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions