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

CSP Issues #12

Open
theimo1221 opened this issue Nov 15, 2021 · 6 comments
Open

CSP Issues #12

theimo1221 opened this issue Nov 15, 2021 · 6 comments

Comments

@theimo1221
Copy link

Hello @zVolodymyr,

Unfortunately the creation of those style blocks result in violations to csp settings in regards to inline-styles.
Do you see any possibility to introduce an option, to style all elements directly with js calls, instead of providing a style block?

Thanks and best regards

Thiemo

@theimo1221
Copy link
Author

I'm working on this.

@VolodymyrBaydalka
Copy link
Owner

Hi,
Potentially it's possible to apply all styles via element styles, but there come issues

  • custom fonts will not be supported
  • numbering may not work correctly

@isocroft
Copy link

@theimo1221 I think a good compromise might be to load the docxjs script and other helper scripts like jszip inside an iframe and then use the CSP (<iframe csp="" ) attribute.

Have you tried this ?

@theimo1221
Copy link
Author

I did and it didn't work in our application.
But I found a proper work around and added it as a setting.
You can check my fork, from which I'll open more PR's once the current ones are merged.

@VolodymyrBaydalka
Copy link
Owner

VolodymyrBaydalka commented Nov 17, 2021

As far as I know csp restricts from appending <style> into document, but not from creating <style> element. Knowing that, there is one quick workarround but only for chrome (for know), but maybe it will give you some ideas for futher investigation:

var container = document.getElementById("doc");
// create dummy container for styles
var stylesContainer = document.createElement("div");

docx.renderAsync(file, container, stylesContainer).then(function(x){ 
    // convert rendered styles into CSSStyleSheet
    var styles = Array.from(stylesContainer.children).map(s => {
        var css = new CSSStyleSheet();
        css.replaceSync(s.innerHTML);
        return css;
    }); 

    // apply styles to document
    document.adoptedStyleSheets = styles;
    // or apply styles to each element of content container manually
});

@theimo1221
Copy link
Author

#17 resolves the topic for me.

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

No branches or pull requests

3 participants