Fix `mediaSingle` traversals

Issue #35 closed
Chris Fuller created an issue

The mediaSingle node type is weird because it has a content section, but the nodes within it are restricted in ways that do not fit well with the ContentNode interface, so it doesn’t implement it. The result is that many of the things that “just work” for most other nodes don’t work for mediaSingle without some special support.

Currently, the traversal functions like allNodes() and transformDescendants are not working properly for mediaSingle nodes, and that is a serious problem. In particular, somebody could be trying to visit all media nodes for some reason (example: clearing all the collection values for them), and currently any of the ones inside a mediaSingle would be missed!

The allNodes() case is easy enough to deal with, as we can just inject the media and (if present) caption nodes into the node stream. But the story is a lot nastier for transformDescendants. One of its features is that the transformer function is permitted to return null, which means to remove that node. There is a warning on it that this could leave the document in an invalid state, but in the case of deleting a media from inside of a mediaSingle, there is no “if” about it: that would unconditionally result in an invalid document. So what can we do?

What I’ve decided to do is this: If the traversal enters a mediaSingle node and the transformer wants to remove the media within it, then the mediaSingle is removed, too, and its caption (if any) does not get visited.

Comments (2)

  1. Log in to comment