Escape entities when serializing attributes and text

* Add decodeEntity and escapeEntities methods in Util and use it from the
  tokenizer and the serializer.

* Escape the & char in (attribute-)text if followed by something that would
  decode to an entity. This leaves newly-inserted bare and chars unescaped.
  Existing entities in text content are round-tripped using span
  encapsulation. This does not work for entities in attributes, as the
  mw:Entity wrapper is removed when converting the attribute to a string. This
  can still lead to syntactic diffs. This new escaping code will however
  eliminate semantic diffs for cases like <div title="&amp;amp;">.

No changes in parser tests results (only some output diffs in failing tests).

TODO: Add parser tests.

* Also avoid a crash in selser image serialization if the resource attribute
  was not found, which greens two parser tests.

Change-Id: Ie51cf8292096d9e455e96e7da6a4d0848111448f
Merge "Escape entities when serializing attributes and text"