summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/test/unit/test_attribute-parsing-02.js
blob: 2c24d8f05bb1485e32d1c44f172ac77e4eea4514 (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
/* 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 {require} = Components.utils.import("resource://devtools/shared/Loader.jsm", {});
const {parseAttribute} = require("devtools/client/shared/node-attribute-parser");

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 (let {tagName, namespaceURI, attributeName,
            otherAttributes, attributeValue, expected} of TEST_DATA) {
    do_print("Testing <" + tagName + " " + attributeName + "='" + attributeValue + "'>");

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

    do_print("Checking that the number of parsed tokens is correct");
    do_check_eq(tokens.length, expected.length);

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