Closed (outdated)
Project:
Drupal core
Version:
6.x-dev
Component:
filter.module
Priority:
Normal
Category:
Bug report
Assigned:
Unassigned
Issue tags:
Reporter:
Created:
23 Jan 2009 at 03:52 UTC
Updated:
2 Mar 2016 at 22:18 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #1
karschsp commentedTagging for the novice queue. Good way to brush up on regular expressions! :)
Comment #2
stewsnoozeComment #3
stewsnoozehow about this attempt.
Comment #4
david straussA more direct place to put this would be in
_filter_xss_attributes, which already strips thestyleattribute. Then again, this isn't really an XSS problem, so I'm kind of split.But, regardless of implementation mechanism, this needs a unit test, so I'm setting this to "needs work."
Comment #5
taz88ny commentedsubscribe
Comment #6
sharda_ram commentedsubscribe
Comment #7
stewsnoozeComment #8
stewsnoozeA few SimpleTests have been added.
Comment #9
cburschkaI'm not sure why the PHP parser doesn't choke on this, but there's a double semicolon in
It looks good otherwise. :)
Comment #10
stewsnoozeComment #11
stewsnoozeNew patch with extra ; removed.
Comment #12
david straussThis is looking much better now.
One change: please have the tests verify that the cleaned tag comes out properly and not just that the
rel="follow"has been stripped. All tests would currently pass if_filter_html()just returned a blank string, and that shouldn't be the case.Comment #13
stewsnoozeThis now validates the correct string is returned, and it isn't just blank e.t.c.
Comment #14
david straussThere are minor code style issues with the tests, but this is looking very close to RTBC now.
Comment #15
stewsnoozeAre you just talking about me using single quotes instead of using double and \escaping?? or bigger than that?
Comment #16
david strauss* Commas should have spaces after, but not before.
* String concatenation operators should have a space on each side.
* Grammar, capitalization, and punctuation need cleanup in the assertion descriptions.
* Tests should use assertNoText() instead of attempting a string match.
* Spelling error: "obviosy"
That's most of it; I haven't run the code through any automated style checking.
Comment #17
stewsnoozeComment #18
stewsnoozeNew patch rolled with David's comments rectified, I think. This should be better.
Comment #20
stewsnoozeNew patch with test passing loveliness.
please review
Comment #21
tic2000 commentedWouldn't a "simple"
$text = preg_replace('/<a([^>]+)(rel=[^> ]+)([^>]*)>/i', '<a\\1\\3 rel="nofollow">', $text);be easier?Or
$text = preg_replace('/<a([^>]*)( rel=[^> ]+)([^>]*)>/i', '<a\\1\\3 rel="nofollow">', $text);so we don't end up with double spaces.Comment #22
stewsnooze@tic2000
If I had that would strip the whole thing.
My code handles only the rel element *I think*. Whereas I think your regex assumes it is the last item.
Comment #23
tic2000 commentedNope, it doesn't assumes that. That's why there are 3 pairs of () and there is
\\1\\3. 1 is everything before rel (at least a space in the first case or nothing in the second case if rel is first attribute). 2 is rel (without the space before it, or with the space). 3 is anything after rel (if there is anything left).Where my regexp will fail is if there is a space like rel="follow something" and I don't know if your code would handle that better. For sure it would fail on something more complex.
Comment #24
tic2000 commentedThis should take care of any complex rel attribute.
It uses DOMDocument to remove the old attribute, if there is one and add a new clean one.
I don't like the fact that it uses some of the code in html corrector, but filter.module would need a good refactoring to get rid of that.
Comment #25
tic2000 commentedhook_filter should not be touched.
Comment #26
stewsnoozeI didn't realise DomDocment is part of php5 core.
Your approach is better.
+1.
Comment #27
david straussThis approach is most excellent.
Comment #28
stewsnoozetwo of us have reviewed the latest patch and think it is good enough? I am RTBCing but unsure of normal RTBC protocol.
Comment #29
stewsnoozeAlthough this does have some of my code in. back to needs review for someone else
Comment #30
tic2000 commentedLast patch has nothing of your code in it.
Comment #31
stewsnoozeAdding the tests back. They got lost in the recent patches.
Comment #33
stewsnoozeI've added the tests back, they didn't apply properly. We need tests for this patch, although the old ones weren't brilliant. We should probably do some more DomDocument stuff to make sure the rel attribute exists and is set to nofollow
Comment #34
tic2000 commentedThere are already tests in HEAD for the spam deterent. I only added two for the case where rel is already set to check if the old one is removed and rel="nofollow" is added.
With DOMDocument you don't need check for rel=follow, rel='follow' because DOMDocument will automatically convert those to use double quotes.
Comment #35
stewsnoozeIn that case this is a fine patch. I hope to see more conversions from regex to DomDocument in the future.
Comment #36
tic2000 commentedThere is no need to remove the attribute first.
Comment #37
tic2000 commentedAnd this one with just a simple setAttribute instead of setAttributeNode
Comment #38
damien tournoud commentedEven nicer!
Comment #39
webchickHm. Do we need to benchmark this? Also, just one question:
I don't understand what this part has to do with link rel tags?
This review is powered by Dreditor.
Comment #40
tic2000 commented@webchick
About performance you can check #374441-23: Refactor Drupal HTML corrector (PHP5), but we can try do to a test with the regexp patch and with the dom patch and see how it goes.
About the code, as you can see when loading the HTML into the DOM we add the html and body tags too. After the processing is done we only need what's inside the body tag and the last regexp is to make
<br/>become<br />and things like that.As DamZ suggests, we need a wrapper function for loading and saving the DOM because right now the HTML corrector uses this code. Also this patch and the patch in #16161-122: Move Read More link to end of node content. But that is for another issue.
Comment #41
david straussTypo: "noffolow"
Comment #42
tic2000 commentedDone some testing. Note that I don't have a perfect testing environment, just my pc.
10000 iterations using the source of my comment in #40 which has 2 links.
Patch in #20 average 5.35 seconds
Patch in #37 average 3.1 seconds
Comment #43
tic2000 commentedComment #44
tic2000 commentedTypo fixed. Back to RTBC.
Comment #45
tic2000 commentedJust some extra whitespace removed
Comment #46
damien tournoud commentedRegarding the DOM versus string parsing: DOM is generally faster then complex string parsing, and it all boils down to whether you prefer:
or
(that's supposing we properly refactor the DOM loading / serializing code that is already used elsewhere in core (in the HTML corrector)
Comment #47
tic2000 commentedI opened #542742: Filter: Create wrapper functions to load/serialize a DOM. for the wrapper functions.
Comment #48
webchickI'd like to get #542742: Filter: Create wrapper functions to load/serialize a DOM. in first. When I do, this will require a re-roll, so marking "needs work" pre-emptively. ;)
Comment #49
damien tournoud commentedComment #50
tic2000 commented#542742: Filter: Create wrapper functions to load/serialize a DOM. got committed. Back to work on this one. Well, review :)
Comment #51
damien tournoud commentedNice job, tic2000.
Comment #52
webchickAwesome!
Committed to HEAD.
Comment #54
Garrett Albright commentedI'm a bit late to the party, but I just noticed this in core. And it may be bad form, but I'm re-opening the issue because I think a bit more discussion needs to happen here.
First off, when adding "nofollow" is enabled, then the functionality of this filter now encompasses that of the HTML corrector filter.
That means we could be causing redundant work, but more vitally, shouldn't adding "nofollow" and "correcting" the HTML be distinct actions? What about cases where someone may want to do the former without doing the latter; that's no longer possible. People may be surprised when they enable adding "nofollow" and then find their input also being tinkered with in other ways.
Does this not also open the door for other filters beyond the HTML corrector to also use the DOM object in their filtering? I personally would love to use it in Pathologic, but I can't use it without also "correcting" the HTML, but if the HTML is going to be corrected anyway then why not, but I'm still redundantly creating the DOM object and then unserializing it, and…
Now personally, I'm all for making more use of the PHP DOM object in filtering (#807996: [meta] Input filters and text formats), but we need to be sure the world is ready for it…
+1 for switching back to regex parsing for D7 - and then going DOM object crazy in D8.
Comment #55
damien tournoud commentedNo way. We need to refactor the filtering system for D8. In the meantime, even recreating the DOM object each time is faster then crazy regexps.
Comment #56
Garrett Albright commentedPlease address this, at least.
Comment #57
damien tournoud commentedIf the HTML is not valid to begin with, then the mere concept of
adding 'nofollow'is perfectly meaningless, and the behavior of the filter would be unspecificed.Comment #58
Garrett Albright commentedIndeed it would, but I'm still not satisfied. What about the fact that this filter is doing something it shouldn't be - that it doesn't state it does? It's like taking your car to the shop to get the heater worked on, and when you get it back you find that the stereo has been replaced too, free of charge, with no mention of it made by the mechanic. In many cases it may be appreciated, but perhaps you were more happy with your old stereo and really didn't want the mechanic mucking about anywhere besides the heater.
A labored metaphor, but still…
webchick suggested I take it to a new issue. Perhaps I will in the morning.
Comment #59
drewish commentedWe're getting a bunch of spam comments on D6 sites that cause Google to flag the sites as "adult" which removes them from SafeSearch results. I really think this is a bug fix that needs to be backported.
Comment #60
cannandev commentedI've made a small adjustment for the D6 _filter_html() and posted it at #933538: Override rel="nofollow" problem with _filter_html().
Thanks tic2000.
Comment #61
mcurry commentedsubscribe
Comment #62
mcurry commentedsubscribe
Comment #63
deviantintegral commentedI'm running into this as well, and agree that it should be backported to D6.
Comment #64
deviantintegral commentedHere's a patch that removes any existing rel attributes before adding our own. It uses preg_replace only so that it will work with PHP4. I tested with the following content:
Which produces:
I still need to fix the extra space that's showing up.
Comment #65
deviantintegral commentedHere we go.
Comment #66
deviantintegral commentedComment #68
deviantintegral commentedTestbot shouldn't be firing on this issue.
Comment #69
Niklas Fiekas commentedLet's give him another try. #65: 362972-65.remove-rel-6.x.patch queued for re-testing.
Comment #70
eikes commentedhave you thought of these?
Comment #70.0
eikes commentedUpdated issue summary.