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

issue with type definitions for sanitize-html #7

Closed
vijaykramesh opened this issue Nov 14, 2023 · 7 comments · Fixed by #10
Closed

issue with type definitions for sanitize-html #7

vijaykramesh opened this issue Nov 14, 2023 · 7 comments · Fixed by #10
Labels

Comments

@vijaykramesh
Copy link

It seems like I'm getting invalid javascript generated from sanitizeHtml.ts

this code

export const sanitizeHtml = (subject: string) => {
  return sanitize(subject, {
    // The default allowedTags list already includes _a lot_ of commonly used tags.
    // https://www.npmjs.com/package/sanitize-html#default-options
    //
    // I don't see a need for this to be configurable at the moment,
    // as it already covers all the layout tags, but we can revisit this if necessary.
    allowedTags: sanitize.defaults.allowedTags.concat([
      "button",
      "form",
      "img",
      "input",
      "select",
      "textarea",
    ]),
    // Setting allowedAttributes to false will allow all attributes.
    allowedAttributes: false,
  });
};

turns into

const sanitizeHtml = (subject) => {
    return (0, sanitize_html_1.default)(subject, {

        // The default allowedTags list already includes _a lot_ of commonly used tags.
        // https://www.npmjs.com/package/sanitize-html#default-options
        //
        // I don't see a need for this to be configurable at the moment,
        // as it already covers all the layout tags, but we can revisit this if necessary.
        allowedTags: sanitize_html_1.default.defaults.allowedTags.concat([
            "button",
            "form",
            "img",
            "input",
            "select",
            "textarea",
        ]),
        // Setting allowedAttributes to false will allow all attributes.
        allowedAttributes: false,
    });
};

which fails with

 TypeError: Cannot read properties of undefined (reading 'defaults')
    at sanitizeHtml (/Users/vijay/work/prj/workspace/node_modules/.pnpm/auto-playwright@1.8.1_@playwright+test@1.34.3/node_modules/auto-playwright/dist/sanitizeHtml.js:25:46)
    at getSnapshot (/Users/vijay/work/prj/workspace/node_modules/.pnpm/auto-playwright@1.8.1_@playwright+test@1.34.3/node_modules/auto-playwright/dist/getSnapshot.js:7:46)

If i manually adjust the javascript file in dist to be like

const sanitizeHtml = (subject) => {
    return (0, sanitize_html_1)(subject, {

        // The default allowedTags list already includes _a lot_ of commonly used tags.
        // https://www.npmjs.com/package/sanitize-html#default-options
        //
        // I don't see a need for this to be configurable at the moment,
        // as it already covers all the layout tags, but we can revisit this if necessary.
        allowedTags: sanitize_html_1.defaults.allowedTags.concat([
            "button",
            "form",
            "img",
            "input",
            "select",
            "textarea",
        ]),
        // Setting allowedAttributes to false will allow all attributes.
        allowedAttributes: false,
    });
};

it all seems to work... looking at the underlying type in https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/sanitize-html/index.d.ts it seems correct to me...

@lucgagan
Copy link
Owner

Do you have skipLibCheck set to true?

@vijaykramesh
Copy link
Author

ahh yep I do

@lucgagan
Copy link
Owner

It is pretty unusual to have that enabled in your project.

I will leave this open as I am unsure as to what is the underlying issue, but it is not an issue that I will prioritize. I suspect it is some odd mismatch between package and your project tsconfig.json.

@derenandrii
Copy link

@lucgagan facing the same issue

@ostapel
Copy link

ostapel commented Nov 14, 2023

I have the same issue :)

@tntexplosivesltd
Copy link

tntexplosivesltd commented Nov 15, 2023

Having this issue as well, using the default project created by calling npm init playwright.
(Playwright does not create a tsconfig.json by default)

Might be worth looking into if this is the 'default' out of box experience when using Playwright
I wonder if it's an issue with your tsconfig @lucgagan, as it appears that the JS coming from your .ts files is invalid outside your project.

Copy link

🎉 This issue has been resolved in version 1.12.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants