From 169739a4fc38e38afb8a81398ee05ea64e16913c Mon Sep 17 00:00:00 2001 From: Simon Backx Date: Tue, 2 May 2023 12:11:15 +0200 Subject: [PATCH] Fixed counter-reset not working in combination with inline styles An error is thrown in `resetCounter` when adding an `counter-reset` inline style to an element in combination with a different style (e.g., in head). This error is caused by the fact that the `addProps` method is called when an element doesn't have `styleProps` (in the `if (!el.styleProps) {` block), before a default value for `counterProps` is set. This is fixed by assigning a default value for `counterProps` before calling `addProps`. A test case is also added to reproduce the issue and to prevent future regressions. --- lib/inline.js | 12 ++++++------ test/cases/juice-content/counter-reset.css | 0 test/cases/juice-content/counter-reset.html | 12 ++++++++++++ test/cases/juice-content/counter-reset.json | 4 ++++ test/cases/juice-content/counter-reset.out | 8 ++++++++ 5 files changed, 30 insertions(+), 6 deletions(-) create mode 100644 test/cases/juice-content/counter-reset.css create mode 100644 test/cases/juice-content/counter-reset.html create mode 100644 test/cases/juice-content/counter-reset.json create mode 100644 test/cases/juice-content/counter-reset.out diff --git a/lib/inline.js b/lib/inline.js index 4ffaa05..475a62e 100644 --- a/lib/inline.js +++ b/lib/inline.js @@ -124,6 +124,12 @@ function inlineDocument($, css, options) { return; } + if (!el.counterProps) { + el.counterProps = el.parent && el.parent.counterProps + ? Object.create(el.parent.counterProps) + : {}; + } + if (pseudoElementType) { var pseudoElPropName = 'pseudo' + pseudoElementType; var pseudoEl = el[pseudoElPropName]; @@ -150,12 +156,6 @@ function inlineDocument($, css, options) { editedElements.push(el); } - if (!el.counterProps) { - el.counterProps = el.parent && el.parent.counterProps - ? Object.create(el.parent.counterProps) - : {}; - } - function resetCounter(el, value) { var tokens = value.split(/\s+/); diff --git a/test/cases/juice-content/counter-reset.css b/test/cases/juice-content/counter-reset.css new file mode 100644 index 0000000..e69de29 diff --git a/test/cases/juice-content/counter-reset.html b/test/cases/juice-content/counter-reset.html new file mode 100644 index 0000000..863d222 --- /dev/null +++ b/test/cases/juice-content/counter-reset.html @@ -0,0 +1,12 @@ + + + + + +

Cool!

+ + diff --git a/test/cases/juice-content/counter-reset.json b/test/cases/juice-content/counter-reset.json new file mode 100644 index 0000000..a2c47aa --- /dev/null +++ b/test/cases/juice-content/counter-reset.json @@ -0,0 +1,4 @@ +{ + "removeStyleTags": true, + "inlinePseudoElements": true +} diff --git a/test/cases/juice-content/counter-reset.out b/test/cases/juice-content/counter-reset.out new file mode 100644 index 0000000..5783920 --- /dev/null +++ b/test/cases/juice-content/counter-reset.out @@ -0,0 +1,8 @@ + + + + + +

Cool!

+ +