Refactor icon parsing, add apple touch icons
This commit is contained in:
parent
87baa5960c
commit
44bf2404f6
@ -92,6 +92,12 @@ module Manifique
|
|||||||
end
|
end
|
||||||
|
|
||||||
def parse_icons_from_html
|
def parse_icons_from_html
|
||||||
|
parse_link_icons_from_html
|
||||||
|
parse_apple_touch_icons_from_html
|
||||||
|
parse_mask_icon_from_html
|
||||||
|
end
|
||||||
|
|
||||||
|
def parse_link_icons_from_html
|
||||||
if icon_links = @html.css("link[rel=icon]")
|
if icon_links = @html.css("link[rel=icon]")
|
||||||
icon_links.each do |link|
|
icon_links.each do |link|
|
||||||
icon = {}
|
icon = {}
|
||||||
@ -100,9 +106,26 @@ module Manifique
|
|||||||
icon["sizes"] = link.attributes["sizes"].value rescue nil
|
icon["sizes"] = link.attributes["sizes"].value rescue nil
|
||||||
icon["type"] = link.attributes["type"].value rescue get_icon_type(icon["src"])
|
icon["type"] = link.attributes["type"].value rescue get_icon_type(icon["src"])
|
||||||
@metadata.icons.push icon
|
@metadata.icons.push icon
|
||||||
|
@metadata.from_html.add "icons"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def parse_apple_touch_icons_from_html
|
||||||
|
if icon_links = @html.css("link[rel=apple-touch-icon]")
|
||||||
|
icon_links.each do |link|
|
||||||
|
icon = { "purpose" => "apple-touch-icon" }
|
||||||
|
icon["src"] = link.attributes["href"].value rescue nil
|
||||||
|
next if icon["src"].to_s.empty?
|
||||||
|
icon["sizes"] = link.attributes["sizes"].value rescue nil
|
||||||
|
icon["type"] = link.attributes["type"].value rescue get_icon_type(icon["src"])
|
||||||
|
@metadata.icons.push icon
|
||||||
|
@metadata.from_html.add "icons"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def parse_mask_icon_from_html
|
||||||
if mask_icon_link = @html.at_css("link[rel=mask-icon]")
|
if mask_icon_link = @html.at_css("link[rel=mask-icon]")
|
||||||
icon = { "purpose" => "mask-icon" }
|
icon = { "purpose" => "mask-icon" }
|
||||||
icon["src"] = mask_icon_link.attributes["href"].value rescue nil
|
icon["src"] = mask_icon_link.attributes["href"].value rescue nil
|
||||||
@ -110,6 +133,7 @@ module Manifique
|
|||||||
icon["type"] = link.attributes["type"].value rescue get_icon_type(icon["src"])
|
icon["type"] = link.attributes["type"].value rescue get_icon_type(icon["src"])
|
||||||
icon["color"] = mask_icon_link.attributes["color"].value rescue nil
|
icon["color"] = mask_icon_link.attributes["color"].value rescue nil
|
||||||
@metadata.icons.push icon
|
@metadata.icons.push icon
|
||||||
|
@metadata.from_html.add "icons"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
1
spec/fixtures/mastodon-no-manifest.html
vendored
1
spec/fixtures/mastodon-no-manifest.html
vendored
@ -6,6 +6,7 @@
|
|||||||
<meta name="description" content="A friendly place for tooting">
|
<meta name="description" content="A friendly place for tooting">
|
||||||
<link href='/favicon.ico' rel='icon' type='image/x-icon'>
|
<link href='/favicon.ico' rel='icon' type='image/x-icon'>
|
||||||
<link href='/apple-touch-icon.png' rel='apple-touch-icon' sizes='180x180'>
|
<link href='/apple-touch-icon.png' rel='apple-touch-icon' sizes='180x180'>
|
||||||
|
<link href='/apple-touch-icon-57px.png' rel='apple-touch-icon' sizes='57x57'>
|
||||||
<link color='#2b90d9' href='/mask-icon.svg' rel='mask-icon'>
|
<link color='#2b90d9' href='/mask-icon.svg' rel='mask-icon'>
|
||||||
<meta content='/browserconfig.xml' name='msapplication-config'>
|
<meta content='/browserconfig.xml' name='msapplication-config'>
|
||||||
<meta content='#282c37' name='theme-color'>
|
<meta content='#282c37' name='theme-color'>
|
||||||
|
@ -160,16 +160,31 @@ RSpec.describe Manifique::WebClient do
|
|||||||
|
|
||||||
subject { web_client.fetch_metadata }
|
subject { web_client.fetch_metadata }
|
||||||
|
|
||||||
it "returns a metadata object with the HTML properties loaded" do
|
it "returns a metadata object" do
|
||||||
expect(subject).to be_kind_of(Manifique::Metadata)
|
expect(subject).to be_kind_of(Manifique::Metadata)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "loads properties from parsed HTML" do
|
||||||
expect(subject.name).to eq("kosmos.social")
|
expect(subject.name).to eq("kosmos.social")
|
||||||
expect(subject.description).to eq("A friendly place for tooting")
|
expect(subject.description).to eq("A friendly place for tooting")
|
||||||
expect(subject.theme_color).to eq("#282c37")
|
expect(subject.theme_color).to eq("#282c37")
|
||||||
expect(subject.display).to eq("standalone")
|
expect(subject.display).to eq("standalone")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "loads icons from link[rel=icon] elements" do
|
||||||
png_icons = subject.icons.select{|i| i["type"] == "image/png"}
|
png_icons = subject.icons.select{|i| i["type"] == "image/png"}
|
||||||
expect(png_icons.length).to eq(5)
|
expect(png_icons.length).to eq(7)
|
||||||
expect(subject.icons.find{|i| i["sizes"] == "512x512"}["src"]).to eq( "/application_icon_x512.png")
|
expect(subject.icons.find{|i| i["sizes"] == "512x512"}["src"]).to eq( "/application_icon_x512.png")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "loads icons from link[rel=apple-touch-icon] elements" do
|
||||||
|
apple_touch_icons = subject.icons.select{|i| i["purpose"] == "apple-touch-icon"}
|
||||||
|
expect(apple_touch_icons.length).to eq(2)
|
||||||
|
expect(apple_touch_icons.first["type"]).to eq("image/png")
|
||||||
|
expect(apple_touch_icons.first["sizes"]).to eq("180x180")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "loads mask icons from link[rel=mask-icon] elements" do
|
||||||
mask_icon = subject.icons.find{|i| i["purpose"] == "mask-icon"}
|
mask_icon = subject.icons.find{|i| i["purpose"] == "mask-icon"}
|
||||||
expect(mask_icon["color"]).to eq("#2b90d9")
|
expect(mask_icon["color"]).to eq("#2b90d9")
|
||||||
expect(mask_icon["type"]).to eq("image/svg")
|
expect(mask_icon["type"]).to eq("image/svg")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user