summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/test/xpcshell/test_attribute-parsing-02.js
blob: 2cc05574dd9ec65fa4dc498ddb512140f90b79a3 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Test parseAttribute from node-attribute-parser.js

const {
  parseAttribute,
} = require("resource://devtools/client/shared/node-attribute-parser.js");

const TEST_DATA = [
  {
    tagName: "body",
    namespaceURI: "http://www.w3.org/1999/xhtml",
    attributeName: "class",
    attributeValue: "some css class names",
    expected: [{ value: "some css class names", type: "string" }],
  },
  {
    tagName: "box",
    namespaceURI:
      "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul",
    attributeName: "datasources",
    attributeValue: "/url/1?test=1#test http://mozilla.org/wow",
    expected: [
      { value: "/url/1?test=1#test", type: "uri" },
      { value: " ", type: "string" },
      { value: "http://mozilla.org/wow", type: "uri" },
    ],
  },
  {
    tagName: "form",
    namespaceURI: "http://www.w3.org/1999/xhtml",
    attributeName: "action",
    attributeValue: "/path/to/handler",
    expected: [{ value: "/path/to/handler", type: "uri" }],
  },
  {
    tagName: "a",
    namespaceURI: "http://www.w3.org/1999/xhtml",
    attributeName: "ping",
    attributeValue:
      "http://analytics.com/track?id=54 http://analytics.com/track?id=55",
    expected: [
      { value: "http://analytics.com/track?id=54", type: "uri" },
      { value: " ", type: "string" },
      { value: "http://analytics.com/track?id=55", type: "uri" },
    ],
  },
  {
    tagName: "link",
    namespaceURI: "http://www.w3.org/1999/xhtml",
    attributeName: "href",
    attributeValue: "styles.css",
    otherAttributes: [{ name: "rel", value: "stylesheet" }],
    expected: [{ value: "styles.css", type: "cssresource" }],
  },
  {
    tagName: "link",
    namespaceURI: "http://www.w3.org/1999/xhtml",
    attributeName: "href",
    attributeValue: "styles.css",
    expected: [{ value: "styles.css", type: "uri" }],
  },
  {
    tagName: "output",
    namespaceURI: "http://www.w3.org/1999/xhtml",
    attributeName: "for",
    attributeValue: "element-id something id",
    expected: [
      { value: "element-id", type: "idref" },
      { value: " ", type: "string" },
      { value: "something", type: "idref" },
      { value: " ", type: "string" },
      { value: "id", type: "idref" },
    ],
  },
  {
    tagName: "img",
    namespaceURI: "http://www.w3.org/1999/xhtml",
    attributeName: "contextmenu",
    attributeValue: "id-of-menu",
    expected: [{ value: "id-of-menu", type: "idref" }],
  },
  {
    tagName: "img",
    namespaceURI: "http://www.w3.org/1999/xhtml",
    attributeName: "src",
    attributeValue: "omg-thats-so-funny.gif",
    expected: [{ value: "omg-thats-so-funny.gif", type: "uri" }],
  },
  {
    tagName: "key",
    namespaceURI:
      "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul",
    attributeName: "command",
    attributeValue: "some_command_id",
    expected: [{ value: "some_command_id", type: "idref" }],
  },
  {
    tagName: "script",
    namespaceURI: "whatever",
    attributeName: "src",
    attributeValue: "script.js",
    expected: [{ value: "script.js", type: "jsresource" }],
  },
];

function run_test() {
  for (const {
    tagName,
    namespaceURI,
    attributeName,
    otherAttributes,
    attributeValue,
    expected,
  } of TEST_DATA) {
    info(
      "Testing <" + tagName + " " + attributeName + "='" + attributeValue + "'>"
    );

    const attributes = [
      ...(otherAttributes || []),
      { name: attributeName, value: attributeValue },
    ];
    const tokens = parseAttribute(
      namespaceURI,
      tagName,
      attributes,
      attributeName,
      attributeValue
    );
    if (!expected) {
      Assert.ok(!tokens);
      continue;
    }

    info("Checking that the number of parsed tokens is correct");
    Assert.equal(tokens.length, expected.length);

    for (let i = 0; i < tokens.length; i++) {
      info("Checking the data in token " + i);
      Assert.equal(tokens[i].value, expected[i].value);
      Assert.equal(tokens[i].type, expected[i].type);
    }
  }
}