I am facing this strange problem that I have brought down to a MWE. I am not an expert in this, sorry if it is something obvious.
I have this HTML document:
var floats = document.getElementsByTagName("custom-float");
for (var i = 0; i < floats.length; i++) {
var caption = floats[i].getElementsByTagName("float-caption");
if (caption.length == 0) {
throw 'This float has no caption!';
}
}
<p>
A paragraph.
<custom-float>
<div style="display: flex;">
<image src="1.svg"></image>
</div>
<float-caption>A description of the image.</float-caption>
</custom-float>
</p>
For some reason when I open the HTML (both in Firefox 90.0 and Brave 1.26.77 in Linux) the This float has no caption! error is thrown, so this means that getElementsByTagName is not returning the float-caption element.
For my surprise, I noticed that if I remove the <div style="display: flex;"></div> wrapper and/or if I place the </p> before <custom-float>, everything works as expected. I.e. the following two HTML documents work fine:
var floats = document.getElementsByTagName("custom-float");
for (var i = 0; i < floats.length; i++) {
var caption = floats[i].getElementsByTagName("float-caption");
if (caption.length == 0) {
throw 'This float has no caption!';
}
}
<p>
A paragraph.
<custom-float>
<image src="1.svg"></image>
<float-caption>A description of the image.</float-caption>
</custom-float>
</p>
var floats = document.getElementsByTagName("custom-float");
for (var i = 0; i < floats.length; i++) {
var caption = floats[i].getElementsByTagName("float-caption");
if (caption.length == 0) {
throw 'This float has no caption!';
}
}
<p>
A paragraph.
</p>
<custom-float>
<div style="display: flex;">
<image src="1.svg"></image>
</div>
<float-caption>A description of the image.</float-caption>
</custom-float>