Emacs comes with a web browser built in. Although it’s often regarded as a novelty, EWW (as it’s known) is a real browser, and I use it all the time. A lot of web browsing involves text, and Emacs is the ultimate text-handling platform, so it’s obvioiusly a good match.
But as with many things in Emacs, EWW has some irregularities. For example, here is the Wiktionary entry for the word “locus” as viewed in EWW:
Five definitions are listed, but one of them is blank. What is the missing definition? Is it important? I sure hope not. Dropping information from a web page without warning would be a serious problem!
Compare with the same page as viewed in Firefox:
The Firefox version shows only four definitions, exactly the same ones shown in EWW. So it appears that rather than dropping a definition, EWW is inserting a blank one. That’s not such a serious problem, but it’s still kind of annoying.
To see what’s going on, we need to look at the HTML source. (This can be obtained from within EWW by pressing v.) The word definitions are grouped in an ordered list ol with each individual definition showing up as a list item li:
Apparently the blank definition comes from an explicitly empty list item: <li class="mw-empty-elt"></li>. I don’t know why Wiktionary includes that list item, and I don’t know how Firefox knows not to display it.
It’s not obvious that it’s a bug to show the empty list item. I mean, it’s there, right? But still, it looks like an error, so I don’t want to see it. This will require modifying the browser’s behavior.
Fortunately, Emacs provides a mechanism to unobtrusively modify any behavior at all, namely advice. In short, we will define a new function that will attach itself to an existing function and do some extra stuff.
This could be done in lots of different ways. My idea was to modify the list item handler shr-tag-li, a function of one argument:
We need to define a new function of two arguments to wrap this one. The two arguments will be the handler function and its argument, and the wrapping function will call the handler on its argument only when the list item is non-empty:
The wrapper will be attached to the wrapped with advice-add: