diff --git a/changelog.md b/changelog.md index 6f716c1..703ae9e 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,16 @@ +## 2.0.6 (2021-06-11) + +* style: ignore ts error ([aaab81d](https://github.com/posthtml/posthtml-render/commit/aaab81d)) +* fix: incorrect detect json format ([c334ef2](https://github.com/posthtml/posthtml-render/commit/c334ef2)) +* fix: is-json not declare ([85b7bec](https://github.com/posthtml/posthtml-render/commit/85b7bec)) +* build: update depdev ([76c0752](https://github.com/posthtml/posthtml-render/commit/76c0752)) +* test: change to number with quote ([e2f6cb5](https://github.com/posthtml/posthtml-render/commit/e2f6cb5)) + + + ## 2.0.5 (2021-06-10) +* 2.0.5 ([efc4a41](https://github.com/posthtml/posthtml-render/commit/efc4a41)) * test: for long test ([d019534](https://github.com/posthtml/posthtml-render/commit/d019534)) * test: invalid in htmlnano travis ([69d666b](https://github.com/posthtml/posthtml-render/commit/69d666b)) * test: more tests ([4dfa92e](https://github.com/posthtml/posthtml-render/commit/4dfa92e)) diff --git a/package-lock.json b/package-lock.json index 5344c33..3bf1f94 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "posthtml-render", - "version": "2.0.5", + "version": "2.0.6", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -6966,6 +6966,11 @@ "js-types": "^1.0.0" } }, + "is-json": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/is-json/-/is-json-2.0.1.tgz", + "integrity": "sha1-a+Fm0USCihMdaGiRuYPfYsOUkf8=" + }, "is-negated-glob": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/is-negated-glob/-/is-negated-glob-1.0.0.tgz", diff --git a/package.json b/package.json index 1eb1689..90d8bab 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "posthtml-render", - "version": "2.0.5", + "version": "2.0.6", "description": "Renders PostHTML Tree to HTML/XML", "license": "MIT", "repository": "posthtml/posthtml-render", @@ -26,6 +26,9 @@ "html", "xml" ], + "dependencies": { + "is-json": "^2.0.1" + }, "devDependencies": { "@antfu/eslint-config-ts": "^0.6.4", "@commitlint/cli": "^12.1.1", diff --git a/src/index.ts b/src/index.ts index b975be6..e507ebb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,5 @@ +// @ts-expect-error +import isJSON from 'is-json'; import {Attributes, NodeText, NodeTag} from 'posthtml-parser'; import {closingSingleTagOptionEnum, closingSingleTagTypeEnum, Options, quoteStyleEnum} from '../types/index.d'; @@ -182,13 +184,8 @@ function render(tree?: Node | Node[], options: Options = {}): string { for (const [key, value] of Object.entries(object)) { if (typeof value === 'string') { - let json; - try { - json = JSON.parse(value); - } catch {} - - if (json) { - attr += ` ${key}='${value}'`; + if (isJSON(value)) { + attr += makeAttr(key, value); } else if (quoteAllAttributes || ATTRIBUTE_QUOTES_REQUIRED.test(value)) { let attrValue = value; diff --git a/test/test-core.spec.ts b/test/test-core.spec.ts index 622a110..79aef9f 100644 --- a/test/test-core.spec.ts +++ b/test/test-core.spec.ts @@ -547,8 +547,8 @@ test('{QuoteStyle} for width/height attrs in img', t => { tag: 'img', attrs: { src: 'https://example.com/example.png', - width: 20, - height: 20 + width: '20', + height: '20' } }; const expected = '';