-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DomCrawler] Add method flatText() #23626
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
10857ea
to
92ee667
Compare
What version number should be used for adding an entry to the changelog? |
3.4 (and the PR be rebased + retargetted to branch 3.4 if you can.) |
Added changelog entry, and rebased to 3.4 (I had followed the instructions, they tell to submit to master…) |
@vlakoff these instructions are true for 18 months when we don't develop x.4 and x+1.0 in parallel. during the 6 months where we prepare the next minor and the next major, we have 2 active development branch (and anything which is backward compatible goes into the next minor) |
Another thing, should there be a PR for adding this method to the documentation here, or is better to not add clutter and have it as a "hidden feature"? |
@vlakoff there should be a PR for docs ... but only if this proposal is finally approved. So let's wait a bit. |
public function flatText() | ||
{ | ||
if (!$this->nodes) { | ||
throw new \InvalidArgumentException('The current node list is empty.'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if it's OK to throw an exception in this case. Would it be better to simply return an empty string?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer the exception =/ same as text() does.
Note this method can be a simple one-liner return trim(preg_replace('/\s+/', ' ', $this->text()));
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Throwing an exception is consistent with the rest of this class, which indeed I think is quite strict in practice (in my code I have to add a lot of tests for possibly absent nodes, instead of just getting null).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couldn't it be an argument to the existing text()
method instead?
* | ||
* @throws \InvalidArgumentException When current node is empty | ||
*/ | ||
public function flatText() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like the proposed function name (flatText()
) because this is usually a verb ("flatten") and here we're proposing to use an adjective ("flat"). I've been looking for in the standard libraries of some programming languages (Go, JavaScript, Java, C#, ...) but nobody defines a function like that (they only implement something like our trim(), ltrim(), rtrim(), etc.) Even Laravel doesn't define anything like this in Illuminate\Str.
Maybe trimAll()
would be a better name? Or flatten()
? Or trimText()
, flattenText()
? I'm not sure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's more a getter than an action, so I think a noun rather than a verb is fine (and actually better).
By the way, I had thought about flattenedText
, but it's too long.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're flattening text()
.. it's an action :) i'd also favor flattenText()
here.
Note JS does this by default ( |
We cannot change the behavior of |
True. Just saying it might be better to update it's behavior instead, if this is about DX. We could bridge the BC with a temp. arg |
Thoughts about new method vs. adding a parameter ? (notice the latter isn't strictly BC, if user were extending the method) |
Turning my -1 into a -0, ping @symfony/deciders it's up to us now. |
I'm 👍 about the feature (truly useful and needed very often) but I'm still 👎 about the method name ( |
@javiereguiluz any suggestion to help move forward? |
I've asked in Symfony Slack, and the community has suggested these alternative names:
|
Maybe we can follow @ro0NL proposal and wait until 4.0 to make a BC break? We could have:
|
@javiereguiluz But even with a BC break in 4.0 we would need an upgrade path. We could do so with a new |
It seems like things are getting complicated only because we can't figure out a proper name for the function... I'm unhappy with some of the proposals above, because the function does two things, it doesn't only trim. About reversing the function behaviors, this still can be done later. I just thought of a new one, how about |
Maybe |
As much as I'm usually against such flags, in this case I agree it's our best choice. |
The flag sounds like the best idea, indeed. |
Added a commit to change to the "flag" way. Comments welcome about any rewording, nitpicking, etc. |
Don't use a flag for this unless for deprecation.
|
I understood your point about using the flag only for deprecation. But TBH, handling all this transitional code is getting incredibly boring to me. So I'm closing this, if someone wants to take it over, he would be very welcome. |
see #24412 as a replacement |
…t() method (Simperfit) This PR was merged into the 4.4 branch. Discussion ---------- [DomCrawler] add a normalizeWhitespace argument to text() method | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | #21302, #23626 <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | todo <!-- required for new features --> <!-- Replace this notice by a short README for your feature/bugfix. This will help people understand your PR and can be used as a start for the documentation. Additionally (see https://symfony.com/roadmap): - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against branch 4.4. - Legacy code removals go to the master branch. --> I've taken #24412, rebased with 4.4, fixed the comments in the PR. cc @xabbuh @stof @nicolas-grekas Commits ------- 1746718 [DomCrawler] add a value() method, normalize whitespaces
Similar to
text()
, with some normalization applied to the value: surrounding spaces/newlines are removed, and consecutive spaces/newlines are reduced to one. Helpful if you don't want your code to choke because of the HTML source formatting.This is a follow-up to #11470 and #21302. ping @javiereguiluz, @jpetitcolas.
Quoting #21302 (comment):