Skip to content

Commit

Permalink
Merge pull request #393 from preactjs/fix/spellcheck-prop-serialization
Browse files Browse the repository at this point in the history
fix: `spellcheck` prop
  • Loading branch information
rschristian authored Sep 14, 2024
2 parents d20ad14 + 6255591 commit ddf1bbb
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/angry-chairs-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'preact-render-to-string': patch
---

Fix `spellcheck={false}` not rendering as `spellcheck="false"`
13 changes: 7 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 6 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
UNSAFE_NAME,
NAMESPACE_REPLACE_REGEX,
HTML_LOWER_CASE,
HTML_ENUMERATED,
SVG_CAMEL_CASE,
createComponent
} from './lib/util.js';
Expand Down Expand Up @@ -623,10 +624,11 @@ function _renderToString(
name = name.replace(NAMESPACE_REPLACE_REGEX, '$1:$2').toLowerCase();
} else if (UNSAFE_NAME.test(name)) {
continue;
} else if ((name[4] === '-' || name === 'draggable') && v != null) {
// serialize boolean aria-xyz or draggable attribute values as strings
// `draggable` is an enumerated attribute and not Boolean. A value of `true` or `false` is mandatory
// https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/draggable
} else if (
(name[4] === '-' || HTML_ENUMERATED.has(name)) &&
v != null
) {
// serialize boolean aria-xyz or enumerated attribute values as strings
v = v + EMPTY_STR;
} else if (isSvgMode) {
if (SVG_CAMEL_CASE.test(name)) {
Expand All @@ -637,9 +639,6 @@ function _renderToString(
}
} else if (HTML_LOWER_CASE.test(name)) {
name = name.toLowerCase();
if (name === 'spellcheck') {
v = '' + v;
}
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/lib/util.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
export const VOID_ELEMENTS = /^(?:area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)$/;
export const UNSAFE_NAME = /[\s\n\\/='"\0<>]/;
export const NAMESPACE_REPLACE_REGEX = /^(xlink|xmlns|xml)([A-Z])/;
export const HTML_LOWER_CASE = /^accessK|^auto[A-Z]|^cell|^ch|^col|cont|cross|dateT|encT|form[A-Z]|frame|hrefL|inputM|maxL|minL|noV|playsI|popoverT|readO|rowS|spellC|src[A-Z]|tabI|useM|item[A-Z]/;
export const SVG_CAMEL_CASE = /^ac|^ali|arabic|basel|cap|clipPath$|clipRule$|color|dominant|enable|fill|flood|font|glyph[^R]|horiz|image|letter|lighting|marker[^WUH]|overline|panose|pointe|paint|rendering|shape|stop|strikethrough|stroke|spel|text[^L]|transform|underline|unicode|units|^v[^i]|^w|^xH/;
export const HTML_LOWER_CASE = /^accessK|^auto[A-Z]|^cell|^ch|^col|cont|cross|dateT|encT|form[A-Z]|frame|hrefL|inputM|maxL|minL|noV|playsI|popoverT|readO|rowS|src[A-Z]|tabI|useM|item[A-Z]/;
export const SVG_CAMEL_CASE = /^ac|^ali|arabic|basel|cap|clipPath$|clipRule$|color|dominant|enable|fill|flood|font|glyph[^R]|horiz|image|letter|lighting|marker[^WUH]|overline|panose|pointe|paint|rendering|shape|stop|strikethrough|stroke|text[^L]|transform|underline|unicode|units|^v[^i]|^w|^xH/;

// Boolean DOM properties that translate to enumerated ('true'/'false') attributes
export const HTML_ENUMERATED = new Set(['draggable', 'spellcheck']);

// DOM properties that should NOT have "px" added when numeric
const ENCODED_ENTITIES = /["&<]/;
Expand Down
6 changes: 3 additions & 3 deletions test/render.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,11 @@ describe('render', () => {
expect(rendered).to.equal(`<div data-checked="false"></div>`);
});

it('should support spellCheck', () => {
let rendered = render(<div spellCheck={false} />);
it('should support spellcheck', () => {
let rendered = render(<div spellcheck={false} />);
expect(rendered).to.equal(`<div spellcheck="false"></div>`);

rendered = render(<div spellCheck />);
rendered = render(<div spellcheck />);
expect(rendered).to.equal(`<div spellcheck="true"></div>`);
});

Expand Down
1 change: 0 additions & 1 deletion test/utils.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,6 @@ export const htmlAttributes = {
slot: 'slot',
span: 'span',
spellcheck: 'spellcheck',
spellCheck: 'spellcheck',
src: 'src',
srcset: 'srcset',
srcDoc: 'srcdoc',
Expand Down

0 comments on commit ddf1bbb

Please sign in to comment.