tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_schemaorg_parse.js (5143B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 /**
      6 * Tests that the page data service can parse schema.org metadata into Item
      7 * structures.
      8 */
      9 
     10 const { SchemaOrgPageData } = ChromeUtils.importESModule(
     11  "moz-src:///browser/components/pagedata/SchemaOrgPageData.sys.mjs"
     12 );
     13 
     14 /**
     15 * Collects the schema.org items from the given html string.
     16 *
     17 * @param {string} docStr
     18 *   The html to parse.
     19 * @returns {Promise<Item[]>}
     20 */
     21 async function collectItems(docStr) {
     22  let doc = await parseDocument(docStr);
     23  return SchemaOrgPageData.collectItems(doc);
     24 }
     25 
     26 /**
     27 * Verifies that the items parsed from the html match the expected JSON-LD
     28 * format.
     29 *
     30 * @param {string} docStr
     31 *   The html to parse.
     32 * @param {object[]} expected
     33 *   The JSON-LD objects to match to.
     34 */
     35 async function verifyItems(docStr, expected) {
     36  let items = await collectItems(docStr);
     37  let jsonLD = items.map(item => item.toJsonLD());
     38  Assert.deepEqual(jsonLD, expected);
     39 }
     40 
     41 add_task(async function test_microdata_parse() {
     42  await verifyItems(
     43    `
     44      <!DOCTYPE html>
     45      <html>
     46      <head>
     47      <title>Product Info 1</title>
     48      </head>
     49      <body itemprop="badprop">
     50        <div itemscope itemtype="https://schema.org/Organization">
     51          <div itemprop="employee" itemscope itemtype="https://schema.org/Person">
     52            <span itemprop="name">Mr. Nested Name</span>
     53          </div>
     54 
     55          <span itemprop="name">Mozilla</span>
     56        </div>
     57 
     58        <div itemscope itemtype="https://schema.org/Product">
     59          <img itemprop="image" src="bon-echo-microwave-17in.jpg" />
     60          <a href="microwave.html" itemprop="url">
     61            <span itemprop="name">Bon Echo Microwave</span>
     62          </a>
     63 
     64          <div itemprop="offers" itemscope itemtype="https://schema.org/Offer">
     65            <span itemprop="price" content="3.50">£3.50</span>
     66            <span itemprop="priceCurrency" content="GBP"></span>
     67          </div>
     68 
     69          <span itemprop="gtin" content="13572468"></span>
     70 
     71          <span itemprop="description">The most amazing microwave in the world</span>
     72        </div>
     73      </body>
     74      </html>
     75    `,
     76    [
     77      {
     78        "@type": "Organization",
     79        employee: {
     80          "@type": "Person",
     81          name: "Mr. Nested Name",
     82        },
     83        name: "Mozilla",
     84      },
     85      {
     86        "@type": "Product",
     87        image: BASE_URL + "/bon-echo-microwave-17in.jpg",
     88        url: BASE_URL + "/microwave.html",
     89        name: "Bon Echo Microwave",
     90        offers: {
     91          "@type": "Offer",
     92          price: "3.50",
     93          priceCurrency: "GBP",
     94        },
     95        gtin: "13572468",
     96        description: "The most amazing microwave in the world",
     97      },
     98    ]
     99  );
    100 });
    101 
    102 add_task(async function test_json_ld_parse() {
    103  await verifyItems(
    104    `
    105      <!DOCTYPE html>
    106      <html>
    107      <head>
    108      <script type="application/ld+json">
    109        {
    110          "@context": "http://schema.org",
    111          "@type": "Organization",
    112          "employee": {
    113            "@type": "Person",
    114            "name": "Mr. Nested Name"
    115          },
    116          "name": "Mozilla"
    117        }
    118      </script>
    119      <script type="application/ld+json">
    120        {
    121          "@context": "https://schema.org",
    122          "@type": "Product",
    123          "image": "bon-echo-microwave-17in.jpg",
    124          "url": "microwave.html",
    125          "name": "Bon Echo Microwave",
    126          "offers": {
    127            "@type": "Offer",
    128            "price": "3.50",
    129            "priceCurrency": "GBP"
    130          },
    131          "gtin": "13572468",
    132          "description": "The most amazing microwave in the world"
    133        }
    134      </script>
    135      </head>
    136      <body>
    137      </body>
    138      </html>
    139    `,
    140    [
    141      {
    142        "@type": "Organization",
    143        employee: {
    144          "@type": "Person",
    145          name: "Mr. Nested Name",
    146        },
    147        name: "Mozilla",
    148      },
    149      {
    150        "@type": "Product",
    151        image: "bon-echo-microwave-17in.jpg",
    152        url: "microwave.html",
    153        name: "Bon Echo Microwave",
    154        offers: {
    155          "@type": "Offer",
    156          price: "3.50",
    157          priceCurrency: "GBP",
    158        },
    159        gtin: "13572468",
    160        description: "The most amazing microwave in the world",
    161      },
    162    ]
    163  );
    164 });
    165 
    166 add_task(async function test_microdata_lazy_image() {
    167  await verifyItems(
    168    `
    169      <!DOCTYPE html>
    170      <html>
    171      <head>
    172      <title>Product Info 1</title>
    173      </head>
    174      <body itemprop="badprop">
    175        <div itemscope itemtype="https://schema.org/Product">
    176          <img itemprop="image" src="lazy-load.gif" data-src="bon-echo-microwave-17in.jpg" />
    177          <a href="microwave.html" itemprop="url">
    178            <span itemprop="name">Bon Echo Microwave</span>
    179          </a>
    180        </div>
    181      </body>
    182      </html>
    183    `,
    184    [
    185      {
    186        "@type": "Product",
    187        image: BASE_URL + "/bon-echo-microwave-17in.jpg",
    188        url: BASE_URL + "/microwave.html",
    189        name: "Bon Echo Microwave",
    190      },
    191    ]
    192  );
    193 });