test_attribute-parsing-02.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* Any copyright is dedicated to the Public Domain.
  2. http://creativecommons.org/publicdomain/zero/1.0/ */
  3. "use strict";
  4. // Test parseAttribute from node-attribute-parser.js
  5. const {require} = Components.utils.import("resource://devtools/shared/Loader.jsm", {});
  6. const {parseAttribute} = require("devtools/client/shared/node-attribute-parser");
  7. const TEST_DATA = [{
  8. tagName: "body",
  9. namespaceURI: "http://www.w3.org/1999/xhtml",
  10. attributeName: "class",
  11. attributeValue: "some css class names",
  12. expected: [
  13. {value: "some css class names", type: "string"}
  14. ]
  15. }, {
  16. tagName: "box",
  17. namespaceURI: "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul",
  18. attributeName: "datasources",
  19. attributeValue: "/url/1?test=1#test http://mozilla.org/wow",
  20. expected: [
  21. {value: "/url/1?test=1#test", type: "uri"},
  22. {value: " ", type: "string"},
  23. {value: "http://mozilla.org/wow", type: "uri"}
  24. ]
  25. }, {
  26. tagName: "form",
  27. namespaceURI: "http://www.w3.org/1999/xhtml",
  28. attributeName: "action",
  29. attributeValue: "/path/to/handler",
  30. expected: [
  31. {value: "/path/to/handler", type: "uri"}
  32. ]
  33. }, {
  34. tagName: "a",
  35. namespaceURI: "http://www.w3.org/1999/xhtml",
  36. attributeName: "ping",
  37. attributeValue: "http://analytics.com/track?id=54 http://analytics.com/track?id=55",
  38. expected: [
  39. {value: "http://analytics.com/track?id=54", type: "uri"},
  40. {value: " ", type: "string"},
  41. {value: "http://analytics.com/track?id=55", type: "uri"}
  42. ]
  43. }, {
  44. tagName: "link",
  45. namespaceURI: "http://www.w3.org/1999/xhtml",
  46. attributeName: "href",
  47. attributeValue: "styles.css",
  48. otherAttributes: [{name: "rel", value: "stylesheet"}],
  49. expected: [
  50. {value: "styles.css", type: "cssresource"}
  51. ]
  52. }, {
  53. tagName: "link",
  54. namespaceURI: "http://www.w3.org/1999/xhtml",
  55. attributeName: "href",
  56. attributeValue: "styles.css",
  57. expected: [
  58. {value: "styles.css", type: "uri"}
  59. ]
  60. }, {
  61. tagName: "output",
  62. namespaceURI: "http://www.w3.org/1999/xhtml",
  63. attributeName: "for",
  64. attributeValue: "element-id something id",
  65. expected: [
  66. {value: "element-id", type: "idref"},
  67. {value: " ", type: "string"},
  68. {value: "something", type: "idref"},
  69. {value: " ", type: "string"},
  70. {value: "id", type: "idref"}
  71. ]
  72. }, {
  73. tagName: "img",
  74. namespaceURI: "http://www.w3.org/1999/xhtml",
  75. attributeName: "contextmenu",
  76. attributeValue: "id-of-menu",
  77. expected: [
  78. {value: "id-of-menu", type: "idref"}
  79. ]
  80. }, {
  81. tagName: "img",
  82. namespaceURI: "http://www.w3.org/1999/xhtml",
  83. attributeName: "src",
  84. attributeValue: "omg-thats-so-funny.gif",
  85. expected: [
  86. {value: "omg-thats-so-funny.gif", type: "uri"}
  87. ]
  88. }, {
  89. tagName: "key",
  90. namespaceURI: "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul",
  91. attributeName: "command",
  92. attributeValue: "some_command_id",
  93. expected: [
  94. {value: "some_command_id", type: "idref"}
  95. ]
  96. }, {
  97. tagName: "script",
  98. namespaceURI: "whatever",
  99. attributeName: "src",
  100. attributeValue: "script.js",
  101. expected: [
  102. {value: "script.js", type: "jsresource"}
  103. ]
  104. }];
  105. function run_test() {
  106. for (let {tagName, namespaceURI, attributeName,
  107. otherAttributes, attributeValue, expected} of TEST_DATA) {
  108. do_print("Testing <" + tagName + " " + attributeName + "='" + attributeValue + "'>");
  109. let attributes = [
  110. ...otherAttributes || [],
  111. { name: attributeName, value: attributeValue }
  112. ];
  113. let tokens = parseAttribute(namespaceURI, tagName, attributes, attributeName);
  114. if (!expected) {
  115. do_check_true(!tokens);
  116. continue;
  117. }
  118. do_print("Checking that the number of parsed tokens is correct");
  119. do_check_eq(tokens.length, expected.length);
  120. for (let i = 0; i < tokens.length; i++) {
  121. do_print("Checking the data in token " + i);
  122. do_check_eq(tokens[i].value, expected[i].value);
  123. do_check_eq(tokens[i].type, expected[i].type);
  124. }
  125. }
  126. }