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

A document created through parseFromString inherits creator document's CSS #15389

Open
freeatnet opened this issue Feb 4, 2017 · 2 comments
Open

Comments

@freeatnet
Copy link
Contributor

@freeatnet freeatnet commented Feb 4, 2017

Steps to reproduce:

  1. Open a document that contains one or more CSS rules and creates a non-empty document using DOMParser.parseFromString
  2. Check the result of window.getComputedStyle on the created document

Expected: created document does not inherit CSS rules of the main document
Observed: created document inherits CSS rules of the main document

The issue can be reproduced using the following test:

<!doctype html>
<meta charset="utf-8">
<title></title>
<body>
  <style>
    div {
      background-color: yellow;
    }
  </style>

  <div>I'm inside the form!</div>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
var newDocHtml = '<html><body><div></div></body></html>';

/* This test succeeds */
test(function () {
  var d = (new DOMParser).parseFromString(newDocHtml, 'text/html');

  assert_equals(document.styleSheets.length, 1);
  assert_equals(d.styleSheets.length, 0);
}, "Count styleSheets correctly");

/* This test fails */
test(function () {
  var d = (new DOMParser).parseFromString(newDocHtml, 'text/html');
  var dd = d.getElementsByTagName('div')[0];

  assert_equals(window.getComputedStyle(dd).getPropertyValue("background-color"), '');
}, "Apply styleSheets correctly");
</script>
</body>
@jdm jdm added the A-content/dom label Feb 4, 2017
@jdm
Copy link
Member

@jdm jdm commented Feb 4, 2017

Interestingly enough, there's a note about this specific case in https://drafts.csswg.org/cssom/#dom-window-getcomputedstyle:

Note: This means that even if obj is in a different document (e.g. one fetched via 
XMLHttpRequest) it will still use the style rules associated with the document that is associated 
with the global object on which getComputedStyle() was invoked to compute the CSS declaration 
block.
@jdm
Copy link
Member

@jdm jdm commented Feb 4, 2017

Doubly interestingly, there's code that deals with a similar case which has an interesting TODO comment that suggests that the spec may not be aligned with the real world: https://dxr.mozilla.org/servo/rev/da89099e26955cf5ba44031a0a536e3af4e6a0d5/components/script/dom/cssstyledeclaration.rs#201-205

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.