Skip to content

Commit

Permalink
Correctly escape quotes in attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit0 committed Oct 29, 2023
1 parent ee58d34 commit 57a4b43
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .config/mocha.fast.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"exclude": ["src/test/packages/**", "src/test/slow/**"],
"extension": ["ts", "tsx"],
"require": ["ts-node/register"],
"spec": ["src/test/**/*.test.ts"],
"spec": ["src/test/**/*.test.ts", "src/test/**/*.test.tsx"],
"timeout": 5000,
"watch-files": ["src/**/*.ts"]
"watch-files": ["src/**/*.ts", "src/**/*.tsx"]
}
2 changes: 1 addition & 1 deletion .config/mocha.full.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://json.schemastore.org/mocharc.json",
"timeout": 0,
"spec": "src/test/**/*.test.ts",
"spec": ["src/test/**/*.test.ts", "src/test/**/*.test.tsx"],
"exclude": ["src/test/packages/**"],
"require": ["ts-node/register"]
}
4 changes: 2 additions & 2 deletions .config/mocha.test-explorer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"package": "./package.json",
"require": "ts-node/register",
"slow": 500,
"spec": ["src/**/*.test.ts"],
"spec": ["src/**/*.test.ts", "src/**/*.test.tsx"],
"timeout": 0,
"watch-files": ["src/**/*.ts"]
"watch-files": ["src/**/*.ts", "src/**/*.tsx"]
}
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- TypeDoc now attempts to correct local anchor links in readme files which are broken by its deconfliction logic, #2413.
- TypeDoc now finds comments on index signatures again, #2414.
- TypeDoc now does a better job of detecting properties when destructured function arguments are used.
- Quotes will now be properly escaped in HTML attribute values.

### Thanks!

Expand Down
9 changes: 6 additions & 3 deletions src/lib/utils/jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export const renderElement = function renderElement(
let html = "";

if (tag !== Fragment) {
if (blockElements.has(tag) && renderPretty) {
if (blockElements.has(tag) && renderPretty && html) {
html += "\n";
}
html += "<";
Expand All @@ -141,8 +141,11 @@ export const renderElement = function renderElement(
} else {
html += " ";
html += key;
html += "=";
html += JSON.stringify(val);
html += '="';
html += (
typeof val === "string" ? val : JSON.stringify(val)
).replaceAll('"', "&quot;");
html += '"';
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/test/utils/jsx.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,9 @@ describe("JSX", () => {
</svg>`.replace(/^\s*|\r?\n/gm, ""),
);
});

it("Properly escapes quotes in html attributes", () => {
const quot = `test"quote`;
equal(renderElement(<div data-foo={quot} />), `<div data-foo="test&quot;quote"></div>`);
});
});

0 comments on commit 57a4b43

Please sign in to comment.