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