styles-reader.tests.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. var assert = require("assert");
  2. var readStylesXml = require("../../lib/docx/styles-reader").readStylesXml;
  3. var XmlElement = require("../../lib/xml").Element;
  4. var test = require("../test")(module);
  5. test('paragraph style is null if no style with that ID exists', function() {
  6. var styles = readStylesXml(
  7. new XmlElement("w:styles", {}, [])
  8. );
  9. assert.equal(styles.findParagraphStyleById("Heading1"), null);
  10. });
  11. test('paragraph style can be found by ID', function() {
  12. var styles = readStylesXml(
  13. new XmlElement("w:styles", {}, [
  14. paragraphStyleElement("Heading1", "Heading 1")
  15. ])
  16. );
  17. assert.equal(styles.findParagraphStyleById("Heading1").styleId, "Heading1");
  18. });
  19. test('table style can be found by ID', function() {
  20. var styles = readStylesXml(
  21. new XmlElement("w:styles", {}, [
  22. tableStyleElement("TableNormal", "Normal Table")
  23. ])
  24. );
  25. assert.equal(styles.findTableStyleById("TableNormal").styleId, "TableNormal");
  26. });
  27. test('character style can be found by ID', function() {
  28. var styles = readStylesXml(
  29. new XmlElement("w:styles", {}, [
  30. characterStyleElement("Heading1Char", "Heading 1 Char")
  31. ])
  32. );
  33. assert.equal(styles.findCharacterStyleById("Heading1Char").styleId, "Heading1Char");
  34. });
  35. test('paragraph and character styles are distinct', function() {
  36. var styles = readStylesXml(
  37. new XmlElement("w:styles", {}, [
  38. paragraphStyleElement("Heading1", "Heading 1"),
  39. characterStyleElement("Heading1Char", "Heading 1 Char")
  40. ])
  41. );
  42. assert.equal(styles.findCharacterStyleById("Heading1"), null);
  43. assert.equal(styles.findParagraphStyleById("Heading1Char"), null);
  44. });
  45. test('character and table styles are distinct', function() {
  46. var styles = readStylesXml(
  47. new XmlElement("w:styles", {}, [
  48. tableStyleElement("Heading1", "Heading 1")
  49. ])
  50. );
  51. assert.equal(styles.findCharacterStyleById("Heading1"), null);
  52. });
  53. test('styles include names', function() {
  54. var styles = readStylesXml(
  55. new XmlElement("w:styles", {}, [
  56. paragraphStyleElement("Heading1", "Heading 1")
  57. ])
  58. );
  59. assert.equal(styles.findParagraphStyleById("Heading1").name, "Heading 1");
  60. });
  61. test('style name is null if w:name element does not exist', function() {
  62. var styles = readStylesXml(
  63. new XmlElement("w:styles", {}, [
  64. styleWithoutWNameElement("paragraph", "Heading1"),
  65. styleWithoutWNameElement("character", "Heading1Char")
  66. ])
  67. );
  68. assert.equal(styles.findParagraphStyleById("Heading1").name, null);
  69. assert.equal(styles.findCharacterStyleById("Heading1Char").name, null);
  70. });
  71. test('numbering style is null if no style with that ID exists', function() {
  72. var styles = readStylesXml(
  73. new XmlElement("w:styles", {}, [])
  74. );
  75. assert.equal(styles.findNumberingStyleById("List1"), null);
  76. });
  77. test('numbering style has null numId if style has no paragraph properties', function() {
  78. var styles = readStylesXml(
  79. new XmlElement("w:styles", {}, [
  80. new XmlElement("w:style", {"w:type": "numbering", "w:styleId": "List1"})
  81. ])
  82. );
  83. assert.equal(styles.findNumberingStyleById("List1").numId, null);
  84. });
  85. test('numbering style has numId read from paragraph properties', function() {
  86. var styles = readStylesXml(
  87. new XmlElement("w:styles", {}, [
  88. new XmlElement("w:style", {"w:type": "numbering", "w:styleId": "List1"}, [
  89. new XmlElement("w:pPr", {}, [
  90. new XmlElement("w:numPr", {}, [
  91. new XmlElement("w:numId", {"w:val": "42"})
  92. ])
  93. ])
  94. ])
  95. ])
  96. );
  97. assert.equal(styles.findNumberingStyleById("List1").numId, "42");
  98. });
  99. function paragraphStyleElement(id, name) {
  100. return styleElement("paragraph", id, name);
  101. }
  102. function characterStyleElement(id, name) {
  103. return styleElement("character", id, name);
  104. }
  105. function tableStyleElement(id, name) {
  106. return styleElement("table", id, name);
  107. }
  108. function styleElement(type, id, name) {
  109. return new XmlElement("w:style", {"w:type": type, "w:styleId": id}, [
  110. new XmlElement("w:name", {"w:val": name}, [])
  111. ]);
  112. }
  113. function styleWithoutWNameElement(type, id) {
  114. return new XmlElement("w:style", {"w:type": type, "w:styleId": id}, []);
  115. }