Skip to content

html style attribute not updated #9307

Closed

Description

@paulrouget
<div style="width:10px">
</div>
<script>
  var div = document.querySelector('div');
  div.style.backgroundColor = "red";
  console.log(div.getAttribute("style"));
</script>

This prints width:10px instead of width: 10px; background-color: red;.

Activity

  1. paulrouget commented on Jan 14, 2016

    @paulrouget
    ContributorAuthor

    Supporting this would help a lot debugging browser.html.

  2. added
    A-content/dom
    on Jan 14, 2016
  3. Ms2ger commented on Jan 14, 2016

    @Ms2ger
    Contributor

    https://drafts.csswg.org/cssom/#the-elementcssinlinestyle-interface

    Mutating the declarations must set the style content attribute on the context object to the serialization of the declarations.

  4. added
    I-wrong
    on Jan 14, 2016
  5. craftytrickster commented on Jan 17, 2016

    @craftytrickster
    Contributor

    Some observations:

    The setting of the styles appears to occur in the servo/components/script/dom/element.rs file.

    The initial setting of the width <div style="width:10px"> appears to occur in the set_attribute_from_parser method, and the second setting div.style.backgroundColor = "red"; appears to occur in the update_inline_style method.

  6. craftytrickster commented on Jan 17, 2016

    @craftytrickster
    Contributor

    On the element struct, why is there style_attribute field in addition to the attrs field? Is a style_attribute not just a type of attr?

    I am not sure if this is "robust", but could this be solved by adding a check in the get_attribute_by_name method, by making a special check for name === style, and then appending the inline style attributes to the result as well?

  7. Ms2ger commented on Jan 17, 2016

    @Ms2ger
    Contributor

    I think we'll probably want to update the string any time the style is changed.

  8. craftytrickster commented on Jan 19, 2016

    @craftytrickster
    Contributor

    I have noticed further incorrect results with the implementation of console.log(div.getAttribute("style"));.

    If you change the original code to include the background declaration twice:

     <div style="width:10px; width: 10px;">

    In Firefox it will print out the width with only one instance of width. However, in servo, it will actually print out width:100px; width: 100px; for the console.log style.

    I suspect that this is due to the fact that push_new_attribute is blindly called from set_attribute_from_parser. It seems to me that there should be a check to see if the attribute exists, and if it does, it should simply be updated. If there is any recommended way of doing so, it would be good to know.

    As for the original problem:
    If I comment out the following code - self.attrs.borrow_mut().push(JS::from_rooted(&attr)); the style changes on the screen seem to be fine, but the style string returned by div.getAttribute("style") is null instead of width: 10px;.

    In update_inline_style (which is called by div.style.backgroundColor = "red";), there is no code that appends the self.attrs. It seems that this is why the string is not being updated in OP's example. But, given the problem of blindingly appending the string above, I feel reluctant to append self.attrs, every time this is called. If the same inline style is updated multiple times, doing so would needless bloat self.atrrs. That is why I suggested doing my special check of name === style on get_attribute_by_name, in order to avoid the inline update.

    @Ms2ger Please let me know what you think.

  9. nox commented on Jan 19, 2016

    @nox
    Contributor

    I'm 99% sure the spec requires the actual attribute value to be preserved when getting it, with duplicate entries and whatnot.

  10. paulrouget commented on Jan 19, 2016

    @paulrouget
    ContributorAuthor

    In Firefox it will print out the width with only one instance of width. However, in servo, it will actually print out width:100px; width: 100px; for the console.log style

    Servo just doesn't update the attribute. Ever. So it just doesn't change the original string.
    Firefox applies the property, and pretty print it as width: 100px.

  11. paulrouget commented on Jan 19, 2016

    @paulrouget
    ContributorAuthor

    This is needed to fix #9377. @craftytrickster, are you planning to work on a PR?

  12. nox commented on Jan 19, 2016

    @nox
    Contributor

    In Firefox it will print out the width with only one instance of width. However, in servo, it will actually print out width:100px; width: 100px; for the console.log style.

    I think you are wrong.

    var div = document.createElement('div');
    div.setAttribute("style", "width: 100px; width: 100px; ");
    console.log(div.getAttribute("style"));
  13. nox commented on Jan 19, 2016

    @nox
    Contributor

    Oh, I just now understood thanks to @paulrouget that you were still referring to the original snippet where we change properties on CSSStyleDeclaration. Never mind me.

    The method set_attribute_from_parser is still irrelevant to the problem we have here, though.

  14. craftytrickster commented on Jan 19, 2016

    @craftytrickster
    Contributor

    I would like to work on a PR, maybe tonight if I have some time. I am not sure what the correct thing to do here is, my initial guess would be to edit the update_inline_style method, perhaps converting the PropertyDeclaration field to an Attr, and then do something like self.attrs.push(convertedPropertyDeclaration). As mentioned in my example above where I commented out code, the self.attrs seems to be what drives the result string from console.log(div.getAttribute("style")).
    The complication here is that I do not see any easy way to do the PropertyDeclaration -> Attr conversion offhand, but I am definitely open to suggestions.

  15. nox commented on Jan 19, 2016

    @nox
    Contributor

    @craftytrickster Of course self.attrs is what drives the result of getAttribute. The attribute should be appropriately updated when the corresponding CSSStyleDeclaration value is changed, as the spec mandates.

  16. 7 remaining items

  17. paulrouget commented on Jan 21, 2016

    @paulrouget
    ContributorAuthor

    is that correct

    Yes.

  18. craftytrickster commented on Jan 22, 2016

    @craftytrickster
    Contributor

    I've taken an initial pass (WIP) at this in my branch here, and superficially it seems to work, as it is able to append the style names as expected. However, the else branch of my if statement always causes a panic (reachable if you delete the style attribute in the html directly,
    div style="width:10px").

    I have included the text of the panic as a comment on the branch. I have so far been unable to determine the cause, if I eventually do, I can clean up the code and also take into account remove_inline_style_property. In the meantime, if someone sees a clear reason for the panic, it would be helpful to know.

  19. nox commented on Jan 23, 2016

    @nox
    Contributor

    @craftytrickster Your code panics because you try to mutably-borrow self.attrs while you have already borrowed it.

    On another note, as stated earlier, you should not append anything to the style attribute, you should instead set it to the serialised CSSStyleDeclaration instance.

  20. craftytrickster commented on Jan 23, 2016

    @craftytrickster
    Contributor

    I was able to achieve the results of this issue using regex to modify the existing attrs style instance, but it is was just a temporary approach to see if it would work. @nox, as suggested, I will try to replace the unstructured regex approach with using the CSSStyleDeclaration methods.

  21. craftytrickster commented on Jan 23, 2016

    @craftytrickster
    Contributor

    @nox, after taking a look, I do not see anything on the cssstyledeclaration.rs file that would help with this issue, which is to synchronize element.style_attribute with the style-attr fromelement.attrs. Nevertheless, I will post a PR this weekend, and we can go from there.

  22. added a commit that references this issue on Mar 7, 2016
  23. added a commit that references this issue on May 25, 2016
    52f17a8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-content/domI-wrong

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions