| File: | lib/SEO/Inspector/Plugin/MetaDescriptionCheck.pm |
| Coverage: | 86.1% |
| line | stmt | bran | cond | sub | time | code |
|---|---|---|---|---|---|---|
| 1 | package SEO::Inspector::Plugin::MetaDescriptionCheck; | |||||
| 2 | ||||||
| 3 | 5 5 5 | 2830 6 75 | use strict; | |||
| 4 | 5 5 5 | 11 9 89 | use warnings; | |||
| 5 | 5 5 5 | 1511 65182 19 | use HTML::TreeBuilder; | |||
| 6 | ||||||
| 7 | 7 | 4380 | sub new { bless {}, shift } | |||
| 8 | ||||||
| 9 | 0 | 0 | sub name { 'MetaDescriptionCheck' } | |||
| 10 | ||||||
| 11 | sub run { | |||||
| 12 | 5 | 5 | my ($self, $html) = @_; | |||
| 13 | ||||||
| 14 | 5 | 11 | my $tree = HTML::TreeBuilder->new; | |||
| 15 | 5 | 541 | $tree->parse_content($html); | |||
| 16 | ||||||
| 17 | # Find <meta name="description"> safely | |||||
| 18 | 5 | 1716 | my $meta_content; | |||
| 19 | 5 | 14 | for my $el ($tree->find_by_tag_name('meta')) { | |||
| 20 | 3 | 85 | next unless defined $el->attr('name') && $el->attr('name') eq 'description'; | |||
| 21 | 3 | 34 | $meta_content = $el->attr('content'); | |||
| 22 | 3 | 12 | last; | |||
| 23 | } | |||||
| 24 | ||||||
| 25 | 5 | 42 | $tree->delete; | |||
| 26 | ||||||
| 27 | 5 | 154 | if ($meta_content) { | |||
| 28 | 3 | 20 | return { status => 'ok', notes => 'meta description present' }; | |||
| 29 | } | |||||
| 30 | ||||||
| 31 | 2 | 12 | return { status => 'warn', notes => 'missing meta description' }; | |||
| 32 | } | |||||
| 33 | ||||||
| 34 | 1; | |||||