{"action":"created","issue":{"url":"https://api.github.com/repos/cheeriojs/cheerio/issues/1241","repository_url":"https://api.github.com/repos/cheeriojs/cheerio","labels_url":"https://api.github.com/repos/cheeriojs/cheerio/issues/1241/labels{/name}","comments_url":"https://api.github.com/repos/cheeriojs/cheerio/issues/1241/comments","events_url":"https://api.github.com/repos/cheeriojs/cheerio/issues/1241/events","html_url":"https://github.com/cheeriojs/cheerio/issues/1241","id":372120067,"node_id":"MDU6SXNzdWUzNzIxMjAwNjc=","number":1241,"title":"cheerio's data model inconsistent with itself","user":{"login":"rdm","id":671269,"node_id":"MDQ6VXNlcjY3MTI2OQ==","avatar_url":"https://avatars2.githubusercontent.com/u/671269?v=4","gravatar_id":"","url":"https://api.github.com/users/rdm","html_url":"https://github.com/rdm","followers_url":"https://api.github.com/users/rdm/followers","following_url":"https://api.github.com/users/rdm/following{/other_user}","gists_url":"https://api.github.com/users/rdm/gists{/gist_id}","starred_url":"https://api.github.com/users/rdm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rdm/subscriptions","organizations_url":"https://api.github.com/users/rdm/orgs","repos_url":"https://api.github.com/users/rdm/repos","events_url":"https://api.github.com/users/rdm/events{/privacy}","received_events_url":"https://api.github.com/users/rdm/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":4,"created_at":"2018-10-19T21:21:14Z","updated_at":"2018-10-21T22:22:01Z","closed_at":"2018-10-21T15:30:24Z","author_association":"NONE","body":"When passing data between two apis, I have html fragments from one which use protocol relative urls but the other requires absolute urls. This seems like a good reason to use cheerio. Unfortunately there seem to be a variety of problems in this context:\r\n\r\nWith cheerio version 1.0.0-rc.2,\r\n\r\n    const cheerio= require('cheerio');\r\n    const test= \"<b><a href=\\\"//example.com/c\\\">Click here</a><br /><a href=\\\"//other.com/d\\\">Click \r\n    here</a></b>\";\r\n    const $= cheerio.load(test);\r\n    for (let el= $('a[href]'); el.length; el= el.next('[href]')) {\r\n    \tconst href= el.attr('href');\r\n    \tconsole.log(el.length, el.html(), href);\r\n    \tif (href.startsWith('//')) {\r\n    \t\tconsole.log(`fixing ${href}`);\r\n    \t\tel.attr('href', 'https:'+href);\r\n    \t} else {\r\n    \t\tconsole.log(`this looks ok: ${href}`);\r\n    \t}\r\n    }\r\n    console.log($('body').html());`\r\n\r\ngenerates the result:\r\n\r\n    fixing //example.com/c\r\n    <b><a href=\"https://example.com/c\">Click here</a><br><a href=\"https://example.com/c\">Click here</a></b>\r\n\r\nIn other words, .attr('href') returns a single value but .attr('href', '...') updates two different values. This alone is a significant issue for me. I would want either .attr('href') to return two values (which I think is how jquery does it) or .attr('href', '...') to only update a single value. But the interface needs to be consistent about the data model it's representing.\r\n\r\nThis is no longer an issue for me, at this time. I can't use cheerio for my project. (I would consider trying to find the cause of these issues, and fixing them, but it's not clear from the documentation what would be acceptable as fixes). But hopefully this perspective will be of use to someone else, at some point... \r\n\r\nThat said, note also that .next('[href]') does not find the second element with an href attribute. (But figuring out which way is up on that issue might depend how you want traversal to work for selectors.)"},"comment":{"url":"https://api.github.com/repos/cheeriojs/cheerio/issues/comments/431709353","html_url":"https://github.com/cheeriojs/cheerio/issues/1241#issuecomment-431709353","issue_url":"https://api.github.com/repos/cheeriojs/cheerio/issues/1241","id":431709353,"node_id":"MDEyOklzc3VlQ29tbWVudDQzMTcwOTM1Mw==","user":{"login":"meisl","id":1493886,"node_id":"MDQ6VXNlcjE0OTM4ODY=","avatar_url":"https://avatars3.githubusercontent.com/u/1493886?v=4","gravatar_id":"","url":"https://api.github.com/users/meisl","html_url":"https://github.com/meisl","followers_url":"https://api.github.com/users/meisl/followers","following_url":"https://api.github.com/users/meisl/following{/other_user}","gists_url":"https://api.github.com/users/meisl/gists{/gist_id}","starred_url":"https://api.github.com/users/meisl/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/meisl/subscriptions","organizations_url":"https://api.github.com/users/meisl/orgs","repos_url":"https://api.github.com/users/meisl/repos","events_url":"https://api.github.com/users/meisl/events{/privacy}","received_events_url":"https://api.github.com/users/meisl/received_events","type":"User","site_admin":false},"created_at":"2018-10-21T22:22:01Z","updated_at":"2018-10-21T22:22:01Z","author_association":"NONE","body":"First off, let me say that I'm new to cheerio. too; evaluating it since a few days only (but I do know jquery a bit better).\r\n\r\nAs for the **documentation**: it seems there have been - and *right now are* - problems with that. It's not reflecting what's in 1.0.0-rc.2, both missing some things and also describing some things wrong.\r\nHowever, that's not the case for .each. The snippet above is simply the source of 0.22.0 (and it's the same in master now).\r\n\r\nRe the **for-loop**: since you asked I have dug a bit deeper - and now I'm puzzled! That is: I found other problems which I cannot explain. It might just be me, but maybe there is (additionally) something wrong with cheerio's .next()...\r\n\r\nWell, I think we should first get a few things out of the way before we continue. Please let's be really precise with this, because it'll get tricky/strange enough afterwards:\r\n1) `el.next('[href]')` in `for (let el= $('a[href]'); el.length; el= el.next('[href]'))` is superfluous, could just have been `el.next()`, according to the [jquery docs](https://api.jquery.com/next) - do you agree?\r\n2) the actual output is NOT what you posted but rather\r\n   ```\r\n       2 'Click here' '//example.com'\r\n       fixing //example.com/c\r\n       <b><a href=\"https://example.com/c\">Click here</a><br><a href=\"https://example.com/c\">Click here</a></b>\r\n   ```\r\n   (you omitted the first line)\r\n2) your expected output would have been\r\n```\r\n       2 'Click here' '//example.com/c'\r\n       fixing //example.com/c\r\n       1 'Click here' 'other.com/d'\r\n       this looks ok: other.com/d\r\n       <b><a href=\"https://example.com/c\">Click here</a><br><a href=\"other.com/d\">Click here</a></b>\r\n   ```\r\n   **notice the second `<a>` - it's *other.com/d*, NOT *example.com/c* as in the actual output**\r\n3) having the *same* href-value in 2) - rather than different as in 3) - is due to calling .attr with *two args*\r\n\r\nAgain, please let's get these really clear first. Please confirm, or otherwise tell me what seems wrong."}}
{"actor":{"display_login":"meisl"}}