struct_tree_spec.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 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.lang).toEqual(b.lang);
  29. expect(a.type).toEqual(b.type);
  30. expect("children" in a).toEqual("children" in b);
  31. if (!a.children) {
  32. return;
  33. }
  34. expect(a.children.length).toEqual(b.children.length);
  35. for (let i = 0; i < rootA.children.length; i++) {
  36. walk(a.children[i], b.children[i]);
  37. }
  38. }
  39. return walk(rootA, rootB);
  40. }
  41. describe("struct tree", function () {
  42. describe("getStructTree", function () {
  43. it("parses basic structure", async function () {
  44. const filename = "structure_simple.pdf";
  45. const params = (0, _test_utils.buildGetDocumentParams)(filename);
  46. const loadingTask = (0, _api.getDocument)(params);
  47. const doc = await loadingTask.promise;
  48. const page = await doc.getPage(1);
  49. const struct = await page.getStructTree();
  50. equalTrees({
  51. role: "Root",
  52. children: [{
  53. role: "Document",
  54. lang: "en-US",
  55. children: [{
  56. role: "H1",
  57. children: [{
  58. role: "NonStruct",
  59. children: [{
  60. type: "content"
  61. }]
  62. }]
  63. }, {
  64. role: "P",
  65. children: [{
  66. role: "NonStruct",
  67. children: [{
  68. type: "content"
  69. }]
  70. }]
  71. }, {
  72. role: "H2",
  73. children: [{
  74. role: "NonStruct",
  75. children: [{
  76. type: "content"
  77. }]
  78. }]
  79. }, {
  80. role: "P",
  81. children: [{
  82. role: "NonStruct",
  83. children: [{
  84. type: "content"
  85. }]
  86. }]
  87. }]
  88. }]
  89. }, struct);
  90. await loadingTask.destroy();
  91. });
  92. it("parses structure with marked content reference", async function () {
  93. const filename = "issue6782.pdf";
  94. const params = (0, _test_utils.buildGetDocumentParams)(filename);
  95. const loadingTask = (0, _api.getDocument)(params);
  96. const doc = await loadingTask.promise;
  97. const page = await doc.getPage(1);
  98. const struct = await page.getStructTree();
  99. equalTrees({
  100. role: "Root",
  101. children: [{
  102. role: "Part",
  103. children: [{
  104. role: "P",
  105. children: Array(27).fill({
  106. type: "content"
  107. })
  108. }]
  109. }]
  110. }, struct);
  111. await loadingTask.destroy();
  112. });
  113. });
  114. });