Bug 416611 - Changed BookmarkHTMLUtils.jsm to import bookmark tags from HTML document. r=mak
authorBradley Garlick <brambleg@hotmail.co.uk>
Mon, 14 Sep 2015 13:36:27 +0530
changeset 294864 83d4e623d4762dc8ff5075d3a059de5ebdaab5d3
parent 294848 00693818cbf5fdd073dd4d895be44771091e9432
child 294865 a8a074a2ee8912b7aefd590956534ca0a7d44175
push id5245
push userraliiev@mozilla.com
push dateThu, 29 Oct 2015 11:30:51 +0000
treeherdermozilla-beta@dac831dc1bd0 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmak
bugs416611
milestone43.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 416611 - Changed BookmarkHTMLUtils.jsm to import bookmark tags from HTML document. r=mak
toolkit/components/places/BookmarkHTMLUtils.jsm
toolkit/components/places/tests/unit/test_bookmarks_html_import_tags.js
toolkit/components/places/tests/unit/xpcshell.ini
--- a/toolkit/components/places/BookmarkHTMLUtils.jsm
+++ b/toolkit/components/places/BookmarkHTMLUtils.jsm
@@ -512,16 +512,17 @@ BookmarkImporter.prototype = {
     let lastCharset = this._safeTrim(aElt.getAttribute("last_charset"));
     let keyword = this._safeTrim(aElt.getAttribute("shortcuturl"));
     let postData = this._safeTrim(aElt.getAttribute("post_data"));
     let webPanel = this._safeTrim(aElt.getAttribute("web_panel"));
     let micsumGenURI = this._safeTrim(aElt.getAttribute("micsum_gen_uri"));
     let generatedTitle = this._safeTrim(aElt.getAttribute("generated_title"));
     let dateAdded = this._safeTrim(aElt.getAttribute("add_date"));
     let lastModified = this._safeTrim(aElt.getAttribute("last_modified"));
+    let tags = this._safeTrim(aElt.getAttribute("tags"));
 
     // For feeds, get the feed URL.  If it is invalid, mPreviousFeed will be
     // NULL and we'll create it as a normal bookmark.
     if (feedUrl) {
       frame.previousFeed = NetUtil.newURI(feedUrl);
     }
 
     // Ignore <a> tags that have no href.
@@ -572,16 +573,25 @@ BookmarkImporter.prototype = {
     if (dateAdded) {
       try {
         PlacesUtils.bookmarks.setItemDateAdded(frame.previousId,
           this._convertImportedDateToInternalDate(dateAdded));
       } catch(e) {
       }
     }
 
+    // Adds tags to the URI, if there are any.
+    if (tags) {
+      try {
+        let tagsArray = tags.split(",");
+        PlacesUtils.tagging.tagURI(frame.previousLink, tagsArray);
+      } catch(e) {
+      }
+    }
+
     // Save the favicon.
     if (icon || iconUri) {
       let iconUriObject;
       try {
         iconUriObject = NetUtil.newURI(iconUri);
       } catch(e) {
       }
       if (icon || iconUriObject) {
new file mode 100644
--- /dev/null
+++ b/toolkit/components/places/tests/unit/test_bookmarks_html_import_tags.js
@@ -0,0 +1,57 @@
+let bookmarkData = [
+  { uri: uri("http://www.toastytech.com"),
+    title: "Nathan's Toasty Technology Page",
+    tags: ["technology", "personal", "retro"] },
+  { uri: uri("http://www.reddit.com"),
+    title: "reddit: the front page of the internet",
+    tags: ["social media", "news", "humour"] },
+  { uri: uri("http://www.4chan.org"),
+    title: "4chan",
+    tags: ["discussion", "imageboard", "anime"] }
+];
+
+/*
+  TEST SUMMARY
+  - Add bookmarks with tags
+  - Export tagged bookmarks as HTML file
+  - Delete bookmarks
+  - Import bookmarks from HTML file
+  - Check that all bookmarks are successfully imported with tags
+*/
+
+add_task(function* test_import_tags() {
+  // Removes bookmarks.html if the file already exists.
+  let HTMLFile = OS.Path.join(OS.Constants.Path.profileDir, "bookmarks.html");
+  if ((yield OS.File.exists(HTMLFile)))
+    yield OS.File.remove(HTMLFile);
+
+  // Adds bookmarks and tags to the database.
+  let bookmarkList = new Set();
+  for (let { uri, title, tags } of bookmarkData) {
+    bookmarkList.add(yield PlacesUtils.bookmarks.insert({ 
+                                parentGuid: PlacesUtils.bookmarks.unfiledGuid,
+                                url: uri,
+                                title }));
+    PlacesUtils.tagging.tagURI(uri, tags);
+  }
+
+  // Exports the bookmarks as a HTML file.
+  yield BookmarkHTMLUtils.exportToFile(HTMLFile);
+
+  // Deletes bookmarks and tags from the database.
+  for (let bookmark of bookmarkList) {
+    yield PlacesUtils.bookmarks.remove(bookmark.guid);
+  }
+
+  // Re-imports the bookmarks from the HTML file.
+  yield BookmarkHTMLUtils.importFromFile(HTMLFile, true);
+
+  // Tests to ensure that the tags are still present for each bookmark URI.
+  for (let { uri, tags } of bookmarkData) {
+    do_print("Test tags for " + uri.spec + ": " + tags + "\n");
+    let foundTags = PlacesUtils.tagging.getTagsForURI(uri);
+    Assert.equal(foundTags.length, tags.length);
+    Assert.ok(tags.every(tag => foundTags.includes(tag)));
+  }
+});
+
--- a/toolkit/components/places/tests/unit/xpcshell.ini
+++ b/toolkit/components/places/tests/unit/xpcshell.ini
@@ -68,17 +68,18 @@ skip-if = os == "android"
 [test_async_history_api.js]
 [test_async_in_batchmode.js]
 [test_async_transactions.js]
 skip-if = (os == "win" && os_version == "5.1") # Bug 1158887
 [test_autocomplete_stopSearch_no_throw.js]
 [test_bookmark_catobs.js]
 [test_bookmarks_json.js]
 [test_bookmarks_html.js]
-[test_bookmarks_html_corrupt.js]
+[test_bookmarks_html_corrupt.js]
+[test_bookmarks_html_import_tags.js]
 [test_bookmarks_html_singleframe.js]
 [test_bookmarks_restore_notification.js]
 [test_bookmarks_setNullTitle.js]
 [test_broken_folderShortcut_result.js]
 [test_browserhistory.js]
 [test_bug636917_isLivemark.js]
 [test_childlessTags.js]
 [test_crash_476292.js]