Javascript selection is more sophisticated than CSS selection, so one approach is to use javascript to cycle through all the [aria-label="Story"]
elements on the page, checking to see if each one contains at least one [aria-label="Story"]
child element.
If it doesn't contain any, a class .contains-no-story
may be applied which indicates that this element is an instance of [aria-label="Story"]
which doesn't contain another of itself.
Working Example:
var stories = [... document.querySelectorAll('div[aria-label="Story"]')];
stories.forEach(function(story){
story.classList.add('contains-no-story');
for (var i = 0; i < story.children.length; i++) {
if (stories.indexOf(story.children[i]) > -1) {
story.classList.remove('contains-no-story');
}
}
});
[aria-label="Story"] {
margin: 12px;
padding: 12px;
color: rgb(255, 255, 255);
background-color: rgb(255, 0, 0);
}
.contains-no-story {
background-color: rgb(0, 0, 255);
}
<div aria-label="Story">I contain another <code>[aria-label="Story"]</code> element
<div aria-label="Story">I do not contain another <code>[aria-label="Story"]</code> element</div>
</div>
<div aria-label="Story">I do not contain another <code>[aria-label="Story"]</code> element</div>
N.B. It's beyond the scope of this answer, but if you were to use axe selectors, then the CSS styles (including the any ancestor axe selector ^
) would simply be:
[aria-label="Story"] {
background-color: rgb(0, 0, 255);
}
[aria-label="Story"] ^ [aria-label="Story"] {
background-color: rgb(255, 0, 0);
}
See: http://rounin.co.uk/projects/axe/axe2.html