2

How can I use DOM parser to remove all attributes in span tags but except these two attributes,

<span style="text-decoration: underline;">cultura</span> accept

<span style="text-decoration: line-through;">heart</span> accept

reject this,

<span style="font-family: " lang="EN-US">May</span> accept

Is it possible?

My working code from the other post I made,

$content = '
<span style="text-decoration: underline;">cultura</span>l <span style="text-decoration: line-through;">heart</span>
<span style="font-family: " lang="EN-US">May</span>
';

$dom = new DOMDocument();
$dom->loadHTML($content);

foreach( $dom->getElementsByTagName( "span" ) as $span )
{

    foreach( $span->attributes as $attrib )
    {
        $span->removeAttributeNode( $attrib );
    }


}

$content =  $dom->saveHTML();

But this code will remove all attributes inside the span tags...

Community
  • 1
  • 1
Run
  • 54,938
  • 169
  • 450
  • 748
  • You're not particularly clear: you want to remove everything except style attributes? or just text-decoration styles? – Marc B Jul 22 '11 at 16:42
  • sorry. yes all but except text-decoration styles. – Run Jul 22 '11 at 16:49
  • CSS is outside the purview of DOM - you can easily remove all but the style attributes, but you'd need to parse the actual style definitions separately. – Marc B Jul 22 '11 at 16:51

2 Answers2

1

You need to do it manually.

DOM handles HTML attributes, not CSS properties.

You need to access the style attribute, explode it's value using ; as a delimiter, then loop the array looking for the value you want to unset.

takeshin
  • 49,108
  • 32
  • 120
  • 164
  • 1
    I'm not convinced this is entirely true; the DOM exposes a parsed version of the `style` attribute through the [`element.style`](https://developer.mozilla.org/en/DOM/element.style) property. – Roman Starkov Oct 21 '11 at 12:51
0

This is entirely possible with DOM only. DOM exposes a parsed version of the style attribute through the element.style property.

$('*').each(function() {
    var s = this.style;
    for (var i = s.length - 1; i >= 0; i--)
       if (s[i] != 'font-weight')
           s.removeProperty(s[i]);
});

The above code removes every style except font-weight.

This uses jQuery's $('*') to iterate over every element, but of course you can do it the hard way using just DOM.

Here's a JsFiddle to play with: http://jsfiddle.net/NbN3S/

One difficulty with this is that the names appear to be browser-dependent, unfortunately: for example, text-decoration is taken apart by Firefox into a bunch of -moz-* styles.

Roman Starkov
  • 59,298
  • 38
  • 251
  • 324