From 999bc49e2d7a39c1027ef01c174dfa78b0163eb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Thu, 1 Feb 2024 16:45:58 +0200 Subject: [PATCH] Handle empty manifest values Fall back to HTML if possible, discard otherwise --- lib/manifique/metadata.rb | 2 +- .../fixtures/petrolette-web-app-manifest.json | 19 + spec/fixtures/petrolette.html | 582 ++++++++++++++++++ spec/manifique/web_client_spec.rb | 36 +- 4 files changed, 636 insertions(+), 3 deletions(-) create mode 100644 spec/fixtures/petrolette-web-app-manifest.json create mode 100644 spec/fixtures/petrolette.html diff --git a/lib/manifique/metadata.rb b/lib/manifique/metadata.rb index fd0403b..cfcb97a 100644 --- a/lib/manifique/metadata.rb +++ b/lib/manifique/metadata.rb @@ -17,7 +17,7 @@ module Manifique [ :name, :short_name, :description, :icons, :theme_color, :background_color, :display, :start_url, :scope, :share_target ].map(&:to_s).each do |prop| - next unless manifest[prop] + next unless manifest[prop] && !manifest[prop].to_s.empty? self.send("#{prop}=", manifest[prop]) self.from_web_manifest.add(prop) end diff --git a/spec/fixtures/petrolette-web-app-manifest.json b/spec/fixtures/petrolette-web-app-manifest.json new file mode 100644 index 0000000..f844435 --- /dev/null +++ b/spec/fixtures/petrolette-web-app-manifest.json @@ -0,0 +1,19 @@ +{ + "name": "", + "short_name": "", + "icons": [ + { + "src": "../../../../public/images/favicons/android-chrome-192x192.png", + "sizes": "192x192", + "type": "image/png" + }, + { + "src": "../../../../public/images/favicons/android-chrome-256x256.png", + "sizes": "256x256", + "type": "image/png" + } + ], + "theme_color": "#ffffff", + "background_color": "#ffffff", + "display": "standalone" +} diff --git a/spec/fixtures/petrolette.html b/spec/fixtures/petrolette.html new file mode 100644 index 0000000..7e9d72c --- /dev/null +++ b/spec/fixtures/petrolette.html @@ -0,0 +1,582 @@ + + + + + Pétrolette + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+
    + +
+
+ + + +
+

Pétrolette

+

...Needs DOM Local Storage to work.

+

+ Please enable it in your browser : read about local storage on MDN. +

+
+ +
+ + + + + +
+ + + + + + + + + + + + + + + + diff --git a/spec/manifique/web_client_spec.rb b/spec/manifique/web_client_spec.rb index 3c7d7ff..8426058 100644 --- a/spec/manifique/web_client_spec.rb +++ b/spec/manifique/web_client_spec.rb @@ -165,9 +165,9 @@ RSpec.describe Manifique::WebClient do end describe "#fetch_metadata" do - let(:web_client) { Manifique::WebClient.new(url: "https://kosmos.social/") } - context "web app manifest present" do + let(:web_client) { Manifique::WebClient.new(url: "https://kosmos.social/") } + before do index_html = File.read(File.join(__dir__, "..", "fixtures", "mastodon.html")); stub_request(:get, "https://kosmos.social/"). @@ -207,7 +207,10 @@ RSpec.describe Manifique::WebClient do end end + context "no web app manifest present" do + let(:web_client) { Manifique::WebClient.new(url: "https://kosmos.social/") } + before do index_html = File.read(File.join(__dir__, "..", "fixtures", "mastodon-no-manifest.html")); stub_request(:get, "https://kosmos.social/"). @@ -257,6 +260,8 @@ RSpec.describe Manifique::WebClient do end context "with data URL icons" do + let(:web_client) { Manifique::WebClient.new(url: "https://kosmos.social/") } + before do index_html = File.read(File.join(__dir__, "..", "fixtures", "kommit.html")); stub_request(:get, "https://kosmos.social/"). @@ -287,6 +292,33 @@ RSpec.describe Manifique::WebClient do expect(apple_touch_icons.first["sizes"]).to be_nil end end + + context "empty values in manifest" do + let(:web_client) { Manifique::WebClient.new(url: "https://petrolette.space/") } + + before do + index_html = File.read(File.join(__dir__, "..", "fixtures", "petrolette.html")); + stub_request(:get, "https://petrolette.space/"). + to_return(body: index_html, status: 200, headers: { + "Content-Type": "text/html; charset=utf-8" + }) + manifest = File.read(File.join(__dir__, "..", "fixtures", "petrolette-web-app-manifest.json")); + stub_request(:get, "https://petrolette.space/static/images/favicons/site.webmanifest"). + to_return(body: manifest, status: 200, headers: { + "Content-Type": "application/manifest+json" + }) + end + + subject { web_client.fetch_metadata } + + it "loads empty manifest values from HTML" do + expect(subject.name).to eq("Pétrolette") + end + + it "discards empty manifest values with no HTML equivalent" do + expect(subject.short_name).to be_nil + end + end end end