Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Boolean attributes#629

Merged
jorgebucaran merged 1 commit into
jorgebucaran:masterfrom
frenzzy:falsey-attributes
Jul 18, 2018
Merged

Boolean attributes#629
jorgebucaran merged 1 commit into
jorgebucaran:masterfrom
frenzzy:falsey-attributes

Conversation

@frenzzy

@frenzzy frenzzy commented Mar 1, 2018

Copy link
Copy Markdown
Contributor

Fixes #628

With this change we may rich consistent behavior between client and server rendering for reserved DOM attributes such as spellcheck or draggable, see:

before

<input spellcheck="false" /> // => <input spellcheck="true" /> <== Oh NO!
<input spellcheck={false} /> // => <input spellcheck="false" />

after

<input spellcheck="false" /> // => <input spellcheck="false" /> <== Oh YES!
<input spellcheck={false} /> // => <input spellcheck="false" />

SSR

<input spellcheck="false" /> // => <input spellcheck="false" />
<input spellcheck={false} /> // => <input />

@codecov

codecov Bot commented Mar 1, 2018

Copy link
Copy Markdown

Codecov Report

Merging #629 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master   #629   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files           1      1           
  Lines         166    166           
  Branches       52     52           
=====================================
  Hits          166    166
Impacted Files Coverage Δ
src/index.js 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ebdfc2c...23778ee. Read the comment docs.

@jorgebucaran

jorgebucaran commented Mar 1, 2018

Copy link
Copy Markdown
Owner

How is this different to #592?

@frenzzy

frenzzy commented Mar 1, 2018

Copy link
Copy Markdown
Contributor Author

#592 behavior:

// before
<input spellcheck="false" data-x="false" /> // => <input spellcheck="true" data-x="false" />
<input spellcheck={false} data-x={false} /> // => <input spellcheck="false" />

// after
<input spellcheck="false" data-x="false" /> // => <input spellcheck="true" data-x="false" />
<input spellcheck={false} data-x={false} /> // => <input spellcheck="false" data-x="false" />

// SSR
<input spellcheck="false" data-x="false" /> // => <input spellcheck="false" data-x="false" />
<input spellcheck={false} data-x={false} /> // => <input />

so, this PR is about reserved DOM attributes while #592 is about custom DOM attributes.

