Leo*_*ang 9 html javascript php
我在PHP中使用HTML Tidy,由于<script>JavaScript字符串文字中的标记,它会产生意想不到的结果.这是一个示例输入:
<html>
<script>
var t='<script><'+'/script>';
</script>
</html>
Run Code Online (Sandbox Code Playgroud)
HTML Tidy的输出:
<html>
<script>
//<![CDATA[
var t='<script><'+'/script>';
<\/script>
<\/html>
//]]>
</script>
</html>
Run Code Online (Sandbox Code Playgroud)
它是</script></html>作为脚本的一部分进行解释的.然后,它添加另一个</script></html>来关闭打开的标签.我在HTML Tidy(http://www.dirtymarkup.com/)的在线版本上尝试了这个,它产生了同样的错误.
如何防止在PHP中发生此错误?
稍微玩了一下我发现可以使用注释//'<\/script>'来混淆算法,以防止这种错误发生:
<html>
<script>
var t='<script><'+'/script>'; //'<\/script>'
</script>
</html>
Run Code Online (Sandbox Code Playgroud)
清理后:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<script>
var t='<script><'+'/script>'; //'<\/script>'
</script>
<title></title>
</head>
<body>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我的猜测是,当清理算法查看代码并检测到<script>两次字符串时,它会</script>立即查找.而separting <与/script>使第二</script>未被发现,这就是为什么它决定添加其他</script>的代码结尾,不知何故也antoher关闭它</html>.(确实糟糕的设计!)
所以我做了第二个假设,即算法中没有if语句来确定a </scirpt>是否在评论中,我是对的!将另一个字符串<\/script>作为javascript注释确实使算法认为</script>总共有两个.