1

Topic: Requiring an attribute be added and reset to value even if missing

Hello!

I was trying to make HTMLawed add to every IFRAME tag it finds the sandbox attribute with a specific fixed value.

For example, no matter if the sandbox attribute is present or with what value, it always should output something like:

<iframe sandbox="allow-scripts" ...>

Is it possible to achieve this with HTMLawed?

My Config:

'comment' => 1,
'safe' => 1,
'cdata' => 1,
'remove_void_tags' => true,
'elements' => '*-script-style+iframe+object+embed',

My $spec param:

'iframe=sandbox(default="allow-scripts")'

But it works ONLY if sandbox attribute is already present!

2

Re: Requiring an attribute be added and reset to value even if missing

Excuse me for the long delay in responding.

I will post a solution in a day or so.

3

Re: Requiring an attribute be added and reset to value even if missing

Don't worry, I'll wait patiently, don't rush!

Thank you very much!

4

Re: Requiring an attribute be added and reset to value even if missing

For your purpose, you can utilize the $config hook_tag parameter. You will have to write a custom function, whose PHP code can be put anywhere as long as it gets called when htmLawed's code gets called. The name of the function is assigned to $config['hook_tag']. Example code below.

//// Define the custom function

function my_function($element, $attributeValueArray = 0){

  // If second argument is not received, it means a closing tag is being handled

  if(is_numeric($attributeValueArray)){
    return "</$element>";
  }

  // Inject sandbox = allow-scripts in all opening iframe tags
 
  if($element == 'iframe'){
    $attributeValueArray['sandbox'] => 'allow-scripts';
  }

  // Return

  $string = '';

  foreach($attributeValueArray as $k=>$v){
    $string .= " {$k}=\"{$v}\"";
  }

  static $empty_elements = array('area'=>1, 'br'=>1, 'col'=>1, 'command'=>1, 'embed'=>1, 'hr'=>1, 'img'=>1, 'input'=>1, 'isindex'=>1, 'keygen'=>1, 'link'=>1, 'meta'=>1, 'param'=>1, 'source'=>1, 'track'=>1, 'wbr'=>1);

  return "<{$element}{$string}".(array_key_exists($element, $empty_elements) ? ' /' : '').'>';

}

//// Call htmLawed with function's name specified in $config hook_tag

$config = array( ... 'hook_tag' => 'my_function' ...);
... htmLawed( ... $config ...);

5

Re: Requiring an attribute be added and reset to value even if missing

It works flawlessly!

Thanks again!