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

Values are not properly converted to DOMString #129

Open
cdoublev opened this issue Apr 24, 2021 · 1 comment
Open

Values are not properly converted to DOMString #129

cdoublev opened this issue Apr 24, 2021 · 1 comment

Comments

@cdoublev
Copy link

cdoublev commented Apr 24, 2021

I'm not sure that the following behavior tested in Chrome and Firefox is described in any specification:

// Case 1: toString() from prototype
class Prop {
  toString() {
    return 1 // Number
  }
}
style.setProperty('opacity', new Prop) // Ok, new value: 1

// Case 2: toString() from instance
const Prop = {
  toString() {
    return 1
  }
}
style.setProperty('opacity', Prop) // Ok, new value: 1

// Case 3: recursive toString()
class RecursiveProp {
  toString() {
    return new Prop()
  }
}
style.setProperty('opacity', new RecursiveProp) // Error

Ie. the value should be converted to String if it has a toString() method. cssstyle currently returns '' for all cases, ie. it results to an invalid value. Chrome and Firefox throw an error for case 3.

@cdoublev
Copy link
Author

cdoublev commented Apr 29, 2021

From what I understand, a value is converted to a DOMString when passing from JavaScript to a DOM element style interface.

Interface CSSStyleDeclaration

void setProperty(in DOMString propertyName, in DOMString value, in DOMString priority) raises(DOMException);

https://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleDeclaration

ECMAScript values are converted to an IDL value when passed to a platform object expecting that type

https://heycam.github.io/webidl/#es-type-mapping

An ECMAScript value V is converted to an IDL DOMString value by running the following algorithm:

  1. If V is null and the conversion is to an IDL type associated with the LegacyNullToEmptyString extended attribute, then return the DOMString value that represents the empty string.
  2. Let x be ToString(V)
  3. Return the IDL DOMString value [...]

https://heycam.github.io/webidl/#es-DOMString

I renamed the title of this issue to handle the following cases that were also not handled correctly:

target.style.opacity = Symbol('1')                             // Error
target.style.opacity = { toString: () => [0] }                 // Error
target.style.opacity = BigInt(1)                               // '1'
target.style.setProperty('--custom', undefined)                // 'undefined'
target.style.setProperty('--custom', true)                     // 'true'
target.style.setProperty('--custom', { toString: () => null }) // 'null'

@cdoublev cdoublev changed the title Value as is not converted to String Values are not properly converted to DOMString Apr 29, 2021
@cdoublev cdoublev mentioned this issue May 10, 2021
cdoublev added a commit to cdoublev/cssstyle that referenced this issue May 23, 2021
cdoublev added a commit to cdoublev/cssstyle that referenced this issue May 24, 2021
cdoublev added a commit to cdoublev/cssstyle that referenced this issue Jun 9, 2021
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 a pull request may close this issue.

1 participant