Skip to content
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

Merged
merged 1 commit into from
May 6, 2017
Merged

Conversation

GuillaumeGomez
Copy link
Member

@GuillaumeGomez GuillaumeGomez commented Apr 14, 2017

r? @rust-lang/docs

Fixes #39159.

@GuillaumeGomez GuillaumeGomez force-pushed the jquery-removal branch 2 times, most recently from 67188bc to c20e8be Compare April 15, 2017 00:07
}
}

function on_each(arr, func) {
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Contributor

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(). :/

Copy link

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?

Copy link

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)

Copy link
Member Author

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.

Copy link
Member

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onEach it is now.

@projektir
Copy link
Contributor

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.

@GuillaumeGomez
Copy link
Member Author

Arf, didn't check tidy. Should be ok now.

@@ -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) {
Copy link

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))

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent point!

@@ -202,6 +202,8 @@
document.onkeypress = handleShortcut;
document.onkeydown = handleShortcut;
document.onclick = function(ev) {
console.log(ev.target);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like some console.logs snuck in here

@euclio
Copy link
Contributor

euclio commented Apr 15, 2017

I don't see the button to expand/collapse the description anymore. Also, the animations were removed?

@arielb1 arielb1 added A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 15, 2017
@GuillaumeGomez
Copy link
Member Author

@euclio: I can add them back, but it'll be through CSS.

@@ -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) {
Copy link

@jnv jnv Apr 15, 2017

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

Copy link
Member Author

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.

@projektir
Copy link
Contributor

Looked through the code and through the docs. Looks good to me.

@johansigfrids
Copy link

You don't have to roll your own hasClass, addClass, and removeClass. The classList property on Dom elements has methods for that:

  • elem.classList.contains('class')
  • elem.classList.add('class')
  • elem.classList.remove('class')

@GuillaumeGomez
Copy link
Member Author

@johansigfrids: Yes but this isn't supported by old web browsers so I can't use it.

@johansigfrids
Copy link

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 classList.

@GuillaumeGomez
Copy link
Member Author

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.

@carols10cents
Copy link
Member

That's not up to debate. Docs are supposed to be usable by the maximum number of people, whatever the web browser.

@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.

@GuillaumeGomez
Copy link
Member Author

The change to support old browsers is 3 lines long, so I don't understand why there is a debate in here. :-/

@alexcrichton
Copy link
Member

Randomly assigning @frewsxcv from docs team, mind taking a look and reviewing @frewsxcv?

@steveklabnik
Copy link
Member

@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 :)

@frewsxcv
Copy link
Member

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.

@frewsxcv
Copy link
Member

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') {
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

@frewsxcv frewsxcv left a 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?

link.href = '../' + crates[i] + '/index.html';
link.title = rawSearchIndex[crates[i]].doc;
link.className = klass;
link.innerHTML = crates[i];
Copy link
Member

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?

var div = document.createElement('div');
div.className = 'block ' + shortty;
var h3 = document.createElement('h3');
h3.innerHTML = longty;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto textContents

link.href = current.relpath + path;
link.title = desc;
link.className = klass;
link.innerHTML = name;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

textContent

@GuillaumeGomez
Copy link
Member Author

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?

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!

@GuillaumeGomez
Copy link
Member Author

it'd be helpful if you didn't squash your commits for now so we can easily see the changes you're making

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.

@GuillaumeGomez
Copy link
Member Author

Squashed and pushed (I replaced innerHTML with textContent).

@GuillaumeGomez GuillaumeGomez added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 1, 2017
Copy link
Contributor

@tafia tafia left a 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!

}
}
return false;
}
Copy link
Contributor

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;  
  }
}

Copy link
Member Author

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!

}
elem.className = x.join(' ');
}
}
Copy link
Contributor

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();
  }
}

@GuillaumeGomez
Copy link
Member Author

Updated with @tafia's suggestions.

@frewsxcv
Copy link
Member

frewsxcv commented May 4, 2017

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+?

@steveklabnik
Copy link
Member

steveklabnik commented May 5, 2017 via email

@frewsxcv
Copy link
Member

frewsxcv commented May 5, 2017

@bors r+

@bors
Copy link
Contributor

bors commented May 5, 2017

📌 Commit 6f4c12e has been approved by frewsxcv

frewsxcv added a commit to frewsxcv/rust that referenced this pull request May 5, 2017
…ewsxcv

Remove jquery dependency

r? @rust-lang/docs

Fixes rust-lang#39159.
bors added a commit that referenced this pull request May 5, 2017
Rollup of 9 pull requests

- Successful merges: #41064, #41307, #41512, #41582, #41678, #41722, #41734, #41761, #41763
- Failed merges:
@bors bors merged commit 6f4c12e into rust-lang:master May 6, 2017
@bors
Copy link
Contributor

bors commented May 6, 2017

⌛ Testing commit 6f4c12e with merge 42a4f37...

@bors
Copy link
Contributor

bors commented May 6, 2017

☔ The latest upstream changes (presumably #41773) made this pull request unmergeable. Please resolve the merge conflicts.

@GuillaumeGomez GuillaumeGomez deleted the jquery-removal branch May 6, 2017 11:21
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this pull request May 18, 2017
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this pull request May 18, 2017
frewsxcv added a commit to frewsxcv/rust that referenced this pull request May 18, 2017
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this pull request May 19, 2017
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this pull request May 19, 2017
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.