Skip to content

Commit

Permalink
Merge pull request #202 from keitaoouchi/fix/hreflang_element_to_opti…
Browse files Browse the repository at this point in the history
…onal

Change 'hreflang' to optional element
  • Loading branch information
kjvarga committed Jul 15, 2015
2 parents f8e79ed + 5d43d39 commit 2fd6fd9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1044,7 +1044,7 @@ end

* `:alternate`/`:alternates` - Hash or array of hashes, respectively
* `:href` - Required, string.
* `:lang` - Required, string.
* `:lang` - Optional, string.
* `:nofollow` - Optional, boolean. Used to mark link as "nofollow".
* `:media` - Optional, string. Specify [media targets for responsive design pages][media].

Expand Down
3 changes: 2 additions & 1 deletion lib/sitemap_generator/builder/sitemap_url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ def to_xml(builder=nil)

self[:alternates].each do |alternate|
rel = alternate[:nofollow] ? 'alternate nofollow' : 'alternate'
attributes = { :rel => rel, :hreflang => alternate[:lang].to_s, :href => alternate[:href].to_s }
attributes = { :rel => rel, :href => alternate[:href].to_s }
attributes[:hreflang] = alternate[:lang].to_s if SitemapGenerator::Utilities.present?(alternate[:lang])
attributes[:media] = alternate[:media].to_s if SitemapGenerator::Utilities.present?(alternate[:media])
builder.xhtml :link, attributes
end
Expand Down
21 changes: 21 additions & 0 deletions spec/sitemap_generator/alternate_sitemap_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,27 @@
alternate.attribute('media').should be_nil
end

it "should not include hreflang element unless provided" do
xml_fragment = SitemapGenerator::Builder::SitemapUrl.new('link_with_alternates.html',
:host => 'http://www.example.com',
:alternates => [
{
:href => 'http://www.example.de/link_with_alternate.html'
}
]
).to_xml

doc = Nokogiri::XML.parse("<root xmlns='http://www.sitemaps.org/schemas/sitemap/0.9' xmlns:xhtml='http://www.w3.org/1999/xhtml'>#{xml_fragment}</root>")
url = doc.css('url')
url.should_not be_nil
url.css('loc').text.should == 'http://www.example.com/link_with_alternates.html'

alternate = url.at_xpath('xhtml:link')
alternate.should_not be_nil
alternate.attribute('rel').value.should == 'alternate'
alternate.attribute('hreflang').should be_nil
end

it "should add alternate links to sitemap" do
xml_fragment = SitemapGenerator::Builder::SitemapUrl.new('link_with_alternates.html',
:host => 'http://www.example.com',
Expand Down

0 comments on commit 2fd6fd9

Please sign in to comment.