Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for prop: & attr: directives #4419

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module.exports = function (api) {
],
plugins: [
'@babel/plugin-proposal-object-rest-spread',
'@babel/plugin-transform-react-jsx',
['@babel/plugin-transform-react-jsx', { throwIfNamespace: false }],
'babel-plugin-transform-async-to-promises',
['babel-plugin-transform-rename-properties', { rename }]
],
Expand Down
36 changes: 21 additions & 15 deletions src/diff/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ let eventClock = 0;
* @param {string} namespace Whether or not this DOM node is an SVG node or not
*/
export function setProperty(dom, name, value, oldValue, namespace) {
let useCapture;
let useCapture, prefix;
if ((name[0] == 'a' || name[0] == 'p') && name[4] == ':') {
prefix = name.slice(0, 4);
name = name.slice(5);
}

o: if (name === 'style') {
if (typeof value == 'string') {
Expand Down Expand Up @@ -104,20 +108,22 @@ export function setProperty(dom, name, value, oldValue, namespace) {
// - className --> class
name = name.replace(/xlink(H|:h)/, 'h').replace(/sName$/, 's');
} else if (
name != 'width' &&
name != 'height' &&
name != 'href' &&
name != 'list' &&
name != 'form' &&
// Default value in browsers is `-1` and an empty string is
// cast to `0` instead
name != 'tabIndex' &&
name != 'download' &&
name != 'rowSpan' &&
name != 'colSpan' &&
name != 'role' &&
name != 'popover' &&
name in dom
(prefix != 'attr' &&
name != 'width' &&
name != 'height' &&
name != 'href' &&
name != 'list' &&
name != 'form' &&
// Default value in browsers is `-1` and an empty string is
// cast to `0` instead
name != 'tabIndex' &&
name != 'download' &&
name != 'rowSpan' &&
name != 'colSpan' &&
name != 'role' &&
name != 'popover' &&
name in dom) ||
prefix == 'prop'
) {
try {
dom[name] = value == null ? '' : value;
Expand Down
11 changes: 11 additions & 0 deletions test/browser/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,17 @@ describe('render()', () => {
expect(links[3].hasAttribute('href')).to.equal(true);
});

it('should support using `prop:` directive to set properties', () => {
render(<input prop:foo="bar" />, scratch);
expect(scratch.firstChild.foo).to.equal('bar');
expect(scratch.firstChild.getAttribute('foo')).to.equal(null);
});

it('should support using `attr:` directive to set attributes', () => {
render(<input attr:value="foo" />, scratch);
expect(scratch.firstChild.getAttribute('value')).to.equal('foo');
});

describe('dangerouslySetInnerHTML', () => {
it('should support dangerouslySetInnerHTML', () => {
let html = '<b>foo &amp; bar</b>';
Expand Down
Loading