I am trying to merge two successive div
elements if they have the same coordinates for their borders. While doing tests, I noticed a strange behavior. It seems that sometimes the parent bounding rectangle does not properly compute the coordinates based on its children elements, or at least, there is something that I am missing.
Imagine the example below:
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.border {
border: 5px outset red;
text-align: right;
}
.color {
background-color: lightblue;
text-align: center;
}
</style>
</head>
<body>
<div class="border">
<div class="color">
<h2>Some Title</h2>
<p>Some text.</p>
</div>
</div>
</body>
</html>
This would render to the image below:
My merging algorithm checks if a div has a unique child which is a div and then check if they have the same bounding rectangle, in this case a call to getBoundingClientRect
(or getClientRects
for that matter) yields the same result for both div
in the example. However upon merging, I realize that I have a different image. The merge yields the following html code:
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.border {
border: 5px outset red;
}
.color {
background-color: lightblue;
text-align: center;
}
</style>
</head>
<body>
<div class="border color">
<h2>Some Title</h2>
<p>Some text.</p>
</div>
</body>
</html>
Which is rendered like this:
Note that the white stripes on the top and the bottom disappeared. Both red rectangles have exactly the same dimensions, only now the background is filling the entire space.
Using the inspection tooling, I discovered that is because the rectangle of the element h2
and p
are actually bigger than the one of the div
with the border class, but when I add both class to the div in the second case, then the behavior is as I would expect (matching the rectangle of both children elements). I looked in the specs for the reason of that behavior, but I guess I miss the proper vocabulary since I could not manage to find anything on that.
So here are my questions:
- Would a div with no border define behave differently than one with borders?
- If the bounding rectangle does not give me the actual rendered rectangle of the div, which strategy would be more reliable?