Comment thread src/index.js Outdated
(name in element && name !== "list" && !isSVG)
) {
element[name] = value == null ? "" : value
element[name] = value == null || value === "false" ? "" : value

@frenzzy frenzzy Mar 2, 2018

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or we can use regex here: /^false$/i.test(value)
it will be more robust (users can use False, FALSE etc) but slower

<input spellcheck="false" /> // => <input spellcheck="false" />
<input spellcheck="False" /> // => <input spellcheck="true" />
<input spellcheck="FALSE" /> // => <input spellcheck="true" />
// vs regexp
<input spellcheck="false" /> // => <input spellcheck="false" />
<input spellcheck="False" /> // => <input spellcheck="false" />
<input spellcheck="FALSE" /> // => <input spellcheck="false" />

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HTML spec allow different cases ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, HTML case insensitive

@jorgebucaran

Copy link
Copy Markdown
Owner

Unrelated but related. Adding it here before I forget. preactjs/preact#983

@jorgebucaran

Copy link
Copy Markdown
Owner

@frenzzy But why not use {false}?

@frenzzy

frenzzy commented Mar 8, 2018

Copy link
Copy Markdown
Contributor Author

@frenzzy But why not use {false}?

Because false does't render attribute at all:

<input data-example={false} /> // renders <input />

Also "false" already works correctly for most of attributes:

<input data-example="false" /> // renders <input data-example="false" />

The problem only in bunch of DOM attributes such as spellcheck, dragging etc.

After this change "false" not longer converted to "true" for SOME attributes.
Which makes "false" value consistent between different attributes and server-side rendering.

@frenzzy frenzzy force-pushed the falsey-attributes branch from ee94ef1 to 975f203 Compare March 8, 2018 05:52
@jorgebucaran

jorgebucaran commented Mar 8, 2018

Copy link
Copy Markdown
Owner

@frenzzy Why does "false" work for some attributes and not for others? I am trying to leave this information documented here for our record.

@frenzzy

frenzzy commented Mar 8, 2018

Copy link
Copy Markdown
Contributor Author

@frenzzy Why does "false" work for some attributes and not for others?

Because of the way how we modify attributes (props vs attributes):
https://github.com/hyperapp/hyperapp/blob/fa8fdd373539c386c2bca45dfd185ba5047f6f1a/src/index.js#L163

const el = document.createElement('input')

el.spellcheck = false                   // => <input spellcheck="false" />
el.spellcheck = 'false'                 // => <input spellcheck="true" /> !!! Noooo...
el.setAttribute('spellcheck', false)    // => <input spellcheck="false" />
el.setAttribute('spellcheck', 'false')  // => <input spellcheck="false" />
el.setAttribute('data-custom', false)   // => <input data-custom="false" />
el.setAttribute('data-custom', 'false') // => <input data-custom="false" />

see also #634

@frenzzy frenzzy force-pushed the falsey-attributes branch 2 times, most recently from a02b0d9 to 0b88944 Compare March 9, 2018 09:32
@frenzzy

frenzzy commented Mar 13, 2018

Copy link
Copy Markdown
Contributor Author

Looks like hyperapp has problems with all kind of boolean attribute values "true"/"false", "yes"/"no" and "on"/"off", for example:

// jsx                     => html
<div translate="yes" /> // => <div translate="yes"></div>
<div translate="no" />  // => <div translate="yes"></div> <= No!

Probably related: react/react#10589

@jorgebucaran

jorgebucaran commented Mar 13, 2018

Copy link
Copy Markdown
Owner

I think we should make only {true} or {false} work and doing that, fix that horrible "yes", "no", "on", "off", etc., API.

@frenzzy frenzzy force-pushed the falsey-attributes branch 2 times, most recently from b8fef98 to b8d26a2 Compare March 16, 2018 09:37
@frenzzy frenzzy force-pushed the falsey-attributes branch 2 times, most recently from de86cf8 to ef44361 Compare March 29, 2018 08:46
@frenzzy

frenzzy commented Mar 29, 2018

Copy link
Copy Markdown
Contributor Author

@frenzzy frenzzy force-pushed the falsey-attributes branch from ef44361 to c0f3013 Compare March 29, 2018 18:12
@frenzzy frenzzy changed the title Falsey attributes Boolean attributes Mar 29, 2018
@frenzzy

frenzzy commented Mar 29, 2018

Copy link
Copy Markdown
Contributor Author

I think we should make only {true} or {false} work and doing that, fix that horrible "yes", "no", "on", "off", etc., API.

  1. It is not obvious for user why "false" does not work properly for some attributes. He writes "false" hyperapp renders "true" - WTF!?
  2. It is not possible to render some attributes in isomorphic manner. If use {false}, server will not render it, if use "false", hyperapp will change it to "true".
  3. User should always keep in mind list of attributes which works incorrectly, see https://codepen.io/frenzzy/pen/Zxrrzz/left/?editors=0010

@frenzzy frenzzy force-pushed the falsey-attributes branch 3 times, most recently from bda9768 to 11c99a0 Compare March 31, 2018 06:58
@jorgebucaran

Copy link
Copy Markdown
Owner

Let's wait after 2.0. Then let's figure out what to do.

Comment thread src/index.js Outdated
} else if (
name in element &&
name !== "list" &&
!(typeof value === "string" && typeof element[name] === "boolean") &&

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@frenzzy What about only typeof value === "string"?

So, removing typeof element[name] === "boolean".

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind :D

@frenzzy frenzzy force-pushed the falsey-attributes branch from 11c99a0 to 23778ee Compare July 18, 2018 10:11
@jorgebucaran jorgebucaran merged commit aa2ce46 into jorgebucaran:master Jul 18, 2018
@frenzzy frenzzy deleted the falsey-attributes branch July 18, 2018 10:17
@jorgebucaran

Copy link
Copy Markdown
Owner

Thanks, @frenzzy! 1.2.8 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants