1 (edited by mattfaulds 2010-09-23 19:02:00)

Topic: hook_tag from within class

hi there

i'm enjoying playing with htmLawed - thank you for the development.

i'm having difficulties calling a hook_tag function from within a class. htmLawed is included within a function and i wish to us another function in the same class as the hook_tag function. i can't seem to get the function to be recognised by function_exists. can you help me pin this down?

oh, one other thing. i found that htmLawed can't cope with poorly written html that includes series of <sup> </sup> (a medical site that i trawled seems to do this all the time with their articles...)

2

Re: hook_tag from within class

Sorry, can you explain the situation with some more details? Is it that a function defined in htmLawed configuration through the $config['hook_tag'] parameter is not being recognized as existing by the htmLawed code?

--

Re: the <sup>-containing poor HTML markup, if you think htmLawed should be able to deal with it and other similar poorly-written code, please post the markup. (htmLawed can deal with incorrect markup to some degree beyond which overinterpretation can lead to ordinary text getting recognized as markup, etc.)

3 (edited by mattfaulds 2010-09-28 18:27:08)

Re: hook_tag from within class

Thanks for your reply. I solved the problem by adjusting the code a little.

In my case I had something like:

class parentClass
{
  function childFunc($text)
  {
    require_once('htmLawed.php');
    $text = 'sometext';
    $config('hook_tag'=>'hook_tag_func'); // and other config of course!

    return htmLawed($text,$config);
  }

  function hook_tag_func($e,$a)
  {
    ....
  }
}

This couldn't work because line 72 in htmLawed.php uses function_exists which couldn't access my function.

So, I adjusted to use method_exists(), config now reads $config('hook_tag'=>array('parentClass','hook_tag_func')). Of course, I then had to adjust line 613 to use call_user_func() but it now works nicely for me.

However, I wanted to remove elements based on their attribute but I also want to remove their children. Is there an easy way of doing this with htmLawed as the hook_tag functionality does allow removing an element but it's children remain.

----

I have now forgotten which site generates the terrible <sup> </sup> markup. I'll let you know when I do...

4

Re: hook_tag from within class

Thanks for mentioning how you resolved the function-recognition issue.

Regarding the removal of child elements when a parent element is removed, htmLawed will remove child elements if the new parent element created by removing the original one cannot have them. E.g., if 'table' is removed from '<div><table><tr><td></td></tr></table></div>', using a hook function, then htmLawed will removed 'tr' and 'td' as well [this requires the config. parameter 'balance' to be set to 1]. Otherwise, htmLawed will not remove them. E.g., removing the first 'div' won't cause the removal of the nested 'div' and 'p' in '<div><div><p></p></div></div>'.

I will consider implementing the feature of the kind you are looking for, removal of child elements under all circumstances, in a future release of htmLawed.

You may be able to achieve your goal using a simple regular expression-based search-replace after htmLawed has finished filtering. This may not even require a hook_tag function. Or you can have the hook function add some marker to easily/efficiently identify the offending element during the search-replace phase.