2006-11-22 18:55:15 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2011-12-26 06:49:26 +00:00
|
|
|
* Definition for tables. The general idea is to extract out all of the
|
|
|
|
* essential bits, and then reconstruct it later.
|
|
|
|
*
|
|
|
|
* This is a bit confusing, because the DTDs and the W3C
|
|
|
|
* validators seem to disagree on the appropriate definition. The
|
|
|
|
* DTD claims:
|
|
|
|
*
|
|
|
|
* (CAPTION?, (COL*|COLGROUP*), THEAD?, TFOOT?, TBODY+)
|
|
|
|
*
|
|
|
|
* But actually, the HTML4 spec then has this to say:
|
|
|
|
*
|
|
|
|
* The TBODY start tag is always required except when the table
|
|
|
|
* contains only one table body and no table head or foot sections.
|
|
|
|
* The TBODY end tag may always be safely omitted.
|
|
|
|
*
|
|
|
|
* So the DTD is kind of wrong. The validator is, unfortunately, kind
|
|
|
|
* of on crack.
|
|
|
|
*
|
|
|
|
* The definition changed again in XHTML1.1; and in my opinion, this
|
|
|
|
* formulation makes the most sense.
|
|
|
|
*
|
|
|
|
* caption?, ( col* | colgroup* ), (( thead?, tfoot?, tbody+ ) | ( tr+ ))
|
|
|
|
*
|
|
|
|
* Essentially, we have two modes: thead/tfoot/tbody mode, and tr mode.
|
|
|
|
* If we encounter a thead, tfoot or tbody, we are placed in the former
|
|
|
|
* mode, and we *must* wrap any stray tr segments with a tbody. But if
|
|
|
|
* we don't run into any of them, just have tr tags is OK.
|
2006-11-22 18:55:15 +00:00
|
|
|
*/
|
|
|
|
class HTMLPurifier_ChildDef_Table extends HTMLPurifier_ChildDef
|
|
|
|
{
|
2013-07-16 11:56:14 +00:00
|
|
|
/**
|
|
|
|
* @type bool
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public $allow_empty = false;
|
2013-07-16 11:56:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @type string
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public $type = 'table';
|
2013-07-16 11:56:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @type array
|
|
|
|
*/
|
|
|
|
public $elements = array(
|
|
|
|
'tr' => true,
|
|
|
|
'tbody' => true,
|
|
|
|
'thead' => true,
|
|
|
|
'tfoot' => true,
|
|
|
|
'caption' => true,
|
|
|
|
'colgroup' => true,
|
|
|
|
'col' => true
|
|
|
|
);
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $tokens_of_children
|
|
|
|
* @param HTMLPurifier_Config $config
|
|
|
|
* @param HTMLPurifier_Context $context
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function validateChildren($tokens_of_children, $config, $context)
|
|
|
|
{
|
|
|
|
if (empty($tokens_of_children)) {
|
|
|
|
return false;
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-11-22 18:55:15 +00:00
|
|
|
// this ensures that the loop gets run one last time before closing
|
|
|
|
// up. It's a little bit of a hack, but it works! Just make sure you
|
|
|
|
// get rid of the token later.
|
|
|
|
$tokens_of_children[] = false;
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-11-22 18:55:15 +00:00
|
|
|
// only one of these elements is allowed in a table
|
|
|
|
$caption = false;
|
2013-07-16 11:56:14 +00:00
|
|
|
$thead = false;
|
|
|
|
$tfoot = false;
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-11-22 18:55:15 +00:00
|
|
|
// as many of these as you want
|
2013-07-16 11:56:14 +00:00
|
|
|
$cols = array();
|
2006-11-22 18:55:15 +00:00
|
|
|
$content = array();
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-11-22 18:55:15 +00:00
|
|
|
$nesting = 0; // current depth so we can determine nodes
|
|
|
|
$is_collecting = false; // are we globbing together tokens to package
|
2013-07-16 11:56:14 +00:00
|
|
|
// into one of the collectors?
|
2006-11-22 18:55:15 +00:00
|
|
|
$collection = array(); // collected nodes
|
2013-07-27 04:54:53 +00:00
|
|
|
// INVARIANT: if $is_collecting, then !empty($collection)
|
|
|
|
// The converse does NOT hold, see [WHITESPACE]
|
2006-11-22 18:55:15 +00:00
|
|
|
$tag_index = 0; // the first node might be whitespace,
|
2013-07-16 11:56:14 +00:00
|
|
|
// so this tells us where the start tag is
|
2011-12-26 06:49:26 +00:00
|
|
|
$tbody_mode = false; // if true, then we need to wrap any stray
|
2013-07-16 11:56:14 +00:00
|
|
|
// <tr>s with a <tbody>.
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-11-22 18:55:15 +00:00
|
|
|
foreach ($tokens_of_children as $token) {
|
|
|
|
$is_child = ($nesting == 0);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-11-22 18:55:15 +00:00
|
|
|
if ($token === false) {
|
|
|
|
// terminating sequence started
|
2008-01-19 20:23:01 +00:00
|
|
|
} elseif ($token instanceof HTMLPurifier_Token_Start) {
|
2006-11-22 18:55:15 +00:00
|
|
|
$nesting++;
|
2008-01-19 20:23:01 +00:00
|
|
|
} elseif ($token instanceof HTMLPurifier_Token_End) {
|
2006-11-22 18:55:15 +00:00
|
|
|
$nesting--;
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-11-22 18:55:15 +00:00
|
|
|
// handle node collection
|
|
|
|
if ($is_collecting) {
|
|
|
|
if ($is_child) {
|
|
|
|
// okay, let's stash the tokens away
|
|
|
|
// first token tells us the type of the collection
|
|
|
|
switch ($collection[$tag_index]->name) {
|
|
|
|
case 'tbody':
|
2011-12-26 06:49:26 +00:00
|
|
|
$tbody_mode = true;
|
2013-07-18 17:22:45 +00:00
|
|
|
// fall through
|
2011-12-26 06:49:26 +00:00
|
|
|
case 'tr':
|
2006-11-22 18:55:15 +00:00
|
|
|
$content[] = $collection;
|
|
|
|
break;
|
|
|
|
case 'caption':
|
2013-07-16 11:56:14 +00:00
|
|
|
if ($caption !== false) {
|
|
|
|
break;
|
|
|
|
}
|
2006-11-22 18:55:15 +00:00
|
|
|
$caption = $collection;
|
|
|
|
break;
|
|
|
|
case 'thead':
|
|
|
|
case 'tfoot':
|
2011-12-26 06:49:26 +00:00
|
|
|
$tbody_mode = true;
|
|
|
|
// XXX This breaks rendering properties with
|
|
|
|
// Firefox, which never floats a <thead> to
|
|
|
|
// the top. Ever. (Our scheme will float the
|
|
|
|
// first <thead> to the top.) So maybe
|
|
|
|
// <thead>s that are not first should be
|
|
|
|
// turned into <tbody>? Very tricky, indeed.
|
|
|
|
|
2006-11-22 18:55:15 +00:00
|
|
|
// access the appropriate variable, $thead or $tfoot
|
|
|
|
$var = $collection[$tag_index]->name;
|
|
|
|
if ($$var === false) {
|
|
|
|
$$var = $collection;
|
|
|
|
} else {
|
2011-12-26 06:49:26 +00:00
|
|
|
// Oops, there's a second one! What
|
|
|
|
// should we do? Current behavior is to
|
|
|
|
// transmutate the first and last entries into
|
|
|
|
// tbody tags, and then put into content.
|
|
|
|
// Maybe a better idea is to *attach
|
|
|
|
// it* to the existing thead or tfoot?
|
|
|
|
// We don't do this, because Firefox
|
|
|
|
// doesn't float an extra tfoot to the
|
|
|
|
// bottom like it does for the first one.
|
2006-11-22 18:55:15 +00:00
|
|
|
$collection[$tag_index]->name = 'tbody';
|
2013-07-16 11:56:14 +00:00
|
|
|
$collection[count($collection) - 1]->name = 'tbody';
|
2006-11-22 18:55:15 +00:00
|
|
|
$content[] = $collection;
|
|
|
|
}
|
|
|
|
break;
|
2013-07-16 11:56:14 +00:00
|
|
|
case 'colgroup':
|
2006-11-22 18:55:15 +00:00
|
|
|
$cols[] = $collection;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$collection = array();
|
|
|
|
$is_collecting = false;
|
|
|
|
$tag_index = 0;
|
|
|
|
} else {
|
|
|
|
// add the node to the collection
|
|
|
|
$collection[] = $token;
|
|
|
|
}
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-11-22 18:55:15 +00:00
|
|
|
// terminate
|
2013-07-16 11:56:14 +00:00
|
|
|
if ($token === false) {
|
|
|
|
break;
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-11-22 18:55:15 +00:00
|
|
|
if ($is_child) {
|
|
|
|
// determine what we're dealing with
|
|
|
|
if ($token->name == 'col') {
|
|
|
|
// the only empty tag in the possie, we can handle it
|
|
|
|
// immediately
|
|
|
|
$cols[] = array_merge($collection, array($token));
|
|
|
|
$collection = array();
|
2013-07-27 04:54:53 +00:00
|
|
|
$is_collecting = false;
|
2006-11-22 18:55:15 +00:00
|
|
|
$tag_index = 0;
|
|
|
|
continue;
|
|
|
|
}
|
2013-07-16 11:56:14 +00:00
|
|
|
switch ($token->name) {
|
2006-11-22 18:55:15 +00:00
|
|
|
case 'caption':
|
|
|
|
case 'colgroup':
|
|
|
|
case 'thead':
|
|
|
|
case 'tfoot':
|
|
|
|
case 'tbody':
|
|
|
|
case 'tr':
|
|
|
|
$is_collecting = true;
|
|
|
|
$collection[] = $token;
|
|
|
|
continue;
|
|
|
|
default:
|
2013-07-27 04:54:53 +00:00
|
|
|
// [WHITESPACE] Whitespace is added to the
|
|
|
|
// collection without triggering collection
|
|
|
|
// mode. This is a hack to make whitespace
|
|
|
|
// 'sticky' (that is to say, we ought /not/ to
|
|
|
|
// drop whitespace.)
|
2009-02-05 23:04:10 +00:00
|
|
|
if (!empty($token->is_whitespace)) {
|
2006-11-22 18:55:15 +00:00
|
|
|
$collection[] = $token;
|
|
|
|
$tag_index++;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
if (empty($content)) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-07-27 04:54:53 +00:00
|
|
|
// INVARIANT: all members of content are non-empty. This can
|
|
|
|
// be shown by observing when things are pushed onto content:
|
|
|
|
// they are only ever pushed when is_collecting is true, and
|
|
|
|
// collection is the only thing ever pushed; but it is known
|
|
|
|
// that collections are non-empty when is_collecting is true.
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-11-22 18:55:15 +00:00
|
|
|
$ret = array();
|
2013-07-16 11:56:14 +00:00
|
|
|
if ($caption !== false) {
|
|
|
|
$ret = array_merge($ret, $caption);
|
|
|
|
}
|
|
|
|
if ($cols !== false) {
|
|
|
|
foreach ($cols as $token_array) {
|
|
|
|
$ret = array_merge($ret, $token_array);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($thead !== false) {
|
|
|
|
$ret = array_merge($ret, $thead);
|
|
|
|
}
|
|
|
|
if ($tfoot !== false) {
|
|
|
|
$ret = array_merge($ret, $tfoot);
|
|
|
|
}
|
2011-12-26 06:49:26 +00:00
|
|
|
|
|
|
|
if ($tbody_mode) {
|
|
|
|
// a little tricky, since the start of the collection may be
|
|
|
|
// whitespace
|
|
|
|
$inside_tbody = false;
|
|
|
|
foreach ($content as $token_array) {
|
|
|
|
// find the starting token
|
2013-07-27 04:54:53 +00:00
|
|
|
// INVARIANT: token_array is not empty
|
|
|
|
$t = NULL;
|
2011-12-26 06:49:26 +00:00
|
|
|
foreach ($token_array as $t) {
|
|
|
|
if ($t->name === 'tr' || $t->name === 'tbody') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} // iterator variable carries over
|
|
|
|
if ($t->name === 'tr') {
|
|
|
|
if ($inside_tbody) {
|
|
|
|
$ret = array_merge($ret, $token_array);
|
|
|
|
} else {
|
|
|
|
$ret[] = new HTMLPurifier_Token_Start('tbody');
|
|
|
|
$ret = array_merge($ret, $token_array);
|
|
|
|
$inside_tbody = true;
|
|
|
|
}
|
|
|
|
} elseif ($t->name === 'tbody') {
|
|
|
|
if ($inside_tbody) {
|
|
|
|
$ret[] = new HTMLPurifier_Token_End('tbody');
|
|
|
|
$inside_tbody = false;
|
|
|
|
$ret = array_merge($ret, $token_array);
|
|
|
|
} else {
|
|
|
|
$ret = array_merge($ret, $token_array);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
trigger_error("tr/tbody in content invariant failed in Table ChildDef", E_USER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($inside_tbody) {
|
|
|
|
$ret[] = new HTMLPurifier_Token_End('tbody');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
foreach ($content as $token_array) {
|
|
|
|
// invariant: everything in here is <tr>s
|
|
|
|
$ret = array_merge($ret, $token_array);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
if (!empty($collection) && $is_collecting == false) {
|
2006-11-22 18:55:15 +00:00
|
|
|
// grab the trailing space
|
|
|
|
$ret = array_merge($ret, $collection);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-11-22 18:55:15 +00:00
|
|
|
array_pop($tokens_of_children); // remove phantom token
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-11-22 18:55:15 +00:00
|
|
|
return ($ret === $tokens_of_children) ? true : $ret;
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-11-22 18:55:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-06 09:24:59 +00:00
|
|
|
// vim: et sw=4 sts=4
|