{"id":13436932,"url":"https://github.com/christo-pr/dangerously-set-html-content-v1","last_synced_at":"2025-03-19T05:31:13.474Z","repository":{"id":74073924,"uuid":"227670873","full_name":"christo-pr/dangerously-set-html-content-v1","owner":"christo-pr","description":"Render raw html on your own risk!","archived":false,"fork":false,"pushed_at":"2021-08-23T15:29:44.000Z","size":361,"stargazers_count":56,"open_issues_count":1,"forks_count":8,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-27T21:48:47.767Z","etag":null,"topics":["dangerouslysetinnerhtml","innerhtml","raw-html","reactjs"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/dangerously-set-html-content","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/christo-pr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-12-12T18:25:47.000Z","updated_at":"2023-04-10T14:09:38.000Z","dependencies_parsed_at":null,"dependency_job_id":"a2554fe6-cea6-482b-b726-758a150f7cab","html_url":"https://github.com/christo-pr/dangerously-set-html-content-v1","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/christo-pr%2Fdangerously-set-html-content-v1","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/christo-pr%2Fdangerously-set-html-content-v1/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/christo-pr%2Fdangerously-set-html-content-v1/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/christo-pr%2Fdangerously-set-html-content-v1/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/christo-pr","download_url":"https://codeload.github.com/christo-pr/dangerously-set-html-content-v1/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244364829,"owners_count":20441460,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dangerouslysetinnerhtml","innerhtml","raw-html","reactjs"],"created_at":"2024-07-31T03:00:53.125Z","updated_at":"2025-03-19T05:31:13.468Z","avatar_url":"https://github.com/christo-pr.png","language":"JavaScript","funding_links":[],"categories":["HarmonyOS"],"sub_categories":["Windows Manager"],"readme":"\u003ccenter\u003e\n\u003ch1\u003e\n⚠️⚠️⚠️\ndangerously-set-html-content\n⚠️⚠️⚠️\n\u003c/h1\u003e\n\u003c/center\u003e\n\n\u003e Render raw html at your own risk!\n\n[![NPM](https://img.shields.io/npm/v/dangerously-set-html-content.svg)](https://www.npmjs.com/package/dangerously-set-html-content) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)\n\n\n## Context\n\nHere's a blog post that explain more in detail:\n\n- [Render dangerous content with React](https://dev.to/christo_pr/render-dangerous-content-with-react-2j7j)\n\n## TL;DR\n\nReact uses `dangerouslySetInnerHtml` prop to render raw html, and works pretty much well for almost all the cases, but what if your html has some `scripts` tags inside??\n\nWhen you use `dangerouslySetInnerHtml` on a component, internally react is using the `innerHtml` property of the node to set the content, which for [safety purposes](https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML#Security_considerations) doesn't execute any javascript.\n\nThis behavior seemed very odd to me (I mean the prop name contains the word `dangerously`, and also you need to pass an object with a `__html` propery, which is on purpose, so you really know what you doing), although has totally sense now, still doesn't solve my issue\n\nAfter a little bit of search I found that the `document` has something called [Range](https://developer.mozilla.org/en-US/docs/Web/API/Range), this API let you create a fragment of the document, so using that I created `dangerously-set-html-content`.\n\nThis React component renders html from a string, with the plus of executing all the js code that html contains!! 🎉\n\n🚨🚨 **USE IT AT YOUR OWN RISK** 🚨🚨\n\n## Install\n\n```bash\nyarn add dangerously-set-html-content\n// or\n// npm install --save dangerously-set-html-content\n```\n\n## Usage\n\n```jsx\nimport React from 'react'\n\nimport InnerHTML from 'dangerously-set-html-content'\n\nfunction Example {\n\n  const html = `\n    \u003cdiv\u003eThis wil be rendered\u003c/div\u003e\n    \u003cscript\u003e\n      alert('testing')\n    \u003c/script\u003e\n\n  `\n\n  return (\n    \u003cInnerHTML html={html} /\u003e\n  )\n}\n```\n\nThis will also work for scripts with the `src` attribute set it\n\n## License\n\nMIT © [christo-pr](https://github.com/christo-pr)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchristo-pr%2Fdangerously-set-html-content-v1","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchristo-pr%2Fdangerously-set-html-content-v1","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchristo-pr%2Fdangerously-set-html-content-v1/lists"}