That's a readline feature (quoted insert C-q). So the terminal is smart enough to detect readline and then quote the newlines?
There's lots of other programs that use C-q for quoting, for example emacs, and readline probably copied it from there in the first place.
When pasting text, it's a different code path than quoting. The terminal surely knows when text is being pasted. If nothing else, a simple heuristic based on typing speed is enough to tell apart typing and pasting.