Problem/Motivation
New Drupal developers may confuse the proper use of check_plain(); since it extends the core htmlspecialchars() function, this would be a more appropriate function name.
Proposed resolution
Rename the check_plain() function (and all calls to it) to drupal_htmlspecialchars()
New Drupal developers will have an easier time comparing what this function does (extend the core PHP function htmlspecialchars()) and avoid misuse.
Remaining tasks
- Needs review
- API documentation updated to reflect core change
add API Change record
User interface changes
no impact to user interface (strictly core and module code change)
API changes
check_plain() is now drupal_htmlspecialchars() - to more appropriately identify what this function does and to move to a more manageable namespace for Drupal functions which extend core PHP functions, the function check_plain() is now drupal_htmlspecialchars()
Original report by agentrickard
This is tied to #455724: Deprecate check_markup(). If it makes sense for clarity and consistency, we should rename check_plain() to filter_plain() (or something else).
Patch coming, after I sync issue #s.
Comments
Comment #1
agentrickardAnd a patch.
Comment #2
agentrickardRenamed patch for clarity.
Comment #3
heine commentedI don't think filter_plain makes what the function does any less confusing. check_plain doesn't filter anything; it converts plain text (
x < 2) to HTML (x < 2) by escaping certain characters.I'd rather see something like:
check_plain: converts "plaintext" to HTML: plaintext_to_html()
check_markup: converts "richtext" to HTML: richtext_to_html()
(BTW: filter_xss operates on HTML and limits tags used to a whitelist. It also strips attributes that can cause an XSS attack.)
Comment #4
agentrickardI'm pretty neutral on that. I recall greggles saying in the previous issue that his preference is for functions like that to have a drupal_ namespace, so:
check_plain => drupal_plantext_to_html()
check_markup is likely going in as filter_markup() for namespace consistency with filter.inc.
Comment #5
sunUnlike #455724: Deprecate check_markup(), I am rather opposed to this change. It's a change for consistency, but check_markup() and check_plain() are not at all comparable, and therefore, I rather think it's wrong to name them consistently.
Additionally, check_markup() is provided by and bound to filter module, and thereby using a wrong function namespace. check_plain(), however, lives in bootstrap.inc.
check_plain() is a rather simple, UTF-8-safe wrapper around htmlspecialchars(), so if there was a better name, then it would probably be drupal_htmlspecialchars() - following the pattern of many other wrappers around PHP functions in Drupal.
Comment #6
agentrickardAlright. Marking this one won't fix then? By design?
Comment #7
sunNot sure... it's the first time I thought about this function name, and changing
check_plain() => drupal_htmlspecialchars()
would most probably help many Drupal newcomers to understand why they want to use it. They would normally use htmlspecialchars(), because they are used to it from their own PHP code and other web applications. "check_plain" is very confusing for them (and I remember when I started with Drupal, was used to htmlspecialchars(), and found it hard to recall the purpose of check_plain()).
A change to drupal_htmlspecialchars() would nicely map to other utf-8-related Drupal wrappers:
drupal_strlen()
drupal_strtolower()
drupal_strtoupper()
drupal_substr()
drupal_ucfirst()
Comment #8
damien tournoud commentedOk, this makes sense. +1 for drupal_htmlspecialchars().
Comment #9
agentrickardRe-rolled, with name change and a minor docblock change to the function, explaining why we are extending htmlspecialchars.
Comment #10
sunSome PHPDoc is exceeding 80 chars due to the longer name now.
Not sure about this change... The other define is PASS_THROUGH, which does not map to a function name, so I guess this wouldn't have to be that lengthy...?
That said, why on earth didn't we go with a simple Boolean here?
7 days to code freeze. Better review yourself.
Comment #11
moshe weitzman commentedI thin kthis would help *some* drupal newcomers who already know php. but others will immediately understand the association to plain text better than specialchars. i like the 'check' functions to all be in the same namespace. for me, filter_markup and filter_plain work.
Comment #12
agentrickard@moshe
Then I think you should weigh in on #455724: Deprecate check_markup(), which I am going to stay away from until someone makes a decision.
Comment #14
atheneus commentedI'm wondering how high the priority is on refactoring API calls at this point as we are looking to the finish line for a release of D7. Changing this is going to break a lot of contrib modules & site installation custom mods and I think we should making things as easy as possible for mod maintainers to get their modules ported to D7 as quickly as possible at this point. This is particularly so the first alpha has been released.
I'm in favor of refactoring the API, but I would vote for postponing it for D8. I think this is a useful conversation, but the lack of a clear-cut naming convention and no obvious consensus would suggest to me that more time to really think about it would be preferable. We don't want a situation where the naming of these function changes in D7, and then again in D8.
[edit] -
check_markup()has already changed the function prototype between D6 and D7. However, the API oncheck_plain()has so far not changed.Comment #15
sunNo more non-critical API changes.
Comment #16
marvil07 commentedSince the patch is a little old, and rebasing from the point it was done requires many manual fixes, I tried to do it again.
Basically it's raw replacing and adding the function phpdoc description change manually. For reference:
Let see what testbot thinks.
PS: I guess the comments in 8 still applies, but let see if tests pass ok first.
Comment #17
marvil07 commentedNow with fixed comments.
BTW dreditor was a huge help to identify those lines in the patch :-)
Comment #19
marvil07 commentedI have been looking at vcs history, and I end up in #242873: make drupal_set_title() use check_plain() by default., that's where that change got in, and it seems like the original patches were using boolean, but it's a long thread and I did not finish to read it.
In the other side, IMHO that deserves another issue.
Comment #20
tim.plunkettSo future newcomers to the issue know what it's being renamed *to*.
Comment #21
tim.plunkett#17: 0001-Issue-557148-by-agentrickard-marvil07-Rename-check_p.patch queued for re-testing.
Comment #22
marvil07 commentedTrying again.
Comment #23
marvil07 commentedUploading to review comments on dreditor
Comment #24
marvil07 commentedComment #25
dellintosh commentedRe-rolling patch to work with HEAD as of now.
Please review.
Comment #26
dellintosh commentedwrote issue summary, removing tag.
Comment #26.0
dellintosh commentedAdding Issue summary
Comment #27
dellintosh commentedassigning to myself; writing API change notification
Comment #28
dellintosh commentedChange record added - API Change record
Comment #28.0
dellintosh commentedUpdated issue summary.
Comment #29
patrickd commenteddoes it really make sense to create a change record before the change is committed yet ?
Comment #30
robloach#25: issue-557148-by-agentrickard-dellintosh-rename-checkplain-to-drupalhtmlspecialchars.patch queued for re-testing.
Comment #32
heine commentedI've unpublished the change record.
Comment #32.0
heine commentedupdating issue description to include API Change record link
Comment #33
ianthomas_ukSuperseded by #2302363: FormattableMarkup and HTML utility components replace check_plain(), format_string(), drupal_placeholder() (and SafeMarkup)