While adding Arabic/Persian support to Mosaic, I came up with a similar
rule, but applied it globally to all elements in the document.
What you have described above, I call "dynamic alignment". In my
implementation all elements of HTML including images have an implied
alignment, even though some have "ALIGN=" attributes. All Elements with
text use dynamic aligment algorithm to determine their implied alignment,
(left-to-right or right-to-left) unless that element has an "ALIGN"
attribute which overrides the dynamic alignment. The interesting case
is lists:
<UL>
<LI> Arabic text..
<LI> English Arabic...
<LI> Arabic English
<LI> English text...
</UL>
Here based on a modified "dynamic alignment" algorithm one sets the
directory of the whole list. Even more fun is if this was Unicode
encoded HTML with Unicode direction hints.
Images are also an interesting case because they have no "text"
to provide hint of direction. I use the surrounding text direction
as a guide and also the "document primary direction". The latter is
computed using the same dynamic alignment algorithm, but over
the whole document.
-anoosh