PageRenderTime 21ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/test/spec/XMLUtils-test.js

https://gitlab.com/aelharrak/brackets
JavaScript | 324 lines | 195 code | 62 blank | 67 comment | 0 complexity | 0fafeefa0c3f5519e457f004dd21cda0 MD5 | raw file
  1. /*
  2. * Copyright (c) 2015 Adobe Systems Incorporated. All rights reserved.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. *
  22. */
  23. /*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */
  24. /*global define, describe, beforeEach, afterEach, it, expect */
  25. define(function (require, exports, module) {
  26. "use strict";
  27. var SpecRunnerUtils = require("spec/SpecRunnerUtils"),
  28. XMLUtils = require("language/XMLUtils");
  29. describe("XMLUtils", function () {
  30. var testContent, testDocument, testEditor;
  31. // SVG Content we will be using to run tests againts.
  32. testContent = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" +
  33. "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n" +
  34. " width=\"200\" height=\"200\" preserveAspectRatio=\"xMinYMin meet\">\n" +
  35. " <title>Brackets SVG Code Hints</title>\n" +
  36. " <rect width=\"200\" height=\"200\" baseline-shift=\"baseline\" alignment-baseline=\"alphabetic\" stroke-width=\"1\"></rect>\n" +
  37. " <rect width='160' height='160' x='20' y='20' baseline-shift='super' alignment-baseline='baseline' />\n" +
  38. " <g>\n" +
  39. " \n" +
  40. " </g>\n" +
  41. "</svg>\n";
  42. beforeEach(function () {
  43. // Create a mock svg document to run tests against.
  44. var mockEditor = SpecRunnerUtils.createMockEditor(testContent, "svg", {
  45. startLine: 0,
  46. endLine: 10
  47. });
  48. testEditor = mockEditor.editor;
  49. testDocument = mockEditor.doc;
  50. });
  51. afterEach(function () {
  52. testEditor.destroy();
  53. testEditor = null;
  54. });
  55. // Verifies tagInfo
  56. function expectTagInfo(tagInfo, tokenType, offset, exclusionListLength) {
  57. expect(tagInfo.token).not.toBe(null);
  58. expect(tagInfo.tokenType).toBe(tokenType);
  59. expect(tagInfo.offset).toBe(offset);
  60. expect(tagInfo.exclusionList.length).toBe(exclusionListLength);
  61. }
  62. // Expect tagInfo in its default state.
  63. function expectNoTagInfo(tagInfo) {
  64. expect(tagInfo.token).toBe(null);
  65. expect(tagInfo.tokenType).toBe(null);
  66. expect(tagInfo.offset).toBe(0);
  67. expect(tagInfo.exclusionList.length).toBe(0);
  68. }
  69. it("should provide tag info when creating tag", function () {
  70. var tagInfo;
  71. // After < in <svg
  72. tagInfo = XMLUtils.getTagInfo(testEditor, {line: 1, ch: 1});
  73. expectTagInfo(tagInfo, XMLUtils.TOKEN_TAG, 0, 0);
  74. // After <s in <svg
  75. tagInfo = XMLUtils.getTagInfo(testEditor, {line: 1, ch: 2});
  76. expectTagInfo(tagInfo, XMLUtils.TOKEN_TAG, 1, 0);
  77. // After <svg
  78. tagInfo = XMLUtils.getTagInfo(testEditor, {line: 1, ch: 4});
  79. expectTagInfo(tagInfo, XMLUtils.TOKEN_TAG, 3, 0);
  80. // After <title
  81. tagInfo = XMLUtils.getTagInfo(testEditor, {line: 3, ch: 10});
  82. expectTagInfo(tagInfo, XMLUtils.TOKEN_TAG, 5, 0);
  83. });
  84. it("should provide tag info when creating attribute", function () {
  85. var tagInfo;
  86. // Before xmlns
  87. tagInfo = XMLUtils.getTagInfo(testEditor, {line: 1, ch: 5});
  88. expectTagInfo(tagInfo, XMLUtils.TOKEN_ATTR, 1, 5);
  89. expect(tagInfo.exclusionList[0]).toBe("xmlns");
  90. expect(tagInfo.exclusionList[1]).toBe("xmlns:xlink");
  91. expect(tagInfo.exclusionList[2]).toBe("width");
  92. expect(tagInfo.exclusionList[3]).toBe("height");
  93. expect(tagInfo.exclusionList[4]).toBe("preserveAspectRatio");
  94. // After x in xmlns
  95. tagInfo = XMLUtils.getTagInfo(testEditor, {line: 1, ch: 6});
  96. expectTagInfo(tagInfo, XMLUtils.TOKEN_ATTR, 1, 4);
  97. expect(tagInfo.exclusionList[0]).toBe("xmlns:xlink");
  98. expect(tagInfo.exclusionList[1]).toBe("width");
  99. expect(tagInfo.exclusionList[2]).toBe("height");
  100. expect(tagInfo.exclusionList[3]).toBe("preserveAspectRatio");
  101. // After xmlns
  102. tagInfo = XMLUtils.getTagInfo(testEditor, {line: 1, ch: 10});
  103. expectTagInfo(tagInfo, XMLUtils.TOKEN_ATTR, 5, 4);
  104. expect(tagInfo.exclusionList[0]).toBe("xmlns:xlink");
  105. expect(tagInfo.exclusionList[1]).toBe("width");
  106. expect(tagInfo.exclusionList[2]).toBe("height");
  107. expect(tagInfo.exclusionList[3]).toBe("preserveAspectRatio");
  108. // Before preserveAspectRatio
  109. tagInfo = XMLUtils.getTagInfo(testEditor, {line: 2, ch: 30});
  110. expectTagInfo(tagInfo, XMLUtils.TOKEN_ATTR, 1, 5);
  111. expect(tagInfo.exclusionList[0]).toBe("height");
  112. expect(tagInfo.exclusionList[1]).toBe("width");
  113. expect(tagInfo.exclusionList[2]).toBe("xmlns:xlink");
  114. expect(tagInfo.exclusionList[3]).toBe("xmlns");
  115. expect(tagInfo.exclusionList[4]).toBe("preserveAspectRatio");
  116. });
  117. it("should provide tag info when creating attribute value", function () {
  118. var tagInfo;
  119. // Before x in xMinYMin meet
  120. tagInfo = XMLUtils.getTagInfo(testEditor, {line: 2, ch: 51});
  121. expectTagInfo(tagInfo, XMLUtils.TOKEN_VALUE, 1, 2);
  122. expect(tagInfo.exclusionList[0]).toBe("xMinYMin");
  123. expect(tagInfo.exclusionList[1]).toBe("meet");
  124. // After x in xMinYMin meet
  125. tagInfo = XMLUtils.getTagInfo(testEditor, {line: 2, ch: 52});
  126. expectTagInfo(tagInfo, XMLUtils.TOKEN_VALUE, 2, 2);
  127. expect(tagInfo.exclusionList[0]).toBe("MinYMin");
  128. expect(tagInfo.exclusionList[1]).toBe("meet");
  129. // After xMinYMin
  130. tagInfo = XMLUtils.getTagInfo(testEditor, {line: 2, ch: 59});
  131. expectTagInfo(tagInfo, XMLUtils.TOKEN_VALUE, 9, 2);
  132. expect(tagInfo.exclusionList[0]).toBe("xMinYMin");
  133. expect(tagInfo.exclusionList[1]).toBe("meet");
  134. // Before m in xMinYMin meet
  135. tagInfo = XMLUtils.getTagInfo(testEditor, {line: 2, ch: 60});
  136. expectTagInfo(tagInfo, XMLUtils.TOKEN_VALUE, 10, 2);
  137. expect(tagInfo.exclusionList[0]).toBe("xMinYMin");
  138. expect(tagInfo.exclusionList[1]).toBe("meet");
  139. // After m in xMinYMin meet
  140. tagInfo = XMLUtils.getTagInfo(testEditor, {line: 2, ch: 61});
  141. expectTagInfo(tagInfo, XMLUtils.TOKEN_VALUE, 11, 2);
  142. expect(tagInfo.exclusionList[0]).toBe("xMinYMin");
  143. expect(tagInfo.exclusionList[1]).toBe("eet");
  144. // After meet
  145. tagInfo = XMLUtils.getTagInfo(testEditor, {line: 2, ch: 64});
  146. expectTagInfo(tagInfo, XMLUtils.TOKEN_VALUE, 14, 1);
  147. expect(tagInfo.exclusionList[0]).toBe("xMinYMin");
  148. });
  149. it("should NOT provide tag info inside XML declaration", function () {
  150. var tagInfo;
  151. // After < in <?xml.
  152. tagInfo = XMLUtils.getTagInfo(testEditor, {line: 0, ch: 1});
  153. expectNoTagInfo(tagInfo);
  154. // After <? in <?xml.
  155. tagInfo = XMLUtils.getTagInfo(testEditor, {line: 0, ch: 2});
  156. expectNoTagInfo(tagInfo);
  157. // After <?xml.
  158. tagInfo = XMLUtils.getTagInfo(testEditor, {line: 0, ch: 5});
  159. expectNoTagInfo(tagInfo);
  160. // First space after <?xml.
  161. tagInfo = XMLUtils.getTagInfo(testEditor, {line: 0, ch: 6});
  162. expectNoTagInfo(tagInfo);
  163. // After v in version.
  164. tagInfo = XMLUtils.getTagInfo(testEditor, {line: 0, ch: 7});
  165. expectNoTagInfo(tagInfo);
  166. });
  167. it("should NOT provide tag info inside tag content", function () {
  168. var tagInfo;
  169. // inside <g> and </g>
  170. tagInfo = XMLUtils.getTagInfo(testEditor, {line: 7, ch: 8});
  171. expectNoTagInfo(tagInfo);
  172. // Between stroke-width="1"> and </rect>
  173. tagInfo = XMLUtils.getTagInfo(testEditor, {line: 4, ch: 110});
  174. expectNoTagInfo(tagInfo);
  175. // Inside <title>
  176. tagInfo = XMLUtils.getTagInfo(testEditor, {line: 3, ch: 20});
  177. expectNoTagInfo(tagInfo);
  178. });
  179. it("should NOT provide tag info in closing tag", function () {
  180. var tagInfo;
  181. // Between < and /
  182. tagInfo = XMLUtils.getTagInfo(testEditor, {line: 3, ch: 35});
  183. expectNoTagInfo(tagInfo);
  184. // After </
  185. tagInfo = XMLUtils.getTagInfo(testEditor, {line: 3, ch: 35});
  186. expectNoTagInfo(tagInfo);
  187. });
  188. it("should NOT provide tag info after the attribute value quote", function () {
  189. var tagInfo;
  190. // After /svg"
  191. tagInfo = XMLUtils.getTagInfo(testEditor, {line: 1, ch: 39});
  192. expectNoTagInfo(tagInfo);
  193. // After xlink"
  194. tagInfo = XMLUtils.getTagInfo(testEditor, {line: 1, ch: 82});
  195. expectNoTagInfo(tagInfo);
  196. // After meet"
  197. tagInfo = XMLUtils.getTagInfo(testEditor, {line: 2, ch: 65});
  198. expectNoTagInfo(tagInfo);
  199. });
  200. it("should NOT provide tag info in case the cursor is between an attribute and =", function () {
  201. var tagInfo;
  202. testDocument.replaceRange(" ", {line: 1, ch: 51});
  203. tagInfo = XMLUtils.getTagInfo(testEditor, {line: 1, ch: 52});
  204. expectNoTagInfo(tagInfo);
  205. });
  206. it("should NOT provide tag info in case the cursor is between an equal sign (=) and an attribute value.", function () {
  207. var tagInfo;
  208. testDocument.replaceRange(" ", {line: 4, ch: 50});
  209. tagInfo = XMLUtils.getTagInfo(testEditor, {line: 4, ch: 51});
  210. expectNoTagInfo(tagInfo);
  211. });
  212. it("should provide query for first value", function () {
  213. var tagInfo;
  214. // After =
  215. tagInfo = XMLUtils.getTagInfo(testEditor, {line: 2, ch: 50});
  216. expect(XMLUtils.getValueQuery(tagInfo)).toBe("");
  217. // Afters ="
  218. tagInfo = XMLUtils.getTagInfo(testEditor, {line: 2, ch: 51});
  219. expect(XMLUtils.getValueQuery(tagInfo)).toBe("");
  220. // After ="x
  221. tagInfo = XMLUtils.getTagInfo(testEditor, {line: 2, ch: 52});
  222. expect(XMLUtils.getValueQuery(tagInfo)).toBe("x");
  223. // After ="xMin
  224. tagInfo = XMLUtils.getTagInfo(testEditor, {line: 2, ch: 55});
  225. expect(XMLUtils.getValueQuery(tagInfo)).toBe("xMin");
  226. // After ="xMinYMin
  227. tagInfo = XMLUtils.getTagInfo(testEditor, {line: 2, ch: 59});
  228. expect(XMLUtils.getValueQuery(tagInfo)).toBe("xMinYMin");
  229. });
  230. it("should provide query for second value", function () {
  231. var tagInfo;
  232. // Before meet"
  233. tagInfo = XMLUtils.getTagInfo(testEditor, {line: 2, ch: 60});
  234. expect(XMLUtils.getValueQuery(tagInfo)).toBe("");
  235. // After m in meet
  236. tagInfo = XMLUtils.getTagInfo(testEditor, {line: 2, ch: 61});
  237. expect(XMLUtils.getValueQuery(tagInfo)).toBe("m");
  238. // After meet and before "
  239. tagInfo = XMLUtils.getTagInfo(testEditor, {line: 2, ch: 64});
  240. expect(XMLUtils.getValueQuery(tagInfo)).toBe("meet");
  241. });
  242. it("should provide query for middle value", function () {
  243. var tagInfo;
  244. // between xMinYMin and meet
  245. testDocument.replaceRange(" ", {line: 2, ch: 59});
  246. tagInfo = XMLUtils.getTagInfo(testEditor, {line: 2, ch: 60});
  247. expect(XMLUtils.getValueQuery(tagInfo)).toBe("");
  248. // After x, between xMinYMin and meet
  249. testDocument.replaceRange("x", {line: 2, ch: 60});
  250. tagInfo = XMLUtils.getTagInfo(testEditor, {line: 2, ch: 61});
  251. expect(XMLUtils.getValueQuery(tagInfo)).toBe("x");
  252. // After xMax, between xMinYMin and meet
  253. testDocument.replaceRange("Max", {line: 2, ch: 61});
  254. tagInfo = XMLUtils.getTagInfo(testEditor, {line: 2, ch: 64});
  255. expect(XMLUtils.getValueQuery(tagInfo)).toBe("xMax");
  256. // After xMaxYMax, between xMinYMin and meet
  257. testDocument.replaceRange("YMax", {line: 2, ch: 64});
  258. tagInfo = XMLUtils.getTagInfo(testEditor, {line: 2, ch: 68});
  259. expect(XMLUtils.getValueQuery(tagInfo)).toBe("xMaxYMax");
  260. });
  261. });
  262. });