开发者

Ignoring unclosed tags from another <div>?

开发者 https://www.devze.com 2023-01-03 23:17 出处:网络
I have a website where members can input text using a limited subset of HTML.When a page is displayed that contains a user\'s text, if they have any unclosed tags, the formatting \"bleeds\" across int

I have a website where members can input text using a limited subset of HTML. When a page is displayed that contains a user's text, if they have any unclosed tags, the formatting "bleeds" across into the next area. For example, if the user entered:

Hi, my name is <b>John

Then, the rest of the page will be bold.

Ideally, there'd be someting I could do that would be this simple:

<div contained>Hi, my name is <b>John</div>

And no tags could bleed out of that div. Assuming there isn't anything this simple, how would I accomplish a sim开发者_运维技巧ilar effect? Or, is there something this easy?

Importantly, I do not want to validate the user's input and return an error if they have unclosed tags, since I want to provide the "easiest" user interface possible for my users.

Thanks!


i have solution for php

<?php
    // close opened html tags
    function closetags ( $html )
        {
        #put all opened tags into an array
        preg_match_all ( "#<([a-z]+)( .*)?(?!/)>#iU", $html, $result );
        $openedtags = $result[1];
        #put all closed tags into an array
        preg_match_all ( "#</([a-z]+)>#iU", $html, $result );
        $closedtags = $result[1];
        $len_opened = count ( $openedtags );
        # all tags are closed
        if( count ( $closedtags ) == $len_opened )
        {
        return $html;
        }
        $openedtags = array_reverse ( $openedtags );
        # close tags
        for( $i = 0; $i < $len_opened; $i++ )
        {
            if ( !in_array ( $openedtags[$i], $closedtags ) )
            {
            $html .= "</" . $openedtags[$i] . ">";
            }
            else
            {
            unset ( $closedtags[array_search ( $openedtags[$i], $closedtags)] );
            }
        }
        return $html;
    }
    // close opened html tags

?>

you can use this function like

   <?php echo closetags("your content <p>test test"); ?>


You can put the HTML snippet through Tidy, which will do its best to fix it. Many languages include it in some fashion or another, here for example PHP.


This can't be done.

Don't let users invalidate your HTML.

If you don't want to let users fix their errors, then try to clean it up automatically for them.


You can parse the data entered by the user. Thats what an XML does. You may need to parse or replace the standard html or xml symbols like '<', '>', '/', '&', etc... with '&lt', '&gt', etc...

In this way you can achieve whatever you want.


There is a way to do this using HTML and javascript. I wouldn't recommend this method for public-facing websites; you should clean your data before it reaches the browser. But it might be useful in other situations.

The idea is to put the potentially invalid content into a noscript tag, like this:

<noscript class="contained">
    <div>Hi, my name is <b>John</div>
</noscript>

... and then add javascript that will load it into the DOM. Using jQuery (but probably not necessary):

$("noscript.contained").each(function () {
    $(this).replaceWith(this.innerText);
});

Note that users without javascript will still experience the "bleeding" that you are trying to avoid.

0

精彩评论

暂无评论...
验证码 换一张
取 消