documentFragment.querySelector problem with Safari

Multi tool use
I am trying to retrieve an element inside a documentFragment
using javascript querySelector
method.
I get what i expect with Chrome and Firefox, but not with Safari (Mac OS, Safari 12.0.2).
function msg(s) {
document.getElementById("a").innerHTML += s + "<br>";
}
var myRoot, e;
myRoot = document.createDocumentFragment();
e = document.createElement("div");
e.id = "child1";
e.innerHTML = "Hello!";
myRoot.appendChild(e);
e = myRoot.querySelector("div:nth-of-type(1)");
if (e) {
msg("1st div tag in fragment: " + e.id);
} else {
msg("Error when retrieving 1st div tag in fragment");
}
document.body.appendChild(myRoot);
e = document.querySelector("div:nth-of-type(1)");
if (e) {
msg("1st div tag in document: " + e.id);
} else {
msg("Error when retrieving 1st div tag in document");
}
<p>Messages:</p>
<p id="a"></p>
<p>Inserted div:</p>
jsFiddle: https://jsfiddle.net/bwqvrex2/
Am I missing something, or is it just a bug?
javascript selectors-api documentfragment
add a comment |
I am trying to retrieve an element inside a documentFragment
using javascript querySelector
method.
I get what i expect with Chrome and Firefox, but not with Safari (Mac OS, Safari 12.0.2).
function msg(s) {
document.getElementById("a").innerHTML += s + "<br>";
}
var myRoot, e;
myRoot = document.createDocumentFragment();
e = document.createElement("div");
e.id = "child1";
e.innerHTML = "Hello!";
myRoot.appendChild(e);
e = myRoot.querySelector("div:nth-of-type(1)");
if (e) {
msg("1st div tag in fragment: " + e.id);
} else {
msg("Error when retrieving 1st div tag in fragment");
}
document.body.appendChild(myRoot);
e = document.querySelector("div:nth-of-type(1)");
if (e) {
msg("1st div tag in document: " + e.id);
} else {
msg("Error when retrieving 1st div tag in document");
}
<p>Messages:</p>
<p id="a"></p>
<p>Inserted div:</p>
jsFiddle: https://jsfiddle.net/bwqvrex2/
Am I missing something, or is it just a bug?
javascript selectors-api documentfragment
So to make it clear, the discrepancy is only when using:nth-xxx
pseudo selector on elements that are placed at the root of the frag. i.e,querySelector("div")
(which does exactly the same as your selector btw) does work, same if you do append an element inside youre
div and target it withdiv>div:nth-of-type(1)
(fiddle). I don't know the specs enough to determine if it is indeed a bug or not.
– Kaiido
Jan 1 at 12:48
And actually,div:nth-of-type(1)
will target the inner div in my fiddle (same asdiv>div:nth-of-type(1)
): (fiddle) And if you wonder same forfirst-child
and other pseudo-selectors, they're like disabled for elements at the root of the frag.
– Kaiido
Jan 1 at 12:57
... Fast reading MDN article aboutnth-of-type()
it sounds like it is a recent addition that "matching elements are not required to have a parent." (still a Draft). Safari probably didn't catch on yet.
– Kaiido
Jan 1 at 13:03
add a comment |
I am trying to retrieve an element inside a documentFragment
using javascript querySelector
method.
I get what i expect with Chrome and Firefox, but not with Safari (Mac OS, Safari 12.0.2).
function msg(s) {
document.getElementById("a").innerHTML += s + "<br>";
}
var myRoot, e;
myRoot = document.createDocumentFragment();
e = document.createElement("div");
e.id = "child1";
e.innerHTML = "Hello!";
myRoot.appendChild(e);
e = myRoot.querySelector("div:nth-of-type(1)");
if (e) {
msg("1st div tag in fragment: " + e.id);
} else {
msg("Error when retrieving 1st div tag in fragment");
}
document.body.appendChild(myRoot);
e = document.querySelector("div:nth-of-type(1)");
if (e) {
msg("1st div tag in document: " + e.id);
} else {
msg("Error when retrieving 1st div tag in document");
}
<p>Messages:</p>
<p id="a"></p>
<p>Inserted div:</p>
jsFiddle: https://jsfiddle.net/bwqvrex2/
Am I missing something, or is it just a bug?
javascript selectors-api documentfragment
I am trying to retrieve an element inside a documentFragment
using javascript querySelector
method.
I get what i expect with Chrome and Firefox, but not with Safari (Mac OS, Safari 12.0.2).
function msg(s) {
document.getElementById("a").innerHTML += s + "<br>";
}
var myRoot, e;
myRoot = document.createDocumentFragment();
e = document.createElement("div");
e.id = "child1";
e.innerHTML = "Hello!";
myRoot.appendChild(e);
e = myRoot.querySelector("div:nth-of-type(1)");
if (e) {
msg("1st div tag in fragment: " + e.id);
} else {
msg("Error when retrieving 1st div tag in fragment");
}
document.body.appendChild(myRoot);
e = document.querySelector("div:nth-of-type(1)");
if (e) {
msg("1st div tag in document: " + e.id);
} else {
msg("Error when retrieving 1st div tag in document");
}
<p>Messages:</p>
<p id="a"></p>
<p>Inserted div:</p>
jsFiddle: https://jsfiddle.net/bwqvrex2/
Am I missing something, or is it just a bug?
function msg(s) {
document.getElementById("a").innerHTML += s + "<br>";
}
var myRoot, e;
myRoot = document.createDocumentFragment();
e = document.createElement("div");
e.id = "child1";
e.innerHTML = "Hello!";
myRoot.appendChild(e);
e = myRoot.querySelector("div:nth-of-type(1)");
if (e) {
msg("1st div tag in fragment: " + e.id);
} else {
msg("Error when retrieving 1st div tag in fragment");
}
document.body.appendChild(myRoot);
e = document.querySelector("div:nth-of-type(1)");
if (e) {
msg("1st div tag in document: " + e.id);
} else {
msg("Error when retrieving 1st div tag in document");
}
<p>Messages:</p>
<p id="a"></p>
<p>Inserted div:</p>
function msg(s) {
document.getElementById("a").innerHTML += s + "<br>";
}
var myRoot, e;
myRoot = document.createDocumentFragment();
e = document.createElement("div");
e.id = "child1";
e.innerHTML = "Hello!";
myRoot.appendChild(e);
e = myRoot.querySelector("div:nth-of-type(1)");
if (e) {
msg("1st div tag in fragment: " + e.id);
} else {
msg("Error when retrieving 1st div tag in fragment");
}
document.body.appendChild(myRoot);
e = document.querySelector("div:nth-of-type(1)");
if (e) {
msg("1st div tag in document: " + e.id);
} else {
msg("Error when retrieving 1st div tag in document");
}
<p>Messages:</p>
<p id="a"></p>
<p>Inserted div:</p>
javascript selectors-api documentfragment
javascript selectors-api documentfragment
edited Jan 1 at 12:34
Andreas
16.6k52842
16.6k52842
asked Jan 1 at 12:26
Simon HiSimon Hi
1,268912
1,268912
So to make it clear, the discrepancy is only when using:nth-xxx
pseudo selector on elements that are placed at the root of the frag. i.e,querySelector("div")
(which does exactly the same as your selector btw) does work, same if you do append an element inside youre
div and target it withdiv>div:nth-of-type(1)
(fiddle). I don't know the specs enough to determine if it is indeed a bug or not.
– Kaiido
Jan 1 at 12:48
And actually,div:nth-of-type(1)
will target the inner div in my fiddle (same asdiv>div:nth-of-type(1)
): (fiddle) And if you wonder same forfirst-child
and other pseudo-selectors, they're like disabled for elements at the root of the frag.
– Kaiido
Jan 1 at 12:57
... Fast reading MDN article aboutnth-of-type()
it sounds like it is a recent addition that "matching elements are not required to have a parent." (still a Draft). Safari probably didn't catch on yet.
– Kaiido
Jan 1 at 13:03
add a comment |
So to make it clear, the discrepancy is only when using:nth-xxx
pseudo selector on elements that are placed at the root of the frag. i.e,querySelector("div")
(which does exactly the same as your selector btw) does work, same if you do append an element inside youre
div and target it withdiv>div:nth-of-type(1)
(fiddle). I don't know the specs enough to determine if it is indeed a bug or not.
– Kaiido
Jan 1 at 12:48
And actually,div:nth-of-type(1)
will target the inner div in my fiddle (same asdiv>div:nth-of-type(1)
): (fiddle) And if you wonder same forfirst-child
and other pseudo-selectors, they're like disabled for elements at the root of the frag.
– Kaiido
Jan 1 at 12:57
... Fast reading MDN article aboutnth-of-type()
it sounds like it is a recent addition that "matching elements are not required to have a parent." (still a Draft). Safari probably didn't catch on yet.
– Kaiido
Jan 1 at 13:03
So to make it clear, the discrepancy is only when using
:nth-xxx
pseudo selector on elements that are placed at the root of the frag. i.e, querySelector("div")
(which does exactly the same as your selector btw) does work, same if you do append an element inside your e
div and target it with div>div:nth-of-type(1)
(fiddle). I don't know the specs enough to determine if it is indeed a bug or not.– Kaiido
Jan 1 at 12:48
So to make it clear, the discrepancy is only when using
:nth-xxx
pseudo selector on elements that are placed at the root of the frag. i.e, querySelector("div")
(which does exactly the same as your selector btw) does work, same if you do append an element inside your e
div and target it with div>div:nth-of-type(1)
(fiddle). I don't know the specs enough to determine if it is indeed a bug or not.– Kaiido
Jan 1 at 12:48
And actually,
div:nth-of-type(1)
will target the inner div in my fiddle (same as div>div:nth-of-type(1)
): (fiddle) And if you wonder same for first-child
and other pseudo-selectors, they're like disabled for elements at the root of the frag.– Kaiido
Jan 1 at 12:57
And actually,
div:nth-of-type(1)
will target the inner div in my fiddle (same as div>div:nth-of-type(1)
): (fiddle) And if you wonder same for first-child
and other pseudo-selectors, they're like disabled for elements at the root of the frag.– Kaiido
Jan 1 at 12:57
... Fast reading MDN article about
nth-of-type()
it sounds like it is a recent addition that "matching elements are not required to have a parent." (still a Draft). Safari probably didn't catch on yet.– Kaiido
Jan 1 at 13:03
... Fast reading MDN article about
nth-of-type()
it sounds like it is a recent addition that "matching elements are not required to have a parent." (still a Draft). Safari probably didn't catch on yet.– Kaiido
Jan 1 at 13:03
add a comment |
1 Answer
1
active
oldest
votes
Prior to Selectors Level 4 CSS specifications, it was required that the matching elements have a parent for selectors like nth-of-type
, first-child
etc.
This new specification, still in a state of Working Draft implements this new behavior, now elements don't need to have a parent.
Safari probably still didn't implemented this part of the new specs, but will certainly when the specs will have stabilised.
Anyway, this behavior should still be considered as experimental, and you might prefer use other ways to do the same thing (e.g use a dummy element as container until appending the fragment to the doc).
Thanks for your reply. Finding a workaround is not a problem now.
– Simon Hi
Jan 1 at 14:17
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53995419%2fdocumentfragment-queryselector-problem-with-safari%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Prior to Selectors Level 4 CSS specifications, it was required that the matching elements have a parent for selectors like nth-of-type
, first-child
etc.
This new specification, still in a state of Working Draft implements this new behavior, now elements don't need to have a parent.
Safari probably still didn't implemented this part of the new specs, but will certainly when the specs will have stabilised.
Anyway, this behavior should still be considered as experimental, and you might prefer use other ways to do the same thing (e.g use a dummy element as container until appending the fragment to the doc).
Thanks for your reply. Finding a workaround is not a problem now.
– Simon Hi
Jan 1 at 14:17
add a comment |
Prior to Selectors Level 4 CSS specifications, it was required that the matching elements have a parent for selectors like nth-of-type
, first-child
etc.
This new specification, still in a state of Working Draft implements this new behavior, now elements don't need to have a parent.
Safari probably still didn't implemented this part of the new specs, but will certainly when the specs will have stabilised.
Anyway, this behavior should still be considered as experimental, and you might prefer use other ways to do the same thing (e.g use a dummy element as container until appending the fragment to the doc).
Thanks for your reply. Finding a workaround is not a problem now.
– Simon Hi
Jan 1 at 14:17
add a comment |
Prior to Selectors Level 4 CSS specifications, it was required that the matching elements have a parent for selectors like nth-of-type
, first-child
etc.
This new specification, still in a state of Working Draft implements this new behavior, now elements don't need to have a parent.
Safari probably still didn't implemented this part of the new specs, but will certainly when the specs will have stabilised.
Anyway, this behavior should still be considered as experimental, and you might prefer use other ways to do the same thing (e.g use a dummy element as container until appending the fragment to the doc).
Prior to Selectors Level 4 CSS specifications, it was required that the matching elements have a parent for selectors like nth-of-type
, first-child
etc.
This new specification, still in a state of Working Draft implements this new behavior, now elements don't need to have a parent.
Safari probably still didn't implemented this part of the new specs, but will certainly when the specs will have stabilised.
Anyway, this behavior should still be considered as experimental, and you might prefer use other ways to do the same thing (e.g use a dummy element as container until appending the fragment to the doc).
answered Jan 1 at 13:11


