From 1da6216ef3d7fdb6bb8a4106b7107b798eb8873a Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Fri, 2 Oct 2020 16:06:08 -0400 Subject: [PATCH] MetaTag: when encoding for XML special characters, handle non-string objects The jekyll-github-metadata plugin, for example, sets `site.title` and `site.name` to `Jekyll::GitHubMetadata::Value` objects which respond to `#to_s` and `#to_liquid`, but NOT `#encode`. Therefore, we should cast to a string if `#encode` is not yet available. The reason we cast to a string is because the previous code was "#{k}=#{v}", which was implicitly casting to a string for `v`. --- lib/jekyll-feed/meta-tag.rb | 7 +++++-- spec/jekyll-feed_spec.rb | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/jekyll-feed/meta-tag.rb b/lib/jekyll-feed/meta-tag.rb index 6a47a095..54759297 100644 --- a/lib/jekyll-feed/meta-tag.rb +++ b/lib/jekyll-feed/meta-tag.rb @@ -7,8 +7,11 @@ class MetaTag < Liquid::Tag def render(context) @context = context - attrs = attributes.map { |k, v| %(#{k}=#{v.encode(:xml => :attr)}) }.join(" ") - "" + attrs = attributes.map do |k, v| + v = v.to_s unless v.respond_to?(:encode) + %(#{k}=#{v.encode(:xml => :attr)}) + end + "" end private diff --git a/spec/jekyll-feed_spec.rb b/spec/jekyll-feed_spec.rb index e284dc30..af95ea67 100644 --- a/spec/jekyll-feed_spec.rb +++ b/spec/jekyll-feed_spec.rb @@ -189,6 +189,21 @@ end end + context "with site.title set as a non-string value" do + class MySiteTitle + def to_s + "My Dynamic Site Title <&>" + end + alias_method :to_liquid, :to_s + end + let(:site_title) { MySiteTitle.new } + let(:overrides) { { "title" => site_title } } + + it "ensures the site.title is the string representation of the object" do + expect(feed.title.content).to eql(site_title.to_s.encode(xml: :text)) + end + end + context "with site.name set" do let(:site_name) { "My Site Name" } let(:overrides) { { "name" => site_name } }