Bug 1037483 replace microdata with microformats in share, r=dietrich

This commit is contained in:
Shane Caraveo 2016-01-29 10:58:45 -08:00
parent 5312410492
commit 7cc680d82c
9 changed files with 99 additions and 192 deletions

View File

@ -639,17 +639,17 @@ SocialShare = {
}
this.sharePage(providerOrigin, pageData, target, anchor);
});
gBrowser.selectedBrowser.messageManager.sendAsyncMessage("PageMetadata:GetPageData");
gBrowser.selectedBrowser.messageManager.sendAsyncMessage("PageMetadata:GetPageData", null, { target });
return;
}
// if this is a share of a selected item, get any microdata
if (!pageData.microdata && target) {
messageManager.addMessageListener("PageMetadata:MicrodataResult", _dataFn = (msg) => {
messageManager.removeMessageListener("PageMetadata:MicrodataResult", _dataFn);
pageData.microdata = msg.data;
// if this is a share of a selected item, get any microformats
if (!pageData.microformats && target) {
messageManager.addMessageListener("PageMetadata:MicroformatsResult", _dataFn = (msg) => {
messageManager.removeMessageListener("PageMetadata:MicroformatsResult", _dataFn);
pageData.microformats = msg.data;
this.sharePage(providerOrigin, pageData, target, anchor);
});
gBrowser.selectedBrowser.messageManager.sendAsyncMessage("PageMetadata:GetMicrodata", null, { target });
gBrowser.selectedBrowser.messageManager.sendAsyncMessage("PageMetadata:GetMicroformats", null, { target });
return;
}
this.currentShare = pageData;

View File

@ -715,20 +715,20 @@ addEventListener("pageshow", function(event) {
var PageMetadataMessenger = {
init() {
addMessageListener("PageMetadata:GetPageData", this);
addMessageListener("PageMetadata:GetMicrodata", this);
addMessageListener("PageMetadata:GetMicroformats", this);
},
receiveMessage(message) {
switch(message.name) {
case "PageMetadata:GetPageData": {
let result = PageMetadata.getData(content.document);
let target = message.objects.target;
let result = PageMetadata.getData(content.document, target);
sendAsyncMessage("PageMetadata:PageDataResult", result);
break;
}
case "PageMetadata:GetMicrodata": {
case "PageMetadata:GetMicroformats": {
let target = message.objects.target;
let result = PageMetadata.getMicrodata(content.document, target);
sendAsyncMessage("PageMetadata:MicrodataResult", result);
let result = PageMetadata.getMicroformats(content.document, target);
sendAsyncMessage("PageMetadata:MicroformatsResult", result);
break;
}
}

View File

@ -164,17 +164,17 @@
messageManager.removeMessageListener("PageMetadata:PageDataResult", _dataFn);
this.loadPanel(msg.json, target);
});
gBrowser.selectedBrowser.messageManager.sendAsyncMessage("PageMetadata:GetPageData");
gBrowser.selectedBrowser.messageManager.sendAsyncMessage("PageMetadata:GetPageData", null, { target });
return;
}
// if this is a share of a selected item, get any microdata
if (!pageData.microdata && target) {
messageManager.addMessageListener("PageMetadata:MicrodataResult", _dataFn = (msg) => {
messageManager.removeMessageListener("PageMetadata:MicrodataResult", _dataFn);
pageData.microdata = msg.data;
// if this is a share of a selected item, get any microformats
if (!pageData.microformats && target) {
messageManager.addMessageListener("PageMetadata:MicroformatsResult", _dataFn = (msg) => {
messageManager.removeMessageListener("PageMetadata:MicroformatsResult", _dataFn);
pageData.microformats = msg.data;
this.loadPanel(pageData, target);
});
gBrowser.selectedBrowser.messageManager.sendAsyncMessage("PageMetadata:GetMicrodata", null, { target });
gBrowser.selectedBrowser.messageManager.sendAsyncMessage("PageMetadata:GetMicroformats", null, { target });
return;
}
this.pageData = pageData;

View File

@ -9,7 +9,7 @@ support-files =
opengraph/shortlink_linkrel.html
opengraph/shorturl_link.html
opengraph/shorturl_linkrel.html
microdata.html
microformats.html
share.html
share_activate.html
social_activate.html

View File

@ -205,53 +205,53 @@ var tests = {
port.postMessage({topic: "test-init"});
executeSoon(runOneTest);
},
testShareMicrodata: function(next) {
testShareMicroformats: function(next) {
SocialService.addProvider(manifest, function(provider) {
let port = provider.getWorkerPort();
let target, testTab;
let expecting = JSON.stringify({
"url": "https://example.com/browser/browser/base/content/test/social/microdata.html",
"title": "My Blog",
"previews": [],
"microdata": {
"url": "https://example.com/browser/browser/base/content/test/social/microformats.html",
"title": "Raspberry Pi Page",
"previews": ["https://example.com/someimage.jpg"],
"microformats": {
"items": [{
"types": ["http://schema.org/BlogPosting"],
"type": ["h-product"],
"properties": {
"headline": ["Progress report"],
"datePublished": ["2013-08-29"],
"url": ["https://example.com/browser/browser/base/content/test/social/microdata.html?comments=0"],
"comment": [{
"types": ["http://schema.org/UserComments"],
"name": ["Raspberry Pi"],
"photo": ["https://example.com/someimage.jpg"],
"description": [{
"value": "The Raspberry Pi is a credit-card sized computer that plugs into your TV and a keyboard. It's a capable little PC which can be used for many of the things that your desktop PC does, like spreadsheets, word-processing and games. It also plays high-definition video. We want to see it being used by kids all over the world to learn programming.",
"html": "The Raspberry Pi is a credit-card sized computer that plugs into your TV and a keyboard. It's a capable little PC which can be used for many of the things that your desktop PC does, like spreadsheets, word-processing and games. It also plays high-definition video. We want to see it being used by kids all over the world to learn programming."
}
],
"url": ["https://example.com/"],
"price": ["£29.95"],
"review": [{
"value": "4.5 out of 5",
"type": ["h-review"],
"properties": {
"url": ["https://example.com/browser/browser/base/content/test/social/microdata.html#c1"],
"creator": [{
"types": ["http://schema.org/Person"],
"properties": {
"name": ["Greg"]
}
}
],
"commentTime": ["2013-08-29"]
}
}, {
"types": ["http://schema.org/UserComments"],
"properties": {
"url": ["https://example.com/browser/browser/base/content/test/social/microdata.html#c2"],
"creator": [{
"types": ["http://schema.org/Person"],
"properties": {
"name": ["Charlotte"]
}
}
],
"commentTime": ["2013-08-29"]
"rating": ["4.5"]
}
}
]
],
"category": ["Computer", "Education"]
}
}
]
],
"rels": {
"tag": ["https://example.com/wiki/computer", "https://example.com/wiki/education"]
},
"rel-urls": {
"https://example.com/wiki/computer": {
"text": "Computer",
"rels": ["tag"]
},
"https://example.com/wiki/education": {
"text": "Education",
"rels": ["tag"]
}
}
}
});
@ -259,15 +259,15 @@ var tests = {
let topic = e.data.topic;
switch (topic) {
case "got-share-data-message":
is(JSON.stringify(e.data.result), expecting, "microdata data ok");
is(JSON.stringify(e.data.result), expecting, "microformats data ok");
gBrowser.removeTab(testTab);
SocialService.disableProvider(manifest.origin, next);
break;
}
}
port.postMessage({topic: "test-init"});
let url = "https://example.com/browser/browser/base/content/test/social/microdata.html"
let url = "https://example.com/browser/browser/base/content/test/social/microformats.html"
addTab(url, function(tab) {
testTab = tab;
let doc = tab.linkedBrowser.contentDocument;

View File

@ -27,32 +27,26 @@ function test() {
}
var tests = {
testMarkMicrodata: function(next) {
testMarkMicroformats: function(next) {
// emulates context menu action using target element, calling SocialMarks.markLink
let provider = Social._getProviderFromOrigin(manifest.origin);
let port = provider.getWorkerPort();
let target, testTab;
// browser_share tests microdata on the full page, this is testing a
// browser_share tests microformats on the full page, this is testing a
// specific target element.
let expecting = JSON.stringify({
"url": "https://example.com/browser/browser/base/content/test/social/microdata.html",
"microdata": {
"url": "https://example.com/browser/browser/base/content/test/social/microformats.html",
"microformats": {
"items": [{
"types": ["http://schema.org/UserComments"],
"type": ["h-review"],
"properties": {
"url": ["https://example.com/browser/browser/base/content/test/social/microdata.html#c2"],
"creator": [{
"types": ["http://schema.org/Person"],
"properties": {
"name": ["Charlotte"]
}
}
],
"commentTime": ["2013-08-29"]
"rating": ["4.5"]
}
}
]
],
"rels": {},
"rel-urls": {}
}
});
@ -60,7 +54,7 @@ var tests = {
let topic = e.data.topic;
switch (topic) {
case "got-share-data-message":
is(JSON.stringify(e.data.result), expecting, "microdata data ok");
is(JSON.stringify(e.data.result), expecting, "microformats data ok");
gBrowser.removeTab(testTab);
port.close();
next();
@ -69,11 +63,11 @@ var tests = {
}
port.postMessage({topic: "test-init"});
let url = "https://example.com/browser/browser/base/content/test/social/microdata.html"
let url = "https://example.com/browser/browser/base/content/test/social/microformats.html"
addTab(url, function(tab) {
testTab = tab;
let doc = tab.linkedBrowser.contentDocument;
target = doc.getElementById("test-comment");
target = doc.getElementById("test-review");
SocialMarks.markLink(manifest.origin, url, target);
});
}

View File

@ -1,38 +0,0 @@
<!DOCTYPE HTML>
<head>
<title>My Blog</title>
</head>
<body>
<article itemscope itemtype="http://schema.org/BlogPosting">
<header>
<h1 itemprop="headline">Progress report</h1>
<p><time itemprop="datePublished" datetime="2013-08-29">today</time></p>
<link itemprop="url" href="?comments=0">
</header>
<p>All in all, he's doing well with his swim lessons. The biggest thing was he had trouble
putting his head in, but we got it down.</p>
<section>
<h1>Comments</h1>
<article itemprop="comment" itemscope itemtype="http://schema.org/UserComments" id="c1">
<link itemprop="url" href="#c1">
<footer>
<p>Posted by: <span itemprop="creator" itemscope itemtype="http://schema.org/Person">
<span itemprop="name">Greg</span>
</span></p>
<p><time itemprop="commentTime" datetime="2013-08-29">15 minutes ago</time></p>
</footer>
<p>Ha!</p>
</article>
<article id="test-comment" itemprop="comment" itemscope itemtype="http://schema.org/UserComments" id="c2">
<link itemprop="url" href="#c2">
<footer>
<p>Posted by: <span itemprop="creator" itemscope itemtype="http://schema.org/Person">
<span itemprop="name">Charlotte</span>
</span></p>
<p><time itemprop="commentTime" datetime="2013-08-29">5 minutes ago</time></p>
</footer>
<p>When you say "we got it down"...</p>
</article>
</section>
</article>
</body>

View File

@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<body>
<head><title>Raspberry Pi Page</title></head>
<div class="hproduct">
<h2 class="fn">Raspberry Pi</h2>
<img class="photo" src="https://example.com/someimage.jpg" />
<p class="description">The Raspberry Pi is a credit-card sized computer that plugs into your TV and a keyboard. It's a capable little PC which can be used for many of the things that your desktop PC does, like spreadsheets, word-processing and games. It also plays high-definition video. We want to see it being used by kids all over the world to learn programming.</p>
<a class="url" href="https://example.com/">More info about the Raspberry Pi</a>
<p class="price">£29.95</p>
<p class="review hreview"><span id="test-review" class="rating">4.5</span> out of 5</p>
<p>Categories:
<a rel="tag" href="https://example.com/wiki/computer" class="category">Computer</a>,
<a rel="tag" href="https://example.com/wiki/education" class="category">Education</a>
</p>
</div>
</body>
</html>

View File

@ -10,6 +10,7 @@ const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/microformat-shiv.js");
XPCOMUtils.defineLazyServiceGetter(this, "UnescapeService",
"@mozilla.org/feed-unescapehtml;1",
@ -25,7 +26,7 @@ const DISCOVER_IMAGES_MAX = 5;
/**
* Extract metadata and microdata from a HTML document.
* Extract metadata and microformats from a HTML document.
* @type {Object}
*/
this.PageMetadata = {
@ -36,14 +37,14 @@ this.PageMetadata = {
* - Metadata specified in <meta> tags, including OpenGraph data
* - Links specified in <link> tags (short, canonical, preview images, alternative)
* - Content that can be found in the page content that we consider useful metadata
* - Microdata, as defined by the spec:
* http://www.whatwg.org/specs/web-apps/current-work/multipage/microdata-2.html
* - Microformats
*
* @param {Document} document - Document to extract data from.
* @param {Element} [target] - Optional element to restrict microformats lookup to.
* @returns {Object} Object containing the various metadata, normalized to
* merge some common alternative names for metadata.
*/
getData(document) {
getData(document, target = null) {
let result = {
url: this._validateURL(document, document.documentURI),
title: document.title,
@ -68,84 +69,16 @@ this.PageMetadata = {
this._getMetaData(document, result);
this._getLinkData(document, result);
this._getPageData(document, result);
result.microdata = this.getMicrodata(document);
result.microformats = this.getMicroformats(document, target);
return result;
},
/**
* Get all microdata from an HTML document, or from only a given element.
*
* Returns an object in the format:
* {
* "items": [
* {
* "type": [ "<TYPE>" ],
* "properties": {
* "<PROPERTY-NAME>": [ "<PROPERTY-VALUE>", ... ],
* ...,
* }
* },
* ...,
* ]
* }
*
* @note This is based on wg algorythm to convert microdata to json
* http://www.whatwg.org/specs/web-apps/current-work/multipage/microdata-2.html#json
*
* @param {Document} document - Document to extract data from.
* @param {Element} [target] - Optional element to restrict microdata lookup to.
* @return {Object} Object describing the found microdata.
*/
getMicrodata(document, target = null) {
function getObject(item) {
let result = {};
if (item.itemType.length) {
result.types = [...item.itemType];
}
if (item.itemId) {
result.itemId = item.itemId;
}
if (item.properties.length) {
result.properties = {};
}
for (let elem of item.properties) {
let value;
if (elem.itemScope) {
value = getObject(elem);
} else if (elem.itemValue) {
value = elem.itemValue;
} else if (elem.hasAttribute("content")) {
// Handle mis-formatted microdata.
value = elem.getAttribute("content");
}
for (let prop of elem.itemProp) {
if (!result.properties[prop]) {
result.properties[prop] = [];
}
result.properties[prop].push(value);
}
}
return result;
getMicroformats(document, target = null) {
if (target) {
return Microformats.getParent(target, {node: document});
}
let result = { items: [] };
let elements = target ? [target] : document.getItems();
for (let element of elements) {
if (element.itemScope) {
result.items.push(getObject(element));
}
}
return result;
return Microformats.get({node: document});
},
/**