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

Use ownerDocument instead of global document #2721

Merged
merged 1 commit into from
Sep 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ _The format is based on [Keep a Changelog](http://keepachangelog.com/) and this

## Unreleased

- Fix to use `ownerDocument` instead of global `document`, by [@yamachig](https://github.com/yamachig) (see [#2721](https://github.com/styled-components/styled-components/pull/2721))

- Backport fix for SSR classname mismatches in development mode for some environments like next.js (see [#2701](https://github.com/styled-components/styled-components/pull/2701))

- Fix attrs not properly taking precedence over props
Expand Down
14 changes: 9 additions & 5 deletions packages/styled-components/src/models/StyleTags.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ const addUpUntilIndex = (sizes: number[], index: number): number => {

/* create a new style tag after lastEl */
const makeStyleTag = (target: ?HTMLElement, tagEl: ?Node, insertBefore: ?boolean) => {
const el = document.createElement('style');
let targetDocument = document;
if(target) targetDocument = target.ownerDocument;
else if(tagEl) targetDocument = tagEl.ownerDocument;
Copy link
Member

Choose a reason for hiding this comment

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

I'd write this as:

const targetDocument = target ? target.ownerDocument : tagEl ? tagEl.ownerDocument : document

but that's just my anti-mutability bias 🤷‍♀

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Jessidhia thank you for reviewing! I agree with anti-mutability, though, the eslint rule (no-nested-ternary) didn't let me do so :)


const el = targetDocument.createElement('style');
Copy link
Member

Choose a reason for hiding this comment

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

Interesting that this causes no security issues; apparently any <style> tags' associated StyleSheet are considered same-origin. https://html.spec.whatwg.org/multipage/semantics.html#the-style-element:concept-css-style-sheet-origin-clean-flag

This can still break for CSP reasons, though, but it'd break regardless of whether we use CSSOM or child text nodes.

el.setAttribute(SC_ATTR, '');
el.setAttribute(SC_VERSION_ATTR, __VERSION__);

Expand All @@ -68,7 +72,7 @@ const makeStyleTag = (target: ?HTMLElement, tagEl: ?Node, insertBefore: ?boolean
}

/* Work around insertRule quirk in EdgeHTML */
el.appendChild(document.createTextNode(''));
el.appendChild(targetDocument.createTextNode(''));

if (target && !tagEl) {
/* Append to target when no previous element was passed */
Expand Down Expand Up @@ -226,7 +230,7 @@ const makeSpeedyTag = (el: HTMLStyleElement, getImportRuleTag: ?() => Tag<any>):
};
};

const makeTextNode = id => document.createTextNode(makeTextMarker(id));
const makeTextNode = (targetDocument, id) => targetDocument.createTextNode(makeTextMarker(id));

const makeBrowserTag = (el: HTMLStyleElement, getImportRuleTag: ?() => Tag<any>): Tag<Text> => {
const names = (Object.create(null): Object);
Expand All @@ -243,7 +247,7 @@ const makeBrowserTag = (el: HTMLStyleElement, getImportRuleTag: ?() => Tag<any>)
return prev;
}

markers[id] = makeTextNode(id);
markers[id] = makeTextNode(el.ownerDocument, id);
el.appendChild(markers[id]);
names[id] = Object.create(null);

Expand Down Expand Up @@ -281,7 +285,7 @@ const makeBrowserTag = (el: HTMLStyleElement, getImportRuleTag: ?() => Tag<any>)
if (marker === undefined) return;

/* create new empty text node and replace the current one */
const newMarker = makeTextNode(id);
const newMarker = makeTextNode(el.ownerDocument, id);
el.replaceChild(newMarker, marker);
markers[id] = newMarker;
resetIdNames(names, id);
Expand Down
4 changes: 2 additions & 2 deletions packages/styled-components/src/utils/insertRuleHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ export const sheetForTag = (tag: HTMLStyleElement): CSSStyleSheet => {
if (tag.sheet) return tag.sheet;

/* Firefox quirk requires us to step through all stylesheets to find one owned by the given tag */
const size = document.styleSheets.length;
const size = tag.ownerDocument.styleSheets.length;
for (let i = 0; i < size; i += 1) {
const sheet = document.styleSheets[i];
const sheet = tag.ownerDocument.styleSheets[i];
// $FlowFixMe
if (sheet.ownerNode === tag) return sheet;
}
Expand Down