struct_tree_spec.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. "use strict";
  23. var _test_utils = require("./test_utils.js");
  24. var _api = require("../../display/api.js");
  25. function equalTrees(rootA, rootB) {
  26. function walk(a, b) {
  27. expect(a.role).toEqual(b.role);
  28. expect(a.type).toEqual(b.type);
  29. expect("children" in a).toEqual("children" in b);
  30. if (!a.children) {
  31. return;
  32. }
  33. expect(a.children.length).toEqual(b.children.length);
  34. for (let i = 0; i < rootA.children.length; i++) {
  35. walk(a.children[i], b.children[i]);
  36. }
  37. }
  38. return walk(rootA, rootB);
  39. }
  40. describe("struct tree", function () {
  41. describe("getStructTree", function () {
  42. it("parses basic structure", async function () {
  43. const filename = "structure_simple.pdf";
  44. const params = (0, _test_utils.buildGetDocumentParams)(filename);
  45. const loadingTask = (0, _api.getDocument)(params);
  46. const doc = await loadingTask.promise;
  47. const page = await doc.getPage(1);
  48. const struct = await page.getStructTree();
  49. equalTrees({
  50. role: "Root",
  51. children: [{
  52. role: "Document",
  53. children: [{
  54. role: "H1",
  55. children: [{
  56. role: "NonStruct",
  57. children: [{
  58. type: "content"
  59. }]
  60. }]
  61. }, {
  62. role: "P",
  63. children: [{
  64. role: "NonStruct",
  65. children: [{
  66. type: "content"
  67. }]
  68. }]
  69. }, {
  70. role: "H2",
  71. children: [{
  72. role: "NonStruct",
  73. children: [{
  74. type: "content"
  75. }]
  76. }]
  77. }, {
  78. role: "P",
  79. children: [{
  80. role: "NonStruct",
  81. children: [{
  82. type: "content"
  83. }]
  84. }]
  85. }]
  86. }]
  87. }, struct);
  88. await loadingTask.destroy();
  89. });
  90. it("parses structure with marked content reference", async function () {
  91. const filename = "issue6782.pdf";
  92. const params = (0, _test_utils.buildGetDocumentParams)(filename);
  93. const loadingTask = (0, _api.getDocument)(params);
  94. const doc = await loadingTask.promise;
  95. const page = await doc.getPage(1);
  96. const struct = await page.getStructTree();
  97. equalTrees({
  98. role: "Root",
  99. children: [{
  100. role: "Part",
  101. children: [{
  102. role: "P",
  103. children: Array(27).fill({
  104. type: "content"
  105. })
  106. }]
  107. }]
  108. }, struct);
  109. await loadingTask.destroy();
  110. });
  111. });
  112. });