browser_getshortcutoruri.js
Go to the documentation of this file.
1 function getPostDataString(aIS) {
2  if (!aIS)
3  return null;
4 
5  var sis = Cc["@mozilla.org/scriptableinputstream;1"].
6  createInstance(Ci.nsIScriptableInputStream);
7  sis.init(aIS);
8  var dataLines = sis.read(aIS.available()).split("\n");
9 
10  // only want the last line
11  return dataLines[dataLines.length-1];
12 }
13 
14 function keywordResult(aURL, aPostData) {
15  this.url = aURL;
16  this.postData = aPostData;
17 }
18 
19 function keyWordData() {}
20 keyWordData.prototype = {
21  init: function(aKeyWord, aURL, aPostData, aSearchWord) {
22  this.keyword = aKeyWord;
23  this.uri = makeURI(aURL);
24  this.postData = aPostData;
25  this.searchWord = aSearchWord;
26 
27  this.method = (this.postData ? "POST" : "GET");
28  }
29 }
30 
31 function bmKeywordData(aKeyWord, aURL, aPostData, aSearchWord) {
32  this.init(aKeyWord, aURL, aPostData, aSearchWord);
33 }
34 bmKeywordData.prototype = new keyWordData();
35 
36 function searchKeywordData(aKeyWord, aURL, aPostData, aSearchWord) {
37  this.init(aKeyWord, aURL, aPostData, aSearchWord);
38 }
39 searchKeywordData.prototype = new keyWordData();
40 
41 var testData = [
42  [new bmKeywordData("bmget", "http://bmget/search=%s", null, "foo"),
43  new keywordResult("http://bmget/search=foo", null)],
44 
45  [new bmKeywordData("bmpost", "http://bmpost/", "search=%s", "foo2"),
46  new keywordResult("http://bmpost/", "search=foo2")],
47 
48  [new bmKeywordData("bmpostget", "http://bmpostget/search1=%s", "search2=%s", "foo3"),
49  new keywordResult("http://bmpostget/search1=foo3", "search2=foo3")],
50 
51  [new bmKeywordData("bmget-nosearch", "http://bmget-nosearch/", null, ""),
52  new keywordResult("http://bmget-nosearch/", null)],
53 
54  [new searchKeywordData("searchget", "http://searchget/?search={searchTerms}", null, "foo4"),
55  new keywordResult("http://searchget/?search=foo4", null)],
56 
57  [new searchKeywordData("searchpost", "http://searchpost/", "search={searchTerms}", "foo5"),
58  new keywordResult("http://searchpost/", "search=foo5")],
59 
60  [new searchKeywordData("searchpostget", "http://searchpostget/?search1={searchTerms}", "search2={searchTerms}", "foo6"),
61  new keywordResult("http://searchpostget/?search1=foo6", "search2=foo6")],
62 
63  // Bookmark keywords that don't take parameters should not be activated if a
64  // parameter is passed (bug 420328).
65  [new bmKeywordData("bmget-noparam", "http://bmget-noparam/", null, "foo7"),
66  new keywordResult(null, null)],
67  [new bmKeywordData("bmpost-noparam", "http://bmpost-noparam/", "not_a=param", "foo8"),
68  new keywordResult(null, null)],
69 
70  // Test escaping (%s = escaped, %S = raw)
71  // UTF-8 default
72  [new bmKeywordData("bmget-escaping", "http://bmget/?esc=%s&raw=%S", null, "foé), new keywordResult("http://bmget/?esc=fo%C3%A9&raw=foé", null)], // Explicitly-defined ISO-8859-1 [new bmKeywordData("bmget-escaping2", "http://bmget/?esc=%s&raw=%S&mozcharset=ISO-8859-1", null, "foé"), new keywordResult("http://bmget/?esc=fo%E9&raw=foé", null)], ]; function test() { setupKeywords(); for each (var item in testData) { var [data, result] = item; var postData = {}; var query = data.keyword; if (data.searchWord) query += " " + data.searchWord; var url = getShortcutOrURI(query, postData); // null result.url means we should expect the same query we sent in var expected = result.url || query; is(url, expected, "got correct URL for " + data.keyword); is(getPostDataString(postData.value), result.postData, "got correct postData for " + data.keyword); } cleanupKeywords(); } var gBMFolder = null; var gAddedEngines = []; function setupKeywords() { var searchService = Cc["@mozilla.org/browser/search-service;1"]. getService(Ci.nsIBrowserSearchService); gBMFolder = Application.bookmarks.menu.addFolder("keyword-test"); for each (var item in testData) { var data = item[0]; if (data instanceof bmKeywordData) { var bm = gBMFolder.addBookmark(data.keyword, data.uri); bm.keyword = data.keyword; if (data.postData) bm.annotations.set("bookmarkProperties/POSTData", data.postData, Ci.nsIAnnotationService.EXPIRE_SESSION); } if (data instanceof searchKeywordData) { searchService.addEngineWithDetails(data.keyword, "", data.keyword, "", data.method, data.uri.spec); var addedEngine = searchService.getEngineByName(data.keyword); if (data.postData) { var [paramName, paramValue] = data.postData.split("="); addedEngine.addParam(paramName, paramValue, null); } gAddedEngines.push(addedEngine); } } } function cleanupKeywords() { var searchService = Cc["@mozilla.org/browser/search-service;1"]. getService(Ci.nsIBrowserSearchService); gBMFolder.remove(); gAddedEngines.map(searchService.removeEngine); } "),
73  new keywordResult("http://bmget/?esc=fo%C3%A9&raw=foé, null)], // Explicitly-defined ISO-8859-1 [new bmKeywordData("bmget-escaping2", "http://bmget/?esc=%s&raw=%S&mozcharset=ISO-8859-1", null, "foé"), new keywordResult("http://bmget/?esc=fo%E9&raw=foé", null)], ]; function test() { setupKeywords(); for each (var item in testData) { var [data, result] = item; var postData = {}; var query = data.keyword; if (data.searchWord) query += " " + data.searchWord; var url = getShortcutOrURI(query, postData); // null result.url means we should expect the same query we sent in var expected = result.url || query; is(url, expected, "got correct URL for " + data.keyword); is(getPostDataString(postData.value), result.postData, "got correct postData for " + data.keyword); } cleanupKeywords(); } var gBMFolder = null; var gAddedEngines = []; function setupKeywords() { var searchService = Cc["@mozilla.org/browser/search-service;1"]. getService(Ci.nsIBrowserSearchService); gBMFolder = Application.bookmarks.menu.addFolder("keyword-test"); for each (var item in testData) { var data = item[0]; if (data instanceof bmKeywordData) { var bm = gBMFolder.addBookmark(data.keyword, data.uri); bm.keyword = data.keyword; if (data.postData) bm.annotations.set("bookmarkProperties/POSTData", data.postData, Ci.nsIAnnotationService.EXPIRE_SESSION); } if (data instanceof searchKeywordData) { searchService.addEngineWithDetails(data.keyword, "", data.keyword, "", data.method, data.uri.spec); var addedEngine = searchService.getEngineByName(data.keyword); if (data.postData) { var [paramName, paramValue] = data.postData.split("="); addedEngine.addParam(paramName, paramValue, null); } gAddedEngines.push(addedEngine); } } } function cleanupKeywords() { var searchService = Cc["@mozilla.org/browser/search-service;1"]. getService(Ci.nsIBrowserSearchService); gBMFolder.remove(); gAddedEngines.map(searchService.removeEngine); } ", null)],
74  // Explicitly-defined ISO-8859-1
75  [new bmKeywordData("bmget-escaping2", "http://bmget/?esc=%s&raw=%S&mozcharset=ISO-8859-1", null, "foé), new keywordResult("http://bmget/?esc=fo%E9&raw=foé", null)], ]; function test() { setupKeywords(); for each (var item in testData) { var [data, result] = item; var postData = {}; var query = data.keyword; if (data.searchWord) query += " " + data.searchWord; var url = getShortcutOrURI(query, postData); // null result.url means we should expect the same query we sent in var expected = result.url || query; is(url, expected, "got correct URL for " + data.keyword); is(getPostDataString(postData.value), result.postData, "got correct postData for " + data.keyword); } cleanupKeywords(); } var gBMFolder = null; var gAddedEngines = []; function setupKeywords() { var searchService = Cc["@mozilla.org/browser/search-service;1"]. getService(Ci.nsIBrowserSearchService); gBMFolder = Application.bookmarks.menu.addFolder("keyword-test"); for each (var item in testData) { var data = item[0]; if (data instanceof bmKeywordData) { var bm = gBMFolder.addBookmark(data.keyword, data.uri); bm.keyword = data.keyword; if (data.postData) bm.annotations.set("bookmarkProperties/POSTData", data.postData, Ci.nsIAnnotationService.EXPIRE_SESSION); } if (data instanceof searchKeywordData) { searchService.addEngineWithDetails(data.keyword, "", data.keyword, "", data.method, data.uri.spec); var addedEngine = searchService.getEngineByName(data.keyword); if (data.postData) { var [paramName, paramValue] = data.postData.split("="); addedEngine.addParam(paramName, paramValue, null); } gAddedEngines.push(addedEngine); } } } function cleanupKeywords() { var searchService = Cc["@mozilla.org/browser/search-service;1"]. getService(Ci.nsIBrowserSearchService); gBMFolder.remove(); gAddedEngines.map(searchService.removeEngine); } "),
76  new keywordResult("http://bmget/?esc=fo%E9&raw=foé, null)], ]; function test() { setupKeywords(); for each (var item in testData) { var [data, result] = item; var postData = {}; var query = data.keyword; if (data.searchWord) query += " " + data.searchWord; var url = getShortcutOrURI(query, postData); // null result.url means we should expect the same query we sent in var expected = result.url || query; is(url, expected, "got correct URL for " + data.keyword); is(getPostDataString(postData.value), result.postData, "got correct postData for " + data.keyword); } cleanupKeywords(); } var gBMFolder = null; var gAddedEngines = []; function setupKeywords() { var searchService = Cc["@mozilla.org/browser/search-service;1"]. getService(Ci.nsIBrowserSearchService); gBMFolder = Application.bookmarks.menu.addFolder("keyword-test"); for each (var item in testData) { var data = item[0]; if (data instanceof bmKeywordData) { var bm = gBMFolder.addBookmark(data.keyword, data.uri); bm.keyword = data.keyword; if (data.postData) bm.annotations.set("bookmarkProperties/POSTData", data.postData, Ci.nsIAnnotationService.EXPIRE_SESSION); } if (data instanceof searchKeywordData) { searchService.addEngineWithDetails(data.keyword, "", data.keyword, "", data.method, data.uri.spec); var addedEngine = searchService.getEngineByName(data.keyword); if (data.postData) { var [paramName, paramValue] = data.postData.split("="); addedEngine.addParam(paramName, paramValue, null); } gAddedEngines.push(addedEngine); } } } function cleanupKeywords() { var searchService = Cc["@mozilla.org/browser/search-service;1"]. getService(Ci.nsIBrowserSearchService); gBMFolder.remove(); gAddedEngines.map(searchService.removeEngine); } ", null)],
77 ];
78 
79 function test() {
80  setupKeywords();
81 
82  for each (var item in testData) {
83  var [data, result] = item;
84 
85  var postData = {};
86  var query = data.keyword;
87  if (data.searchWord)
88  query += " " + data.searchWord;
89  var url = getShortcutOrURI(query, postData);
90 
91  // null result.url means we should expect the same query we sent in
92  var expected = result.url || query;
93  is(url, expected, "got correct URL for " + data.keyword);
94  is(getPostDataString(postData.value), result.postData, "got correct postData for " + data.keyword);
95  }
96 
98 }
99 
101 var gAddedEngines = [];
102 function setupKeywords() {
103  var searchService = Cc["@mozilla.org/browser/search-service;1"].
104  getService(Ci.nsIBrowserSearchService);
105  gBMFolder = Application.bookmarks.menu.addFolder("keyword-test");
106  for each (var item in testData) {
107  var data = item[0];
108  if (data instanceof bmKeywordData) {
109  var bm = gBMFolder.addBookmark(data.keyword, data.uri);
110  bm.keyword = data.keyword;
111  if (data.postData)
112  bm.annotations.set("bookmarkProperties/POSTData", data.postData, Ci.nsIAnnotationService.EXPIRE_SESSION);
113  }
114 
115  if (data instanceof searchKeywordData) {
116  searchService.addEngineWithDetails(data.keyword, "", data.keyword, "", data.method, data.uri.spec);
117  var addedEngine = searchService.getEngineByName(data.keyword);
118  if (data.postData) {
119  var [paramName, paramValue] = data.postData.split("=");
120  addedEngine.addParam(paramName, paramValue, null);
121  }
122 
123  gAddedEngines.push(addedEngine);
124  }
125  }
126 }
127 
128 function cleanupKeywords() {
129  var searchService = Cc["@mozilla.org/browser/search-service;1"].
130  getService(Ci.nsIBrowserSearchService);
131  gBMFolder.remove();
132  gAddedEngines.map(searchService.removeEngine);
133 }
const Cc
function searchKeywordData(aKeyWord, aURL, aPostData, aSearchWord)
function getShortcutOrURI(aURL, aPostDataRef)
Definition: browser.js:1960
var Application
Definition: sbAboutDRM.js:37
function cleanupKeywords()
sidebarFactory createInstance
Definition: nsSidebar.js:351
function keyWordData()
getService(Ci.sbIFaceplateManager)
_window init
Definition: FeedWriter.js:1144
function makeURI(aURLSpec, aCharset)
Definition: FeedWriter.js:71
function test()
return null
Definition: FeedWriter.js:1143
var expected
var uri
Definition: FeedWriter.js:1135
function url(spec)
function getPostDataString(aIS)
const Ci
function setupKeywords()
function keywordResult(aURL, aPostData)
observe data
Definition: FeedWriter.js:1329