0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-09-19 18:55:19 +00:00

Add support for full document parsing, aka discard everything that's not in-between body if applicable.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@258 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2006-08-15 00:53:24 +00:00
parent d7140f2e05
commit 9a35dfa6b9
5 changed files with 66 additions and 10 deletions

View File

@ -239,9 +239,15 @@ class HTMLPurifier_Lexer
/**
* Takes a string of HTML (fragment or document) and returns the content
*/
function extractBody($html) {
if (strpos($html, '<html') === false) return $html; // already fragment
// ...
function extractBody($html, $return_bool = false) {
$matches = array();
$result = preg_match('!<body[^>]*>(.+?)</body>!is', $html, $matches);
if ($return_bool) return $result;
if ($result) {
return $matches[1];
} else {
return $html;
}
}
}

View File

@ -28,25 +28,31 @@ class HTMLPurifier_Lexer_DOMLex extends HTMLPurifier_Lexer
public function tokenizeHTML($string, $config = null) {
if (!$config) $config = HTMLPurifier_Config::createDefault();
if ($config->get('Core', 'AcceptFullDocuments')) {
$is_full = $this->extractBody($string, true);
}
$doc = new DOMDocument();
$doc->encoding = 'UTF-8'; // technically does nothing, but comprehensive
$doc->encoding = 'UTF-8'; // technically does nothing, but whatever
// replace and escape the CDATA sections, since parsing under HTML
// mode won't get 'em.
$string = $this->escapeCDATA($string);
if (!$is_full) {
// preprocess string, essential for UTF-8
$string =
'<html><head>'.
'<meta http-equiv="Content-Type" content="text/html;'.
' charset=utf-8" />'.
'</head><body><div>'.$string.'</div></body></html>';
'<html><head>'.
'<meta http-equiv="Content-Type" content="text/html;'.
' charset=utf-8" />'.
'</head><body>'.$string.'</body></html>';
}
@$doc->loadHTML($string); // mute all errors, handle it transparently
return $this->tokenizeDOM(
$doc->childNodes->item(1)-> // html
childNodes->item(1)-> // body
childNodes->item(0) // div
getElementsByTagName('body')->item(0) // body
);
}

View File

@ -114,6 +114,10 @@ class HTMLPurifier_Lexer_DirectLex extends HTMLPurifier_Lexer
$string = @ (string) $string;
if ($string == '') return array();
if ($config->get('Core', 'AcceptFullDocuments')) {
$string = $this->extractBody($string);
}
$cursor = 0; // our location in the text
$inside_tag = false; // whether or not we're parsing the inside of a tag
$array = array(); // result array

View File

@ -32,6 +32,9 @@ class HTMLPurifier_Lexer_PEARSax3 extends HTMLPurifier_Lexer
function tokenizeHTML($html, $config = null) {
if (!$config) $config = HTMLPurifier_Config::createDefault();
$html = $this->escapeCDATA($html);
if ($config->get('Core', 'AcceptFullDocuments')) {
$html = $this->extractBody($html);
}
$html = $this->substituteNonSpecialEntities($html);
$parser=& new XML_HTMLSax3();
$parser->set_object($this);

View File

@ -40,7 +40,44 @@ class HTMLPurifier_LexerTest extends UnitTestCase
$this->Lexer->substituteNonSpecialEntities('"') );
}
function assertExtractBody($text, $extract = true) {
$result = $this->Lexer->extractBody($text);
if ($extract === true) $extract = $text;
$this->assertIdentical($extract, $result);
}
function test_extractBody() {
$this->assertExtractBody('<b>Bold</b>');
$this->assertExtractBody('<html><body><b>Bold</b></body></html>', '<b>Bold</b>');
$this->assertExtractBody(
'<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>xyz</title>
</head>
<body>
<form method="post" action="whatever1">
<div>
<input type="text" name="username" />
<input type="text" name="password" />
<input type="submit" />
</div>
</form>
</body>
</html>',
'
<form method="post" action="whatever1">
<div>
<input type="text" name="username" />
<input type="text" name="password" />
<input type="submit" />
</div>
</form>
');
$this->assertExtractBody('<html><body bgcolor="#F00"><b>Bold</b></body></html>', '<b>Bold</b>');
$this->assertExtractBody('<body>asdf'); // not closed, don't accept
}