style-reader.tests.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. var assert = require("assert");
  2. var htmlPaths = require("../lib/styles/html-paths");
  3. var documentMatchers = require("../lib/styles/document-matchers");
  4. var styleReader = require("../lib/style-reader");
  5. var results = require("../lib/results");
  6. var test = require("./test")(module);
  7. var readHtmlPath = styleReader.readHtmlPath;
  8. var readDocumentMatcher = styleReader.readDocumentMatcher;
  9. var readStyle = styleReader.readStyle;
  10. test('styleReader.readHtmlPath', {
  11. 'reads empty path': function() {
  12. assertHtmlPath("", htmlPaths.empty);
  13. },
  14. 'reads single element': function() {
  15. assertHtmlPath("p", htmlPaths.elements(["p"]));
  16. },
  17. 'reads choice of elements': function() {
  18. assertHtmlPath(
  19. "ul|ol",
  20. htmlPaths.elements([
  21. htmlPaths.element(["ul", "ol"])
  22. ])
  23. );
  24. },
  25. 'reads nested elements': function() {
  26. assertHtmlPath("ul > li", htmlPaths.elements(["ul", "li"]));
  27. },
  28. 'reads class on element': function() {
  29. var expected = htmlPaths.elements([
  30. htmlPaths.element("p", {"class": "tip"})
  31. ]);
  32. assertHtmlPath("p.tip", expected);
  33. },
  34. 'reads class with escaped colon': function() {
  35. var expected = htmlPaths.elements([
  36. htmlPaths.element("p", {"class": "a:b"})
  37. ]);
  38. assertHtmlPath("p.a\\:b", expected);
  39. },
  40. 'reads multiple classes on element': function() {
  41. var expected = htmlPaths.elements([
  42. htmlPaths.element("p", {"class": "tip help"})
  43. ]);
  44. assertHtmlPath("p.tip.help", expected);
  45. },
  46. 'reads attribute on element': function() {
  47. var expected = htmlPaths.elements([
  48. htmlPaths.element("p", {"lang": "fr"})
  49. ]);
  50. assertHtmlPath("p[lang='fr']", expected);
  51. },
  52. 'reads multiple attributes on element': function() {
  53. var expected = htmlPaths.elements([
  54. htmlPaths.element("p", {"lang": "fr", "data-x": "y"})
  55. ]);
  56. assertHtmlPath("p[lang='fr'][data-x='y']", expected);
  57. },
  58. 'reads when element must be fresh': function() {
  59. var expected = htmlPaths.elements([
  60. htmlPaths.element("p", {}, {"fresh": true})
  61. ]);
  62. assertHtmlPath("p:fresh", expected);
  63. },
  64. 'reads separator for elements': function() {
  65. var expected = htmlPaths.elements([
  66. htmlPaths.element("p", {}, {separator: "x"})
  67. ]);
  68. assertHtmlPath("p:separator('x')", expected);
  69. },
  70. 'reads separator with escape sequence': function() {
  71. var expected = htmlPaths.elements([
  72. htmlPaths.element("p", {}, {separator: "\r\n\t\'\\"})
  73. ]);
  74. assertHtmlPath("p:separator('\\r\\n\\t\\'\\\\')", expected);
  75. },
  76. 'reads ignore element': function() {
  77. assertHtmlPath("!", htmlPaths.ignore);
  78. }
  79. });
  80. function assertHtmlPath(input, expected) {
  81. assert.deepEqual(readHtmlPath(input), results.success(expected));
  82. }
  83. test("styleReader.readDocumentMatcher", {
  84. "reads plain paragraph": function() {
  85. assertDocumentMatcher("p", documentMatchers.paragraph());
  86. },
  87. "reads paragraph with style ID": function() {
  88. assertDocumentMatcher(
  89. "p.Heading1",
  90. documentMatchers.paragraph({styleId: "Heading1"})
  91. );
  92. },
  93. "reads paragraph with exact style name": function() {
  94. assertDocumentMatcher(
  95. "p[style-name='Heading 1']",
  96. documentMatchers.paragraph({styleName: documentMatchers.equalTo("Heading 1")})
  97. );
  98. },
  99. "reads paragraph with style name prefix": function() {
  100. assertDocumentMatcher(
  101. "p[style-name^='Heading']",
  102. documentMatchers.paragraph({styleName: documentMatchers.startsWith("Heading")})
  103. );
  104. },
  105. "reads p:ordered-list(1) as ordered list with index of 0": function() {
  106. assertDocumentMatcher(
  107. "p:ordered-list(1)",
  108. documentMatchers.paragraph({list: {isOrdered: true, levelIndex: 0}})
  109. );
  110. },
  111. "reads p:unordered-list(1) as unordered list with index of 0": function() {
  112. assertDocumentMatcher(
  113. "p:unordered-list(1)",
  114. documentMatchers.paragraph({list: {isOrdered: false, levelIndex: 0}})
  115. );
  116. },
  117. "reads plain run": function() {
  118. assertDocumentMatcher(
  119. "r",
  120. documentMatchers.run()
  121. );
  122. },
  123. "reads plain table": function() {
  124. assertDocumentMatcher("table", documentMatchers.table());
  125. },
  126. "reads table with style ID": function() {
  127. assertDocumentMatcher(
  128. "table.TableNormal",
  129. documentMatchers.table({
  130. styleId: "TableNormal"
  131. })
  132. );
  133. },
  134. "reads table with style name": function() {
  135. assertDocumentMatcher(
  136. "table[style-name='Normal Table']",
  137. documentMatchers.table({
  138. styleName: documentMatchers.equalTo("Normal Table")
  139. })
  140. );
  141. },
  142. "reads bold": function() {
  143. assertDocumentMatcher(
  144. "b",
  145. documentMatchers.bold
  146. );
  147. },
  148. "reads italic": function() {
  149. assertDocumentMatcher(
  150. "i",
  151. documentMatchers.italic
  152. );
  153. },
  154. "reads underline": function() {
  155. assertDocumentMatcher(
  156. "u",
  157. documentMatchers.underline
  158. );
  159. },
  160. "reads strikethrough": function() {
  161. assertDocumentMatcher(
  162. "strike",
  163. documentMatchers.strikethrough
  164. );
  165. },
  166. "reads all-caps": function() {
  167. assertDocumentMatcher(
  168. "all-caps",
  169. documentMatchers.allCaps
  170. );
  171. },
  172. "reads small-caps": function() {
  173. assertDocumentMatcher(
  174. "small-caps",
  175. documentMatchers.smallCaps
  176. );
  177. },
  178. "reads highlight without color": function() {
  179. assertDocumentMatcher(
  180. "highlight",
  181. documentMatchers.highlight()
  182. );
  183. },
  184. "reads highlight with color": function() {
  185. assertDocumentMatcher(
  186. "highlight[color='yellow']",
  187. documentMatchers.highlight({color: "yellow"})
  188. );
  189. },
  190. "reads comment-reference": function() {
  191. assertDocumentMatcher(
  192. "comment-reference",
  193. documentMatchers.commentReference
  194. );
  195. },
  196. "reads line breaks": function() {
  197. assertDocumentMatcher(
  198. "br[type='line']",
  199. documentMatchers.lineBreak
  200. );
  201. },
  202. "reads page breaks": function() {
  203. assertDocumentMatcher(
  204. "br[type='page']",
  205. documentMatchers.pageBreak
  206. );
  207. },
  208. "reads column breaks": function() {
  209. assertDocumentMatcher(
  210. "br[type='column']",
  211. documentMatchers.columnBreak
  212. );
  213. }
  214. });
  215. function assertDocumentMatcher(input, expected) {
  216. assert.deepEqual(readDocumentMatcher(input), results.success(expected));
  217. }
  218. test("styleReader.read", {
  219. "document matcher is mapped to HTML path using arrow": function() {
  220. assertStyleMapping(
  221. "p => h1",
  222. {
  223. from: documentMatchers.paragraph(),
  224. to: htmlPaths.elements(["h1"])
  225. }
  226. );
  227. },
  228. "reads style mapping with no HTML path": function() {
  229. assertStyleMapping(
  230. "r =>",
  231. {
  232. from: documentMatchers.run(),
  233. to: htmlPaths.empty
  234. }
  235. );
  236. },
  237. "error when not all input is consumed": function() {
  238. assert.deepEqual(
  239. readStyle("r => span a"),
  240. new results.Result(null, [results.warning("Did not understand this style mapping, so ignored it: r => span a\nError was at character number 10: Expected end but got whitespace")])
  241. );
  242. }
  243. });
  244. function assertStyleMapping(input, expected) {
  245. assert.deepEqual(readStyle(input), results.success(expected));
  246. }