document-matchers.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. exports.paragraph = paragraph;
  2. exports.run = run;
  3. exports.table = table;
  4. exports.bold = new Matcher("bold");
  5. exports.italic = new Matcher("italic");
  6. exports.underline = new Matcher("underline");
  7. exports.strikethrough = new Matcher("strikethrough");
  8. exports.allCaps = new Matcher("allCaps");
  9. exports.smallCaps = new Matcher("smallCaps");
  10. exports.highlight = highlight;
  11. exports.commentReference = new Matcher("commentReference");
  12. exports.lineBreak = new BreakMatcher({breakType: "line"});
  13. exports.pageBreak = new BreakMatcher({breakType: "page"});
  14. exports.columnBreak = new BreakMatcher({breakType: "column"});
  15. exports.equalTo = equalTo;
  16. exports.startsWith = startsWith;
  17. function paragraph(options) {
  18. return new Matcher("paragraph", options);
  19. }
  20. function run(options) {
  21. return new Matcher("run", options);
  22. }
  23. function table(options) {
  24. return new Matcher("table", options);
  25. }
  26. function highlight(options) {
  27. return new HighlightMatcher(options);
  28. }
  29. function Matcher(elementType, options) {
  30. options = options || {};
  31. this._elementType = elementType;
  32. this._styleId = options.styleId;
  33. this._styleName = options.styleName;
  34. if (options.list) {
  35. this._listIndex = options.list.levelIndex;
  36. this._listIsOrdered = options.list.isOrdered;
  37. }
  38. }
  39. Matcher.prototype.matches = function(element) {
  40. return element.type === this._elementType &&
  41. (this._styleId === undefined || element.styleId === this._styleId) &&
  42. (this._styleName === undefined || (element.styleName && this._styleName.operator(this._styleName.operand, element.styleName))) &&
  43. (this._listIndex === undefined || isList(element, this._listIndex, this._listIsOrdered)) &&
  44. (this._breakType === undefined || this._breakType === element.breakType);
  45. };
  46. function HighlightMatcher(options) {
  47. options = options || {};
  48. this._color = options.color;
  49. }
  50. HighlightMatcher.prototype.matches = function(element) {
  51. return element.type === "highlight" &&
  52. (this._color === undefined || element.color === this._color);
  53. };
  54. function BreakMatcher(options) {
  55. options = options || {};
  56. this._breakType = options.breakType;
  57. }
  58. BreakMatcher.prototype.matches = function(element) {
  59. return element.type === "break" &&
  60. (this._breakType === undefined || element.breakType === this._breakType);
  61. };
  62. function isList(element, levelIndex, isOrdered) {
  63. return element.numbering &&
  64. element.numbering.level == levelIndex &&
  65. element.numbering.isOrdered == isOrdered;
  66. }
  67. function equalTo(value) {
  68. return {
  69. operator: operatorEqualTo,
  70. operand: value
  71. };
  72. }
  73. function startsWith(value) {
  74. return {
  75. operator: operatorStartsWith,
  76. operand: value
  77. };
  78. }
  79. function operatorEqualTo(first, second) {
  80. return first.toUpperCase() === second.toUpperCase();
  81. }
  82. function operatorStartsWith(first, second) {
  83. return second.toUpperCase().indexOf(first.toUpperCase()) === 0;
  84. }