Generated content string(<identifier>) uses last item without checking identifier
Hello,
I noticed when using more than one content:string(<identifier>)
for the margins (e.g. @top-left
and @top-right
) on new pages, if it can't find an element in the page it will use last item it found regardless of the "identifier".
Actual result (@top-right
) has the same content as (@top-left
):
Expected result:
( I think that's how it's supposed to work
To fix the problem in the class StringSets
I modified afterPageLayout
function, so the pageLastString is an object with the identifier as the key.
afterPageLayout(fragment) {
if ( this.pageLastString === undefined )
{
this.pageLastString = {};
}
// get the value of the previous last string
// Removed, not needed
// let previousPageLastString = this.pageLastString;
for (let name of Object.keys(this.stringSetSelectors)) {
let set = this.stringSetSelectors[name];
let selected = fragment.querySelectorAll(set.selector);
// let cssVar = previousPageLastString;
// Get the last found string for the current identifier
let cssVar = ( name in this.pageLastString ) ? this.pageLastString[name] : '';
selected.forEach((sel) => {
// push each content into the array to define in the variable the first and the last element of the page.
//this.pageLastString = selected[selected.length - 1].textContent;
// Index by identifier
this.pageLastString[name] = selected[selected.length - 1].textContent;
if (this.type === "first") {
cssVar = selected[0].textContent;
}
else if (this.type === "last") {
cssVar = selected[selected.length - 1].textContent;
}
else if (this.type === "start") {
if (sel.parentElement.firstChild === sel) {
cssVar = sel.textContent;
}
}
else if (this.type === "first-except") {
cssVar = "";
}
else {
cssVar = selected[0].textContent;
}
});
fragment.setAttribute("data-string", `string-type-${this.type}-${name}`);
// fragment.style.setProperty(`--pagedjs-string-${name}`, `"${cssVar.replace(/\\([\s\S])|(["|'])/g, "\\$1$2")}"`);
fragment.style.setProperty(`--pagedjs-string-${name}`, `"${cleanPseudoContent(cssVar)}`);
// if there is no new string on the page
if (!fragment.hasAttribute("data-string")) {
fragment.style.setProperty(`--pagedjs-string-${name}`, `"${this.pageLastString}"`);
}
}
}