summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/unit/test_bookmarks_html_escape_entities.js
blob: 5349d3948c4d3ec2805e51bb2557920fc025869e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Checks that html entities are escaped in bookmarks.html files.

add_task(async function () {
  // Removes bookmarks.html if the file already exists.
  let HTMLFile = PathUtils.join(PathUtils.profileDir, "bookmarks.html");
  await IOUtils.remove(HTMLFile, { ignoreAbsent: true });

  let unescaped = '<unescaped="test">';
  // Adds bookmarks and tags to the database.
  const url = 'http://www.google.it/"/';
  let bm = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    url,
    title: unescaped,
  });
  await PlacesUtils.keywords.insert({
    url,
    keyword: unescaped,
    postData: unescaped,
  });
  PlacesUtils.tagging.tagURI(Services.io.newURI(url), [unescaped]);
  await PlacesUtils.history.update({
    url,
    annotations: new Map([[PlacesUtils.CHARSET_ANNO, unescaped]]),
  });

  // Exports the bookmarks as a HTML file.
  await BookmarkHTMLUtils.exportToFile(HTMLFile);
  await PlacesUtils.bookmarks.remove(bm);

  // Check there are no unescaped entities in the html file.
  let xml = await new Promise((resolve, reject) => {
    let xhr = new XMLHttpRequest();
    xhr.onload = () => {
      try {
        resolve(xhr.responseXML);
      } catch (e) {
        reject(e);
      }
    };
    xhr.onabort =
      xhr.onerror =
      xhr.ontimeout =
        () => {
          reject(new Error("xmlhttprequest failed"));
        };
    xhr.open("GET", PathUtils.toFileURI(HTMLFile));
    xhr.responseType = "document";
    xhr.overrideMimeType("text/html");
    xhr.send();
  });

  let checksCount = 5;
  for (
    let current = xml;
    current;
    current =
      current.firstChild ||
      current.nextSibling ||
      current.parentNode.nextSibling
  ) {
    switch (current.nodeType) {
      case current.ELEMENT_NODE:
        for (let { name, value } of current.attributes) {
          info("Found attribute: " + name);
          // Check tags, keyword, postData and charSet.
          if (
            ["tags", "last_charset", "shortcuturl", "post_data"].includes(name)
          ) {
            Assert.equal(
              value,
              unescaped,
              `Attribute ${name} should be complete`
            );
            checksCount--;
          }
        }
        break;
      case current.TEXT_NODE:
        // Check Title.
        if (!current.data.startsWith("\n") && current.data.includes("test")) {
          Assert.equal(
            current.data.trim(),
            unescaped,
            "Text node should be complete"
          );
          checksCount--;
        }
        break;
    }
  }
  Assert.equal(checksCount, 0, "All the checks ran");
});