summaryrefslogtreecommitdiffstats
path: root/browser/components/pagedata/tests/unit/test_schemaorg_parse.js
blob: e002598af2576d6aea0d36819b984e7e2ce74e78 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/**
 * Tests that the page data service can parse schema.org metadata into Item
 * structures.
 */

const { SchemaOrgPageData } = ChromeUtils.importESModule(
  "resource:///modules/pagedata/SchemaOrgPageData.sys.mjs"
);

/**
 * Collects the schema.org items from the given html string.
 *
 * @param {string} docStr
 *   The html to parse.
 * @returns {Promise<Item[]>}
 */
async function collectItems(docStr) {
  let doc = await parseDocument(docStr);
  return SchemaOrgPageData.collectItems(doc);
}

/**
 * Verifies that the items parsed from the html match the expected JSON-LD
 * format.
 *
 * @param {string} docStr
 *   The html to parse.
 * @param {object[]} expected
 *   The JSON-LD objects to match to.
 */
async function verifyItems(docStr, expected) {
  let items = await collectItems(docStr);
  let jsonLD = items.map(item => item.toJsonLD());
  Assert.deepEqual(jsonLD, expected);
}

add_task(async function test_microdata_parse() {
  await verifyItems(
    `
      <!DOCTYPE html>
      <html>
      <head>
      <title>Product Info 1</title>
      </head>
      <body itemprop="badprop">
        <div itemscope itemtype="https://schema.org/Organization">
          <div itemprop="employee" itemscope itemtype="https://schema.org/Person">
            <span itemprop="name">Mr. Nested Name</span>
          </div>

          <span itemprop="name">Mozilla</span>
        </div>

        <div itemscope itemtype="https://schema.org/Product">
          <img itemprop="image" src="bon-echo-microwave-17in.jpg" />
          <a href="microwave.html" itemprop="url">
            <span itemprop="name">Bon Echo Microwave</span>
          </a>

          <div itemprop="offers" itemscope itemtype="https://schema.org/Offer">
            <span itemprop="price" content="3.50">£3.50</span>
            <span itemprop="priceCurrency" content="GBP"></span>
          </div>

          <span itemprop="gtin" content="13572468"></span>

          <span itemprop="description">The most amazing microwave in the world</span>
        </div>
      </body>
      </html>
    `,
    [
      {
        "@type": "Organization",
        employee: {
          "@type": "Person",
          name: "Mr. Nested Name",
        },
        name: "Mozilla",
      },
      {
        "@type": "Product",
        image: BASE_URL + "/bon-echo-microwave-17in.jpg",
        url: BASE_URL + "/microwave.html",
        name: "Bon Echo Microwave",
        offers: {
          "@type": "Offer",
          price: "3.50",
          priceCurrency: "GBP",
        },
        gtin: "13572468",
        description: "The most amazing microwave in the world",
      },
    ]
  );
});

add_task(async function test_json_ld_parse() {
  await verifyItems(
    `
      <!DOCTYPE html>
      <html>
      <head>
      <script type="application/ld+json">
        {
          "@context": "http://schema.org",
          "@type": "Organization",
          "employee": {
            "@type": "Person",
            "name": "Mr. Nested Name"
          },
          "name": "Mozilla"
        }
      </script>
      <script type="application/ld+json">
        {
          "@context": "https://schema.org",
          "@type": "Product",
          "image": "bon-echo-microwave-17in.jpg",
          "url": "microwave.html",
          "name": "Bon Echo Microwave",
          "offers": {
            "@type": "Offer",
            "price": "3.50",
            "priceCurrency": "GBP"
          },
          "gtin": "13572468",
          "description": "The most amazing microwave in the world"
        }
      </script>
      </head>
      <body>
      </body>
      </html>
    `,
    [
      {
        "@type": "Organization",
        employee: {
          "@type": "Person",
          name: "Mr. Nested Name",
        },
        name: "Mozilla",
      },
      {
        "@type": "Product",
        image: "bon-echo-microwave-17in.jpg",
        url: "microwave.html",
        name: "Bon Echo Microwave",
        offers: {
          "@type": "Offer",
          price: "3.50",
          priceCurrency: "GBP",
        },
        gtin: "13572468",
        description: "The most amazing microwave in the world",
      },
    ]
  );
});

add_task(async function test_microdata_lazy_image() {
  await verifyItems(
    `
      <!DOCTYPE html>
      <html>
      <head>
      <title>Product Info 1</title>
      </head>
      <body itemprop="badprop">
        <div itemscope itemtype="https://schema.org/Product">
          <img itemprop="image" src="lazy-load.gif" data-src="bon-echo-microwave-17in.jpg" />
          <a href="microwave.html" itemprop="url">
            <span itemprop="name">Bon Echo Microwave</span>
          </a>
        </div>
      </body>
      </html>
    `,
    [
      {
        "@type": "Product",
        image: BASE_URL + "/bon-echo-microwave-17in.jpg",
        url: BASE_URL + "/microwave.html",
        name: "Bon Echo Microwave",
      },
    ]
  );
});