Welcome! » Log In » Create A New Profile

height attribute on <table> removed

Posted by DP 
DP
height attribute on <table> removed
July 20, 2009 06:57PM

Hello,

I'm trying to allow the "height" attribute on the table element, and I can't seem to get it to work properly.

My config is this:


		$config->set(&#039;Core&#039;, &#039;Encoding&#039;, &#039;ISO-8859-1&#039;);
		$config->set(&#039;Core&#039;, &#039;CollectErrors&#039;, true);
		$config->set(&#039;HTML&#039;, &#039;Doctype&#039;, &#039;XHTML 1.0 Transitional&#039;);
		$config->set(&#039;Core&#039;, &#039;EscapeNonASCIICharacters&#039;, true);
		$config->set(&#039;Core&#039;, &#039;EscapeInvalidTags&#039;, true);
		$config->set(&#039;HTML&#039;, &#039;Trusted&#039;, true);
		$config->set(&#039;HTML&#039;, &#039;SafeObject&#039;, true);
		$config->set(&#039;HTML&#039;, &#039;SafeEmbed&#039;, true);
		$config->set(&#039;HTML&#039;, &#039;EnableAttrID&#039;, true);
		$config->set(&#039;HTML&#039;, &#039;TidyLevel&#039;, &#039;medium&#039;);
		$config->set(&#039;Filter&#039;, &#039;YouTube&#039;, true);
		$config->set(&#039;Cache&#039;, &#039;DefinitionImpl&#039;, null); // remove this later!
				
		$config->set(&#039;HTML&#039;, &#039;DefinitionID&#039;, &#039;1&#039;);
		$config->set(&#039;HTML&#039;, &#039;DefinitionRev&#039;, 2);

$def =& $config->getHTMLDefinition(true);
$def->addAttribute(&#039;table&#039;, &#039;height&#039;, new HTMLPurifier_AttrDef_Enum(array(0 => &#039;*&#039;)));

I have tried to define the value array in multiple ways, but nothing seems to work. If anybody sees what I'm doing wrong, I'd appreciate the input.

Re: height attribute on <table> removed
July 20, 2009 07:03PM
new HTMLPurifier_AttrDef_Enum(array(0 => &#039;*&#039;))

This is definitely incorrect. Where did you get this syntax from?

This might be a better job for an AttrTransform, since if you want to keep your code standards compliant, you should use the CSS height instead.

DP
Re: height attribute on <table> removed
July 20, 2009 07:26PM

Well...I'm really really new with PHP, and I've tried all sorts of ways to define the statement above. What I pasted was just the last attempt.

About your suggestion, I assume you're saying to create an object similar to those in \HTMLPurifier\AttrTransform.

I've modified one of the existing objects there like this:


<?php

class HTMLPurifier_AttrTransform_TableHeight extends HTMLPurifier_AttrTransform {

    public function transform($attr, $config, $context) {
        
        if (!isset($attr[&#039;height&#039;])) return $attr;
        
        $height = $this->confiscateAttr($attr, &#039;height&#039;);

        $this->prependCSS($attr, "height:$height;");
        
        return $attr;
        
    }    
}

Can you tell if it's correct and if so, how do I "apply" this?

DP
Re: height attribute on <table> removed
July 20, 2009 08:12PM

Got it.

I added the last line in \HTMLPurifier\HTMLModule\Tidy\Proprietary.php


<?php

class HTMLPurifier_HTMLModule_Tidy_Proprietary extends HTMLPurifier_HTMLModule_Tidy
{
    
    public $name = &#039;Tidy_Proprietary&#039;;
    public $defaultLevel = &#039;light&#039;;
    
    public function makeFixes() {
        $r = array();
        $r[&#039;table@background&#039;] = new HTMLPurifier_AttrTransform_Background();
        $r[&#039;td@background&#039;]    = new HTMLPurifier_AttrTransform_Background();
        $r[&#039;th@background&#039;]    = new HTMLPurifier_AttrTransform_Background();
        $r[&#039;tr@background&#039;]    = new HTMLPurifier_AttrTransform_Background();
        $r[&#039;thead@background&#039;] = new HTMLPurifier_AttrTransform_Background();
        $r[&#039;tfoot@background&#039;] = new HTMLPurifier_AttrTransform_Background();
        $r[&#039;tbody@background&#039;] = new HTMLPurifier_AttrTransform_Background();
	 $r[&#039;table@height&#039;] = new HTMLPurifier_AttrTransform_TableHeight();
        return $r;
    }
    
}

It's now transforming

<table height="100%"> to <table style="height:100%;">

Thanks for the advice.

Re: height attribute on <table> removed
July 20, 2009 08:26PM

Great! I'll add this to the next release.

Re: height attribute on <table> removed
July 22, 2009 02:20PM

i didn't think height was allowed in table (has it ever been? because as far as i'm aware, height has never ever been an attribute of table) (do new browsers actually support it?). it certainly isn't valid markup. only width is used. it's still valid for use in TH & TD etc.. but certainly not in Table.

and as you're new to PHP etc, would it not be better to start off doing it the correct valid way rather than the wrong way?

also if you're doctype is XHTML then you definitely will not pass the validation check.

It&#039;s now transforming

<table height="100%"> to <table style="height:100%;">

what would happen if you tried to use


            
            

            

            

        
Re: height attribute on <table> removed
July 22, 2009 02:55PM

I did a quick google search, and I believe at least some browsers support height, so it's a "proprietary" attribute. That's why a transform is preferred.

Re: height attribute on <table> removed
July 22, 2009 03:19PM

yeah i just checked.. :) though they don't support height="100%" when in xhtml doctypes, it has to be a fixed size, which is why CSS has to be used for heights defined in %.

Re: height attribute on <table> removed
July 22, 2009 03:23PM

Yeah, I kinda suspected they didn't. :-)

Sorry, you do not have permission to post/reply in this forum.