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: clone and copy styles #184

Merged
merged 3 commits into from
May 15, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions js/shared/pollimporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,31 @@
*/
/* global WebImporter */

const DEFAULT_SUPPORTED_STYLES = ['background-image'];

function deepCloneWithStyles(document, styles = DEFAULT_SUPPORTED_STYLES) {
const clone = document.cloneNode(true);

const applyStyles = (nodeSrc, nodeDest) => {
const style = window.getComputedStyle(nodeSrc, null);

styles.forEach((styleName) => {
if (style[styleName]) {
nodeDest.style[styleName] = style[styleName];
}
});

if (nodeSrc.children && nodeSrc.children.length > 0) {
const destChildren = [...nodeDest.children];
[...nodeSrc.children].forEach((child, i) => {
applyStyles(child, destChildren[i]);
});
}
};
applyStyles(document.body, clone.body);
return clone;
}

export default class PollImporter {
constructor(cfg) {
this.config = {
Expand Down Expand Up @@ -113,10 +138,13 @@ export default class PollImporter {
console.log(`Starting transformation of ${url} with import file: ${this.projectTransformFileURL || 'none (default)'}`);
try {
let results;

const documentClone = deepCloneWithStyles(document, this.projectTransform?.REQUIRED_STYLES);

if (includeDocx) {
const out = await WebImporter.html2docx(
url,
document.cloneNode(true),
documentClone,
this.projectTransform,
params,
);
Expand All @@ -129,7 +157,7 @@ export default class PollImporter {
} else {
const out = await WebImporter.html2md(
url,
document.cloneNode(true),
documentClone,
this.projectTransform,
params,
);
Expand Down