Change has_declaration to work on node pointers

This is more for consistency with the surrounding code than for performance.
Add more assertions for page memory handling code
Prevent depth underflow when printing documents

Since depth is unsigned this is actually well-defined but it's better to not
have the underflow anyway.
Make sure remove_node fully detaches the node

Right now remove_node is only used in contexts where parent is reset after
removing but this might be important in the future.