Fixes bug 13976. Garbage input should not cause error in buildFragment.

Please review this PR. Thanks.