{"id":20504503,"url":"https://github.com/candyframework/html-filter","last_synced_at":"2025-09-20T01:29:06.096Z","repository":{"id":57267388,"uuid":"344376191","full_name":"candyframework/html-filter","owner":"candyframework","description":"A library for filtering HTML tags and attributes written in JavaScript","archived":false,"fork":false,"pushed_at":"2022-06-01T01:48:00.000Z","size":53,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-03-27T11:11:59.421Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/candyframework.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}},"created_at":"2021-03-04T06:41:54.000Z","updated_at":"2022-06-13T02:26:35.000Z","dependencies_parsed_at":"2022-09-02T05:40:50.338Z","dependency_job_id":null,"html_url":"https://github.com/candyframework/html-filter","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/candyframework%2Fhtml-filter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/candyframework%2Fhtml-filter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/candyframework%2Fhtml-filter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/candyframework%2Fhtml-filter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/candyframework","download_url":"https://codeload.github.com/candyframework/html-filter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248782279,"owners_count":21160716,"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":[],"created_at":"2024-11-15T19:38:27.425Z","updated_at":"2025-09-20T01:29:00.993Z","avatar_url":"https://github.com/candyframework.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## html 过滤库 A library for filtering HTML tags and attributes written in JavaScript\n\n## 4.0 变化\n\n从 4.0 开始，过滤标签时不再删除子元素，只会关注标签是否需要过滤，行为如下\n\n```\n// before 4.0\nhtmlFilter.allowedTags = { p: null, br: null };\nhtmlFilter.filter('\u003cdiv\u003e\u003cbr /\u003e\u003cp\u003ehello\u003c/p\u003e\u003c/div\u003e');\n\nresult is: ''\n```\n\n```\n// 4.0 +\nhtmlFilter.allowedTags = { p: null, br: null };\nhtmlFilter.filter('\u003cdiv\u003e\u003cbr /\u003e\u003cp\u003ehello\u003c/p\u003e\u003c/div\u003e');\n\nresult is: '\u003cbr /\u003e\u003cp\u003ehello\u003c/p\u003e'\n```\n\n## More\n\n[php version html-filter](https://packagist.org/packages/afuafuyo/html-filter-php)\n\n[js version html-filter](https://www.npmjs.com/package/html-filter)\n\n### change log\n\n+ 2021-05-21 publish 4.3.0 add custom element support\n\n+ 2021-04-18 publish 4.2.2 fix pure text filter bug\n\n+ 2021-02-07 publish 4.2 all tags wouled be removed will the allowedTags attribute is not init with a whitelist\n\n+ 2021-02-03 publish 4.1 optimize the regexp\n\n+ 2019-01-30 publish 3.0.0 Add browser support\n\n+ 2018-10-11 publish 2.0.0 Change parse() function to filter()\n\n+ 2018-12-13 publish 2.0.4 Separate the dom part from main lib\n\n### 浏览器中使用 ( Browser use )\n\n```javascript\n\u003cscript src=\"index.js\"\u003e\u003c/script\u003e\n\u003cscript\u003e\nvar htmlFilter = new HtmlFilter();\n// todo sth\n\u003c/script\u003e\n```\n\n### Node.js\n\n```javascript\nconst HtmlFilter = require('html-filter');\nconst htmlFilter = new HtmlFilter();\n// todo sth\n```\n\n### 过滤标签和属性 - filter tags and attributes\n\n```javascript\nvar html =\n`\n\u003ch1\u003e\u003cbr /\u003e\u003cp\u003euser info\u003c/p\u003e\u003c/h1\u003e\n\u003cdiv id=\"myid\" style=\"border: 1px solid red\"\u003e\n    \u003cdiv style=\"font-weight: bold\"\u003eHello\u003c/div\u003e\n    \u003cbr /\u003e\n    \u003cdiv id=\"user\" onclick=\"alert(1)\"\u003e\n        \u003cp\u003ezhangsan\u003c/p\u003e\n        \u003cp\u003emale\u003c/p\u003e\n        \u003cp\u003e20\u003c/p\u003e\n    \u003c/div\u003e\n\u003c/div\u003e\n`;\n\nvar obj = new HtmlFilter();\nobj.allowedTags = {\n    p: null,  // not support attr\n    div: {id: 1, style: 1},  // support id and style attr\n    br: null\n};\n\nconsole.log(obj.filter(html));\n\n// the console result:\n\u003cbr /\u003e\u003cp\u003euser info\u003c/p\u003e\n\u003cdiv id=\"myid\" style=\"border: 1px solid red\"\u003e\n    \u003cdiv style=\"font-weight: bold\"\u003eHello\u003c/div\u003e\n    \u003cbr /\u003e\n    \u003cdiv id=\"user\"\u003e\n        \u003cp\u003ezhangsan\u003c/p\u003e\n        \u003cp\u003emale\u003c/p\u003e\n        \u003cp\u003e20\u003c/p\u003e\n    \u003c/div\u003e\n\u003c/div\u003e\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcandyframework%2Fhtml-filter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcandyframework%2Fhtml-filter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcandyframework%2Fhtml-filter/lists"}