KaiidoKaiido
43k464104
43k464104
Thanks for your reply. Finding a workaround is not a problem now.
– Simon Hi
Jan 1 at 14:17
add a comment |
Thanks for your reply. Finding a workaround is not a problem now.
– Simon Hi
Jan 1 at 14:17
Thanks for your reply. Finding a workaround is not a problem now.
– Simon Hi
Jan 1 at 14:17
Thanks for your reply. Finding a workaround is not a problem now.
– Simon Hi
Jan 1 at 14:17
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53995419%2fdocumentfragment-queryselector-problem-with-safari%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
d1boDD W76SN86EL08g51VSqAmF8YWRAiH
So to make it clear, the discrepancy is only when using
:nth-xxx
pseudo selector on elements that are placed at the root of the frag. i.e,querySelector("div")
(which does exactly the same as your selector btw) does work, same if you do append an element inside youre
div and target it withdiv>div:nth-of-type(1)
(fiddle). I don't know the specs enough to determine if it is indeed a bug or not.– Kaiido
Jan 1 at 12:48
And actually,
div:nth-of-type(1)
will target the inner div in my fiddle (same asdiv>div:nth-of-type(1)
): (fiddle) And if you wonder same forfirst-child
and other pseudo-selectors, they're like disabled for elements at the root of the frag.– Kaiido
Jan 1 at 12:57
... Fast reading MDN article about
nth-of-type()
it sounds like it is a recent addition that "matching elements are not required to have a parent." (still a Draft). Safari probably didn't catch on yet.– Kaiido
Jan 1 at 13:03