cleans up implied property and adds lots of specs for them
This commit is contained in:
parent
3f2a627816
commit
10ea0256fc
15 changed files with 339 additions and 62 deletions
|
@ -1,6 +1,8 @@
|
|||
module Microformats2
|
||||
module ImpliedProperty
|
||||
class Foundation
|
||||
attr_reader :selector
|
||||
|
||||
def initialize(element)
|
||||
@element = element
|
||||
end
|
||||
|
@ -14,7 +16,7 @@ module Microformats2
|
|||
end
|
||||
|
||||
def value
|
||||
@value ||= element_value
|
||||
@value ||= element_value || selector_value
|
||||
end
|
||||
|
||||
def to_hash
|
||||
|
@ -29,15 +31,27 @@ module Microformats2
|
|||
|
||||
def element_value
|
||||
ev = nil
|
||||
attr_map.each_pair do |k, v|
|
||||
selected_elements = @element.css(k)
|
||||
if selected_elements.first
|
||||
ev ||= selected_elements.first.attribute(v).to_s
|
||||
name_map.each_pair do |elname, attr|
|
||||
if elname == @element.name && @element.attribute(attr)
|
||||
ev ||= @element.attribute(attr).to_s
|
||||
@selector ||= elname
|
||||
end
|
||||
end
|
||||
ev
|
||||
end
|
||||
|
||||
def selector_value
|
||||
sv = nil
|
||||
selector_map.each_pair do |sel, attr|
|
||||
selected_elements = @element.css(sel)
|
||||
if selected_elements.first
|
||||
sv ||= selected_elements.first.attribute(attr).to_s
|
||||
@selector ||= sel
|
||||
end
|
||||
end
|
||||
sv
|
||||
end
|
||||
|
||||
def attribute
|
||||
attr_map[@element.name]
|
||||
end
|
||||
|
|
|
@ -7,23 +7,27 @@ module Microformats2
|
|||
end
|
||||
|
||||
def value
|
||||
@value ||= element_value || text_value
|
||||
@value ||= element_value || selector_value || text_value
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def attr_map
|
||||
{ "img[alt]" => "alt",
|
||||
"abbr[title]" => "title",
|
||||
">img[alt]:only-of-type" => "alt",
|
||||
def name_map
|
||||
{ "img" => "alt",
|
||||
"abbr" => "title" }
|
||||
end
|
||||
|
||||
def selector_map
|
||||
{ ">img[alt]:only-of-type" => "alt",
|
||||
">abbr[title]:only-of-type" => "title",
|
||||
">:only-of-type>img[alt]:only-of-type" => "alt",
|
||||
">:only-of-type>abbr[title]:only-of-type" => "title" }
|
||||
">:only-of-type>abbr[title]:only-of-type" => "title" } # only-of-type bit not working
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def text_value
|
||||
@selector = "inner_text"
|
||||
@element.inner_text.gsub(/\n+/, " ").gsub(/\s+/, " ").strip
|
||||
end
|
||||
end
|
||||
|
|
|
@ -8,10 +8,13 @@ module Microformats2
|
|||
|
||||
protected
|
||||
|
||||
def attr_map
|
||||
{ "img[src]" => "src",
|
||||
"object[data]" => "data",
|
||||
">img[src]:only-of-type" => "src",
|
||||
def name_map
|
||||
{ "img" => "src",
|
||||
"object" => "data" }
|
||||
end
|
||||
|
||||
def selector_map
|
||||
{ ">img[src]:only-of-type" => "src",
|
||||
">object[data]:only-of-type" => "data",
|
||||
">:only-child>img[src]:only-of-type" => "src",
|
||||
">:only-child>object[data]:only-of-type" => "data" }
|
||||
|
|
|
@ -8,9 +8,12 @@ module Microformats2
|
|||
|
||||
protected
|
||||
|
||||
def attr_map
|
||||
{ "a[href]" => "href",
|
||||
">a[href]:only-of-type" => "href" }
|
||||
def name_map
|
||||
{ "a" => "href" }
|
||||
end
|
||||
|
||||
def selector_map
|
||||
{ ">a[href]:only-of-type" => "href" }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -132,39 +132,4 @@ describe Microformats2::Collection do
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe ".h-card implied properties simple" do
|
||||
before do
|
||||
html = "spec/support/hcard-implied-simple.html"
|
||||
@collection = Microformats2.parse(html)
|
||||
end
|
||||
|
||||
describe "#parse" do
|
||||
it "assigns .h-card .p-name to HCard#name" do
|
||||
@collection.first.name.first.value.should == "@jlsuttles"
|
||||
end
|
||||
it "assigns .h-card .p-photo to HCard#photo" do
|
||||
@collection.first.photo.first.value.should == "http://gravatar.com/jlsuttles"
|
||||
end
|
||||
it "assigns .h-card .p-url to HCard#url" do
|
||||
@collection.first.url.first.value.should == "http://twitter.com/jlsuttles"
|
||||
end
|
||||
end
|
||||
|
||||
describe "#to_hash" do
|
||||
it "returns the correct Hash" do
|
||||
hash = {
|
||||
:items => [{
|
||||
:type => ["h-card"],
|
||||
:properties => {
|
||||
:name => ["@jlsuttles"],
|
||||
:photo => ["http://gravatar.com/jlsuttles"],
|
||||
:url => ["http://twitter.com/jlsuttles"]
|
||||
}
|
||||
}]
|
||||
}
|
||||
@collection.to_hash.should == hash
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
29
spec/lib/microformats2/implied_property/name_spec.rb
Normal file
29
spec/lib/microformats2/implied_property/name_spec.rb
Normal file
|
@ -0,0 +1,29 @@
|
|||
require "spec_helper"
|
||||
require "microformats2"
|
||||
|
||||
describe Microformats2::ImpliedProperty::Name do
|
||||
describe "name-pass.html" do
|
||||
html = "spec/support/lib/microformats2/implied_property/name-pass.html"
|
||||
collection = Microformats2.parse(html)
|
||||
it "should have the correct number of formats" do
|
||||
collection.all.length.should == 6
|
||||
end
|
||||
collection.all.each_with_index do |format, index|
|
||||
it "passes case #{index+1}" do
|
||||
format.name.first.value.should == "Jessica"
|
||||
end
|
||||
end
|
||||
end
|
||||
describe "name-fail.html" do
|
||||
html = "spec/support/lib/microformats2/implied_property/name-fail.html"
|
||||
collection = Microformats2.parse(html)
|
||||
it "should have the correct number of formats" do
|
||||
collection.all.length.should == 8
|
||||
end
|
||||
collection.all.each_with_index do |format, index|
|
||||
it "fails case #{index+1}" do
|
||||
format.name.first.value.should == ""
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
29
spec/lib/microformats2/implied_property/photo_spec.rb
Normal file
29
spec/lib/microformats2/implied_property/photo_spec.rb
Normal file
|
@ -0,0 +1,29 @@
|
|||
require "spec_helper"
|
||||
require "microformats2"
|
||||
|
||||
describe Microformats2::ImpliedProperty::Photo do
|
||||
describe "photo-pass.html" do
|
||||
html = "spec/support/lib/microformats2/implied_property/photo-pass.html"
|
||||
collection = Microformats2.parse(html)
|
||||
it "should have the correct number of formats" do
|
||||
collection.all.length.should == 6
|
||||
end
|
||||
collection.all.each_with_index do |format, index|
|
||||
it "passes case #{index+1}" do
|
||||
format.photo.first.value.should == "http://gravatar.com/jlsuttles"
|
||||
end
|
||||
end
|
||||
end
|
||||
describe "photo-fail.html" do
|
||||
html = "spec/support/lib/microformats2/implied_property/photo-fail.html"
|
||||
collection = Microformats2.parse(html)
|
||||
it "should have the correct number of formats" do
|
||||
collection.all.length.should == 8
|
||||
end
|
||||
collection.all.each_with_index do |format, index|
|
||||
it "fails case #{index+1}" do
|
||||
format.photo.should be_nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
29
spec/lib/microformats2/implied_property/url_spec.rb
Normal file
29
spec/lib/microformats2/implied_property/url_spec.rb
Normal file
|
@ -0,0 +1,29 @@
|
|||
require "spec_helper"
|
||||
require "microformats2"
|
||||
|
||||
describe Microformats2::ImpliedProperty::Url do
|
||||
describe "url-pass.html" do
|
||||
html = "spec/support/lib/microformats2/implied_property/url-pass.html"
|
||||
collection = Microformats2.parse(html)
|
||||
it "should have the correct number of formats" do
|
||||
collection.all.length.should == 2
|
||||
end
|
||||
collection.all.each_with_index do |format, index|
|
||||
it "passes case #{index+1}" do
|
||||
format.url.first.value.should == "http://github.com/jlsuttles"
|
||||
end
|
||||
end
|
||||
end
|
||||
describe "url-fail.html" do
|
||||
html = "spec/support/lib/microformats2/implied_property/url-fail.html"
|
||||
collection = Microformats2.parse(html)
|
||||
it "should have the correct number of formats" do
|
||||
collection.all.length.should == 2
|
||||
end
|
||||
collection.all.each_with_index do |format, index|
|
||||
it "fails case #{index+1}" do
|
||||
format.url.should be_nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,9 +0,0 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<body>
|
||||
<div class="h-card">
|
||||
<a href="http://twitter.com/jlsuttles">@jlsuttles</a>
|
||||
<img src="http://gravatar.com/jlsuttles">
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,60 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<body>
|
||||
|
||||
<div class="h-card">
|
||||
<img alt="Jessica">
|
||||
<img alt="Shane">
|
||||
</div>
|
||||
|
||||
<div class="h-card">
|
||||
<abbr title="Jessica" />
|
||||
<abbr title="Shane" />
|
||||
</div>
|
||||
|
||||
<div class="h-card">
|
||||
<div>
|
||||
<img alt="Jessica">
|
||||
<img alt="Shane">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="h-card">
|
||||
<div></div>
|
||||
<div>
|
||||
<img alt="Jessica">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="h-card">
|
||||
<div>
|
||||
<abbr title="Jessica" />
|
||||
<abbr title="Shane" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="h-card">
|
||||
<div></div>
|
||||
<div>
|
||||
<abbr title="Jessica" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="h-card">
|
||||
<div>
|
||||
<div>
|
||||
<img alt="Jessica">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="h-card">
|
||||
<div>
|
||||
<div>
|
||||
<abbr title="Jessica" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,30 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<body>
|
||||
|
||||
<img class="h-card" alt="Jessica">
|
||||
|
||||
<abbr class="h-card" title="Jessica" />
|
||||
|
||||
<div class="h-card">
|
||||
<img alt="Jessica">
|
||||
</div>
|
||||
|
||||
<div class="h-card">
|
||||
<abbr title="Jessica" />
|
||||
</div>
|
||||
|
||||
<div class="h-card">
|
||||
<div>
|
||||
<img alt="Jessica">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="h-card">
|
||||
<div>
|
||||
<abbr title="Jessica" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,60 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<body>
|
||||
|
||||
<div class="h-card">
|
||||
<img src="http://gravatar.com/jlsuttles">
|
||||
<img src="http://gravatar.com/veganstraightedge">
|
||||
</div>
|
||||
|
||||
<div class="h-card">
|
||||
<object data="http://gravatar.com/jlsuttles" />
|
||||
<object data="http://gravatar.com/veganstraightedge" />
|
||||
</div>
|
||||
|
||||
<div class="h-card">
|
||||
<div>
|
||||
<img src="http://gravatar.com/jlsuttles">
|
||||
<img src="http://gravatar.com/veganstraightedge">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="h-card">
|
||||
<div></div>
|
||||
<div>
|
||||
<img src="http://gravatar.com/jlsuttles">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="h-card">
|
||||
<div>
|
||||
<object data="http://gravatar.com/jlsuttles" />
|
||||
<object data="http://gravatar.com/veganstraightedge" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="h-card">
|
||||
<div></div>
|
||||
<div>
|
||||
<object data="http://gravatar.com/jlsuttles" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="h-card">
|
||||
<div>
|
||||
<div>
|
||||
<img src="http://gravatar.com/jlsuttles">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="h-card">
|
||||
<div>
|
||||
<div>
|
||||
<object data="http://gravatar.com/jlsuttles" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,30 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<body>
|
||||
|
||||
<img class="h-card" src="http://gravatar.com/jlsuttles">
|
||||
|
||||
<object class="h-card" data="http://gravatar.com/jlsuttles" />
|
||||
|
||||
<div class="h-card">
|
||||
<img src="http://gravatar.com/jlsuttles">
|
||||
</div>
|
||||
|
||||
<div class="h-card">
|
||||
<object data="http://gravatar.com/jlsuttles" />
|
||||
</div>
|
||||
|
||||
<div class="h-card">
|
||||
<div>
|
||||
<img src="http://gravatar.com/jlsuttles">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="h-card">
|
||||
<div>
|
||||
<object data="http://gravatar.com/jlsuttles" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,18 @@
|
|||
</html>
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<body>
|
||||
|
||||
<div class="h-card">
|
||||
<a href="http://github.com/jlsuttles" />
|
||||
<a href="http://github.com/veganstraightedge" />
|
||||
</div>
|
||||
|
||||
<div class="h-card">
|
||||
<div>
|
||||
<a href="http://github.com/jlsuttles" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,12 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<body>
|
||||
|
||||
<a class="h-card" href="http://github.com/jlsuttles" />
|
||||
|
||||
<div class="h-card">
|
||||
<a href="http://github.com/jlsuttles" />
|
||||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
Loading…
Add table
Add a link
Reference in a new issue