Forum How do I...?

Given an element ID, get the range of pages it spans

David J Prokopetz
Given an HTML element of a known ID that potentially spans multiple pages (for example, a <section> element containing many paragraphs), is there any straightforward way to get the range of pages that it spans? Assuming that this range will never be non-consecutive, the page numbers of the first and last pages on which any part of that element appears would also suffice.

(A Javascript-based solution is fine, since it'll almost certainly require some scripting to correctly format the resulting page range anyway.)
mikeday
Yes, the box-tracking API allows you to find this information with JavaScript, and you can then log it to the console or feed it back into Prince for index-generation or what have you via a two-pass solution, let me know if you need any tips on this!
David J Prokopetz
I see – so something like the attached would be the proper way to handle it?

(I may be overcomplicating things here; do an element's boxes have any guaranteed ordering, or is it necessary to assume – as my test code presently does – that the page numbers of the boxes may appear in any arbitrary order?)
  1. pagespantest.html28.4 kB
mikeday
Yep, that looks great!

The boxes should appear in document order, so you could just compare the first and last box, but being overly cautious doesn't do any harm in this case.

("document order" can get a bit complicated in the presence of page floats and footnotes, but should still at least be in page order).