-
Notifications
You must be signed in to change notification settings - Fork 12.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
Remove jquery dependency #41307
Remove jquery dependency #41307
Conversation
67188bc
to
c20e8be
Compare
src/librustdoc/html/static/main.js
Outdated
} | ||
} | ||
|
||
function on_each(arr, func) { |
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.
Why not just use Array.prototype.forEach
?
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.
Simply because it seems that in a lot of cases, this method isn't available (for some reason?). So I wrote this one for all of them and we don't have to check if the method exists or not.
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 not available because many of the methods used, such as getElementsByClassName(), don't return an array, but an array-like object such as HTMLCollection, which indeed doesn't implement forEach(). :/
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.
Is it possible to use Array.from(arr).forEach()
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.
@isonmad Really depends on browsers you are targetting, but Array.from
is supported only by recent browseres, i.e. not IE. And if that's of no concern, you could also use for
/of
.
You could use forEach
here, although it is generally less performant:
Array.prototype.forEach.call(arr, func)
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 made this function exactly because at least every browser supports it. I don't see the point to try to add unneeded things over potential things.
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 also doesn't match the naming conventions of the surrounding functions
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.
onEach
it is now.
Hah, it failed because of what looks like just one tidy error: [00:02:37] tidy error: /checkout/src/librustdoc/html/static/main.js:580: line longer than 100 chars I did compare the output (not sure what you mean by navigation as any navigation should also necessarily be part of the output), and everything looks the same besides the commit hash and the removed jquery import, so that looks good. Looked through the docs manually, as well, and didn't see anything so far. |
c20e8be
to
2d9a5f1
Compare
Arf, didn't check tidy. Should be ok now. |
src/librustdoc/html/static/main.js
Outdated
@@ -65,18 +113,27 @@ | |||
from = parseInt(match[1], 10); | |||
to = Math.min(50000, parseInt(match[2] || match[1], 10)); | |||
from = Math.min(from, to); | |||
if ($('#' + from).length === 0) { | |||
if (document.getElementById(from).length === 0) { |
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.
getElementById returns null
if the element does not exist, so this will fail.
if (!document.getElementById(from))
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.
Excellent point!
src/librustdoc/html/static/main.js
Outdated
@@ -202,6 +202,8 @@ | |||
document.onkeypress = handleShortcut; | |||
document.onkeydown = handleShortcut; | |||
document.onclick = function(ev) { | |||
console.log(ev.target); |
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.
looks like some console.log
s snuck in here
I don't see the button to expand/collapse the description anymore. Also, the animations were removed? |
@euclio: I can add them back, but it'll be through CSS. |
src/librustdoc/html/static/main.js
Outdated
@@ -113,7 +113,8 @@ | |||
from = parseInt(match[1], 10); | |||
to = Math.min(50000, parseInt(match[2] || match[1], 10)); | |||
from = Math.min(from, to); | |||
if (document.getElementById(from).length === 0) { | |||
var elem = document.getElementById(from); | |||
if (!elem || elem.length === 0) { |
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.
Thanks for improving this!
However, check for elem.length === 0
is redundant, since getElementById
always returns at most a single element since there can be only one element with a given ID. There's no way you could get an array or NodeList of elements from this method. See https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById
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.
Once again, you're right! I remove it as well.
4bf9566
to
952036d
Compare
Looked through the code and through the docs. Looks good to me. |
You don't have to roll your own
|
@johansigfrids: Yes but this isn't supported by old web browsers so I can't use it. |
How old browsers do you really need to support? All browsers since 2011 support if. In fact, since IE9 hit end of life I don't think there are any supported (as in getting security patches) browsers that don't support |
That's not up to debate. Docs are supposed to be usable by the maximum number of people, whatever the web browser. It doesn't change much, it just adds a function. |
@GuillaumeGomez It very much is up to debate-- just like we have tier-1 platforms, we should also have tier-1 web browsers that we spend our time supporting. There are real costs to choosing to support arbitrarily old browsers. I'm not saying we shouldn't support old browsers, just that we should consider the tradeoffs of doing so. |
The change to support old browsers is 3 lines long, so I don't understand why there is a debate in here. :-/ |
952036d
to
f747b00
Compare
@alexcrichton that's a good assignment; we had a long discussion about this PR in the docs meeting yesterday, sort of a collective review at a high level :) |
I'm not sure I'm the best person to review this. I'm not really in favor of the removal personally unless we have thorough testing on all the browsers we support, which I don't have time to do. |
Also, I don't really care that we currently require jQuery. Its existence makes our JavaScript code cleaner and more backwards compatible. |
var click_func = function(e) { | ||
var el = e.target; | ||
// to retrieve the real "owner" of the event. | ||
while (el.tagName !== 'TR') { |
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.
What does this logic correspond to in the old code?
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.
Jquery allows to get the real owner of the event, whereas you have to find yourself normally. So if you bind something on an element, if it has children, then it's certainly one of the children which will "emit" the event.
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.
Did a quick skim through the code and it seems alright. Considering all the changes that are happening in rustdoc right now, it might be good to hold off on this for a bit until things have settled?
src/librustdoc/html/static/main.js
Outdated
link.href = '../' + crates[i] + '/index.html'; | ||
link.title = rawSearchIndex[crates[i]].doc; | ||
link.className = klass; | ||
link.innerHTML = crates[i]; |
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.
Would it be more appropriate to use textContent
here instead of innerHTML
?
src/librustdoc/html/static/main.js
Outdated
var div = document.createElement('div'); | ||
div.className = 'block ' + shortty; | ||
var h3 = document.createElement('h3'); | ||
h3.innerHTML = longty; |
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.
ditto textContents
src/librustdoc/html/static/main.js
Outdated
link.href = current.relpath + path; | ||
link.title = desc; | ||
link.className = klass; | ||
link.innerHTML = 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.
textContent
I said it on IRC that we should in any case wait for the new beta to land before merging this one. Thanks for your review! |
People seemed to agree on the fact that the removal was almost done so I squashed at this point. My bad for the bad understanding. |
30d9891
to
78137f4
Compare
Squashed and pushed (I replaced |
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.
Small js comment ...
Feel free not to take them into account!
src/librustdoc/html/static/main.js
Outdated
} | ||
} | ||
return false; | ||
} |
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 am really not a javascript guru, but wouldn't this function be faster and shorter (We probably don't really care but ...).
function hasClass(elem, className) {
if (elem && className && elem.className) {
return (" " + elem.className + " ").indexOf(" " + className + " ") != -1;
}
return false;
}
Or even faster but more code:
function hasClass(elem, className) {
if (elem && className && elem.className) {
var elemClass = elem.className;
var start = elemClass.indexOf(className);
if (start == -1) {
return false;
} else if (elemClass.length == className.length) {
return true;
} else {
if (start > 0 && elemClass[start - 1] != ' ') {
return false;
}
var end = start + className.length;
if (end < elemClass.length && elemClass[end] != ' ') {
return false;
}
return true;
}
return false;
}
}
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'll take the second one. Thanks!
src/librustdoc/html/static/main.js
Outdated
} | ||
elem.className = x.join(' '); | ||
} | ||
} |
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.
similarly this one:
function removeClass(elem, className) {
if (elem && className && elem.className) {
elem.className = (" " + elem.className + " ")
.replace(" " + className + " ", " ")
.trim();
}
}
78137f4
to
8c66bbb
Compare
Updated with @tafia's suggestions. |
8c66bbb
to
6f4c12e
Compare
I'm still not a big fan of the jQuery removal, but it sounds like people want to get rid of it. A lot of the logic introduced in this PR is pretty complicated compared to the existing code, which is unfortunate. I just skimmed through and nothing seemed glaringly wrong though. I also tried it out locally and didn't come across any issues in my manual testing. @steveklabnik Any reason to hold off on an r+? |
As long as we don't regress browsers badly (which I don't think we do),
then I'm fine with it.
…On Thu, May 4, 2017 at 3:19 PM, Corey Farwell ***@***.***> wrote:
I'm still not a big fan of the jQuery removal, but it sounds like people
want to get rid of it. A lot of the logic introduced in this PR is pretty
complicated compared to the existing code, which is unfortunate. I just
skimmed through and nothing seemed glaringly wrong though. I also tried it
out locally and didn't come across any issues in my manual testing.
@steveklabnik <https://github.com/steveklabnik> Any reason to hold off on
an r+?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#41307 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AABsir3wbZH9BITzD9P2TcpTwxIk8NIvks5r2iSsgaJpZM4M-LJP>
.
|
@bors r+ |
📌 Commit 6f4c12e has been approved by |
…ewsxcv Remove jquery dependency r? @rust-lang/docs Fixes rust-lang#39159.
⌛ Testing commit 6f4c12e with merge 42a4f37... |
☔ The latest upstream changes (presumably #41773) made this pull request unmergeable. Please resolve the merge conflicts. |
Fix regression introduced by jQuery removal Fixes rust-lang#42078. Follows rust-lang#41307. r? @steveklabnik cc @frewsxcv
Fix regression introduced by jQuery removal Fixes rust-lang#42078. Follows rust-lang#41307. r? @steveklabnik cc @frewsxcv
Fix regression introduced by jQuery removal Fixes rust-lang#42078. Follows rust-lang#41307. r? @steveklabnik cc @frewsxcv
Fix regression introduced by jQuery removal Fixes rust-lang#42078. Follows rust-lang#41307. r? @steveklabnik cc @frewsxcv
…meGomez rustdoc: Fix implementors list javascript * Use a different loop variable, `i` was already taken. This caused missing items in the implementors list. * Use `.getAttribute('href')` rather than `.href` to get the relative URL which is what it needs to actually fix the links. More fallout from rust-lang#41307. r? @GuillaumeGomez
r? @rust-lang/docs
Fixes #39159.