Question

I would like to modify the value of an attribute, if another attribute contains a certain string.

my $dom = Mojo::DOM->new('<link href="http://google.com/feed/" rel="alternate">');
$dom->at('link[href*="google"]')->replace_content('http://www.yahoo.com/feed/');
print $dom;

So if, tag "<LINK" contains the word "google" in the HREF attribute, it should change the HREF attribute to yahoo. But instead the output is:

<link href="http://google.com/feed/" rel="alternate">http://www.yahoo.com/feed/</link>

But I want it to be:

<link href="http://www.yahoo.com/feed/" rel="alternate">

I realize the above is not meant to work (replace_content() modifies the content, not the attribute), but it is just to explain what I want.

Thank you

Was it helpful?

Solution

Use the attr method instead, i.e.

$dom->at('link[href*="google"]')->attr(href => 'http://www.yahoo.com/feed/');

it can set attributes to specified value.

p/s: The attrs method has been deprecated (and eventually removed) in favor of attr.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top