diff --git a/lib/microformats2/implied_property/foundation.rb b/lib/microformats2/implied_property/foundation.rb index d67181a..ab49af9 100644 --- a/lib/microformats2/implied_property/foundation.rb +++ b/lib/microformats2/implied_property/foundation.rb @@ -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 diff --git a/lib/microformats2/implied_property/name.rb b/lib/microformats2/implied_property/name.rb index 4b43bcc..766c7d6 100644 --- a/lib/microformats2/implied_property/name.rb +++ b/lib/microformats2/implied_property/name.rb @@ -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 diff --git a/lib/microformats2/implied_property/photo.rb b/lib/microformats2/implied_property/photo.rb index ebd4d45..2dc2676 100644 --- a/lib/microformats2/implied_property/photo.rb +++ b/lib/microformats2/implied_property/photo.rb @@ -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" } diff --git a/lib/microformats2/implied_property/url.rb b/lib/microformats2/implied_property/url.rb index 71b218d..1fb3af0 100644 --- a/lib/microformats2/implied_property/url.rb +++ b/lib/microformats2/implied_property/url.rb @@ -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 diff --git a/spec/lib/microformats2/collection_spec.rb b/spec/lib/microformats2/collection_spec.rb index af525be..a8c9419 100644 --- a/spec/lib/microformats2/collection_spec.rb +++ b/spec/lib/microformats2/collection_spec.rb @@ -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 diff --git a/spec/lib/microformats2/implied_property/name_spec.rb b/spec/lib/microformats2/implied_property/name_spec.rb new file mode 100644 index 0000000..7241406 --- /dev/null +++ b/spec/lib/microformats2/implied_property/name_spec.rb @@ -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 diff --git a/spec/lib/microformats2/implied_property/photo_spec.rb b/spec/lib/microformats2/implied_property/photo_spec.rb new file mode 100644 index 0000000..0636934 --- /dev/null +++ b/spec/lib/microformats2/implied_property/photo_spec.rb @@ -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 diff --git a/spec/lib/microformats2/implied_property/url_spec.rb b/spec/lib/microformats2/implied_property/url_spec.rb new file mode 100644 index 0000000..a729093 --- /dev/null +++ b/spec/lib/microformats2/implied_property/url_spec.rb @@ -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 diff --git a/spec/support/hcard-implied-simple.html b/spec/support/hcard-implied-simple.html deleted file mode 100644 index a3fdc8c..0000000 --- a/spec/support/hcard-implied-simple.html +++ /dev/null @@ -1,9 +0,0 @@ - - - -
- @jlsuttles - -
- - diff --git a/spec/support/lib/microformats2/implied_property/name-fail.html b/spec/support/lib/microformats2/implied_property/name-fail.html new file mode 100644 index 0000000..45bb51a --- /dev/null +++ b/spec/support/lib/microformats2/implied_property/name-fail.html @@ -0,0 +1,60 @@ + + + + +
+ Jessica + Shane +
+ +
+ + +
+ +
+
+ Jessica + Shane +
+
+ +
+
+
+ Jessica +
+
+ +
+
+ + +
+
+ +
+
+
+ +
+
+ +
+
+
+ Jessica +
+
+
+ +
+
+
+ +
+
+
+ + + diff --git a/spec/support/lib/microformats2/implied_property/name-pass.html b/spec/support/lib/microformats2/implied_property/name-pass.html new file mode 100644 index 0000000..b2c5c7d --- /dev/null +++ b/spec/support/lib/microformats2/implied_property/name-pass.html @@ -0,0 +1,30 @@ + + + + +Jessica + + + +
+ Jessica +
+ +
+ +
+ +
+
+ Jessica +
+
+ +
+
+ +
+
+ + + diff --git a/spec/support/lib/microformats2/implied_property/photo-fail.html b/spec/support/lib/microformats2/implied_property/photo-fail.html new file mode 100644 index 0000000..c44fa98 --- /dev/null +++ b/spec/support/lib/microformats2/implied_property/photo-fail.html @@ -0,0 +1,60 @@ + + + + +
+ + +
+ +
+ + + + +
+
+ + +
+
+ +
+
+
+ +
+
+ +
+
+ + + + + +
+
+
+ + + + +
+
+
+ +
+
+
+ +
+
+
+ + + + + + + diff --git a/spec/support/lib/microformats2/implied_property/photo-pass.html b/spec/support/lib/microformats2/implied_property/photo-pass.html new file mode 100644 index 0000000..d38a71e --- /dev/null +++ b/spec/support/lib/microformats2/implied_property/photo-pass.html @@ -0,0 +1,30 @@ + + + + + + + + +
+ +
+ +
+ + + +
+
+ +
+
+ +
+
+ + + + + + diff --git a/spec/support/lib/microformats2/implied_property/url-fail.html b/spec/support/lib/microformats2/implied_property/url-fail.html new file mode 100644 index 0000000..e2c5117 --- /dev/null +++ b/spec/support/lib/microformats2/implied_property/url-fail.html @@ -0,0 +1,18 @@ + + + + + + + + + + + diff --git a/spec/support/lib/microformats2/implied_property/url-pass.html b/spec/support/lib/microformats2/implied_property/url-pass.html new file mode 100644 index 0000000..7150f14 --- /dev/null +++ b/spec/support/lib/microformats2/implied_property/url-pass.html @@ -0,0 +1,12 @@ + + + + + + + + + +