-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Full data support #221
Full data support #221
Conversation
Would you mind rebasing these commits on top of the head of master? |
Alright, done. Let me know if you need anything else. |
Are you sure you pushed your branch (with |
Hum, I'm not sure what I'm missing here... I did the rebase of |
Here are the commands I would run (everyone uses Git slightly differently):
|
Alright, just did it. Should be good to go now. |
The pull request still hasn't been updated. I'll review it as is; we can easily fulfil it manually if need be. |
el.data[name] = encode(value); | ||
} else if(typeof name === 'object') { | ||
// If its an object, loop through it | ||
for(var key in name) { |
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.
Let's use _.each
here.
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 wasn't using Underscore's each
because attr
wasn't using it either. Thought that following the standard would be the right thing to do.
I'll add each
for object loops both in data
and attr
.
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.
Cool. I advocate a somewhat functional style wherever possible, and Matt hasn't yet complained. ;)
if(elem.data === undefined) elem.data = {}; | ||
var value; | ||
for(var key in elem.attribs) { | ||
if(key.match('data-')) { |
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.
This needs to check that the key begins with "data-":
> 'foo-data-bar'.match('data-')
["data-"]
I suggest:
var match = /^data-(.*)$/.exec(key);
if (match) {
elem.data[match[1]] = elem.attribs[key];
}
@davidchambers Just did what you asked for. Would you mind explaining me again what you want me to do regarding the pull request? I can't see anything wrong in here and I want to understand the issue. |
@@ -19,6 +19,8 @@ var formatAttrs = function(attributes) { | |||
|
|||
// Loop through the attributes | |||
for (var key in attributes) { | |||
// Make sure it doesn't format data attributes. | |||
if(key.match('data-')) continue; |
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.
This check needs to be anchored to the start of the string, too.
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 mean it doesn't need only to check for data-
but also make sure it starts with it, right?
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.
Yes, that's right. Something like /^data-(.*)$/
should do the trick.
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.
That's just awful. A regexp is way too powerful here, use key.substr(0, 5) === 'data-'
.
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 took it from a snippet I suggested elsewhere, to avoid doing both key.substr(0, 5)
and key.substr(5)
:
var match = /^data-(.*)$/.exec(key); if (match) { elem.data[match[1]] = elem.attribs[key]; }
Here's the current commit graph:
Here's what I'd like to see:
|
So you want |
@rafaelrinaldi the goal is to be able to merge the pull request from the github interface. that way we can make sure there aren't any merge conflicts and tests don't break. |
Sure. There will be some merge conflicts to resolve, so you might as well tackle them now rather than later. |
… behavior. Suggestion made by @davidchambers on PR #221.
…x instead as suggested by @davidchambers on PR #221.
FYI: I'm kinda busy in a project right now but I expect to finish all the stuff needed for this PR to be merged in a couple of weeks. |
Tried this, didn't work correctly. I have |
@pihvi It took me a while but I did an update that fixes that. A little test snippet I just made: var $ = require('./lib/cheerio');
var markup = '<a href="#" data-foo="bar">Link</a>';
// Ordinary operations.
console.log($(markup).text()); // Link
console.log($(markup).attr('href')); // #
// Testing data support.
console.log($(markup).data('foo')); // bar
console.log($(markup).data()); // { foo: 'bar' }
// Adding a new value.
markup = $(markup).data('lorem', 'ipsum');
console.log(markup.data()); // { foo: 'bar', lorem: 'ipsum' } Can someone guide me on doing the proper merge/rebase for the branch I'm working on? @davidchambers maybe? |
Tried this and got: ReferenceError: DATA_PREFIX_REGEX is not defined |
@rafaelrinaldi: I suggest running the commands listed in my earlier comment. On another note, this is a good test case to include: > $('<div data-foo-bar="42">').data()
{fooBar: 42} This tests both the conversion to camel case and the type coercion. |
@pihvi You must use the branch |
@rafaelrinaldi I pointed cheerio in npm package.json to your data-support branch and got the error. Isn't the DATA_PREFIX_REGEX variable added by you to that branch? If that's so, the error can not occur unless your branch would be in use. |
@rafaelrinaldi here is the troubling line: Looks like you forgot to require it in var DATA_PREFIX_REGEX = require('./utils').DATA_PREFIX_REGEX; |
Since I need this functionality as well and couldn't wait on a solution on this, I polished @rafaelrinaldi solution so that it can be merged. |
add .data() function. cleaned up PR #221 from @rafaelrinaldi, added tests, updated readme
Close? |
Adding support to
data()
just like in jQuery. Wrote a new formatter, new parser and elements now havedata
property.I've added tests as well, and they're all passing. Unless the ones who wasn't passing already (at least, from the version I've forked).
@mcwhittemore already made a PR on the same thing, as you can see on the issue #215 but I've used a different approach.