Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: running element page counter incorrect #1327

Merged
merged 7 commits into from
May 27, 2024

Conversation

EnochGao
Copy link
Sponsor Member

@EnochGao EnochGao commented May 11, 2024

#1325
I'm not sure if this modification is elegant, but the result seems to be correct. I'm not sure how to handle the total number of pages

Copy link

vercel bot commented May 11, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
vivliostyle ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 27, 2024 1:13am

@MurakamiShinyu
Copy link
Member

Thank you for the fix! I will check tomorrow.

@EnochGao
Copy link
Sponsor Member Author

and target-counter in running element equally incorrect🙋🙋🙋

Comment on lines 170 to 174
let value = arr[0];
if (arr[1]) {
value = arr[1];
}
return format(value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let value = arr[0];
if (arr[1]) {
value = arr[1];
}
return format(value);
return format(arr[arr.length - 1]);

value = arr[1] is not good when counter-reset: page happens multiple times, for example:

  @page :nth(2) {
    counter-reset: page 20;
  }
  @page :nth(3) {
    counter-reset: page 30;
  }

@MurakamiShinyu
Copy link
Member

I'm not sure how to handle the total number of pages

I found the problem in CounterStore.finishLastPage() in counters.ts:

finishLastPage(viewport: Vgen.Viewport) {
const nodes = viewport.root.querySelectorAll(`[${PAGES_COUNTER_ATTR}]`);
const pages = this.currentPageCounters["page"][0];
for (const node of nodes) {
const key = node.getAttribute(PAGES_COUNTER_ATTR);
const i = this.pagesCounterExprs.findIndex((o) => o.expr.key === key);
Asserts.assert(i >= 0);
node.textContent = this.pagesCounterExprs[i].format([pages]);
}
}

In this code, the "pages" counter value is obtained from the "page" counter value at the last page:

    const pages = this.currentPageCounters["page"][0];

It doesn't work when counter-reset: page is used for the same reason as return format(arr[0]); in getPageCounterVal(). However, there is a further problem.

In the CSS Paged Media spec, the definition of the pages counter is "Its value is always the total number of pages in the document" and not same as the "page" counter value at the last page. So the "pages" counter value should be obtained from the number of generated pages.

Suggested change:

    const pages = viewport.contentContainer.childElementCount;

@MurakamiShinyu
Copy link
Member

@MurakamiShinyu
Copy link
Member

@EnochGao Thanks for the fix and the test case "counter_in_running.html".

The fix seems almost good, but I found a problem that it does not work when a page-based
counter other than "page" is used. Example: https://gist.githubusercontent.com/MurakamiShinyu/7616ab180e72e38addafc1a7ea44d8d0/raw/counter_in_running_custom_page.html

@MurakamiShinyu
Copy link
Member

Thanks. The fix 72fa5bc is good for custom page-based counter, but still has a problem with element-based counter.

I made a test case which tests element-based counter in addition to custom page-based counter:

https://gist.githubusercontent.com/MurakamiShinyu/7616ab180e72e38addafc1a7ea44d8d0/raw/counter_in_running_custom_page2.html

Test: https://vivliostyle-git-fork-enochgao-fix-counter-vivliostyle.vercel.app/#src=https://gist.githubusercontent.com/MurakamiShinyu/7616ab180e72e38addafc1a7ea44d8d0/raw/counter_in_running_custom_page2.html

The "(Section 11)" in the page header is wrong. It should be "(Section 2)".


I found this problem is caused by the following code in finishLastPage():

      const transformedId = node.getAttribute(TARGET_PAGE_COUNTER_ID_ATTR);
      const expr = this.targetPageCounterExprs.find(
        (o) => o.transformedId === transformedId,
      );

There are multiple target counter expressions with the same transformedId in this.targetPageCounterExprs and the wrong one is selected. I think we should use key instead of transformedId to identify the target counter expression. So the code should be changed to:

      const key = node.getAttribute(TARGET_PAGE_COUNTER_ATTR);
      const expr = this.targetPageCounterExprs.find((o) => o.expr.key === key);

If this change is ok, the TARGET_PAGE_COUNTER_ID_ATTR attribute is no longer needed and can be removed.

Also the naming TARGET_PAGE_COUNTER_ATTR is no longer appropriate because it is used for not only page-based counters but also element-based counters. I suggest renaming it to TARGET_COUNTER_ATTR and TARGET_PAGE_COUNTER_IN_RUNNING_ATTR to TARGET_COUNTER_IN_RUNNING_ATTR, and change their values to "data-vivliostyle-target-counter" and "data-vivliostyle-target-counter-in-running" respectively.

Please check the proposed changes in the following diff:
https://gist.github.com/MurakamiShinyu/d175de0ddb9175e3f2ba711735aa0e17

@EnochGao
Copy link
Sponsor Member Author

I see. Thanks for the correction.

Comment on lines 192 to 193
<div class="reset-page">reset page</div>

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This <div class="reset-page">reset page</div> element is not used for resetting page counter. So I think this should be removed.

Suggested change
<div class="reset-page">reset page</div>

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

Copy link
Member

@MurakamiShinyu MurakamiShinyu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! LGTM 💯

@EnochGao
Copy link
Sponsor Member Author

Thanks! LGTM 💯

Thanks for the correction. Made me know more about this place

@MurakamiShinyu MurakamiShinyu merged commit 8c390b8 into vivliostyle:master May 27, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants