added same changes as in master

@bcordis As you suggested i just added the same changes to development branch. I can't test it without a running installation, so please test it.
A question to the variable `$custom`: is this really a string? Because when the while-loop is not executed (when there are no braces), line 51/52 will produce an error when you want to access $element->id of a string.