Bug 729256: Busy loop under replace_local_image_links()

Rework replace_local_image_links to use CSS selectors to avoid iterating
through the DOM tree.