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

findParentRatio infinite recursion #3

Open
jbenet opened this issue Dec 11, 2012 · 0 comments
Open

findParentRatio infinite recursion #3

jbenet opened this issue Dec 11, 2012 · 0 comments

Comments

@jbenet
Copy link

jbenet commented Dec 11, 2012

The findParentRatio function:

function findParentRatio(jqObject) {
  var p = jqObject.parent(),
    displayType = p.css('display');

  if (displayType == 'block' || displayType == '-webkit-box' && p.width() > 0) {
    return { obj: p, width: p.width(), height: p.height(), ratio: (p.width() / p.height()) };
  } else {
    return findParentRatio(p);
  }
}

can cause infinite recursion (e.g. when the element is not in the DOM yet).

I suggest something like:

function findParentRatio(jqObject) {
  var p = jqObject.parent(),
    displayType = p.css('display');

  if (!p) {
    return a default or throw error here.
  }

  if (displayType == 'block' || displayType == '-webkit-box' && p.width() > 0) {
    return { obj: p, width: p.width(), height: p.height(), ratio: (p.width() / p.height()) };
  } else {
    return findParentRatio(p);
  }
}

Also, this approach doesn't mesh well with some MVC frameworks (like Backbone),
where views are rendered before they're added to the DOM.

Can't think of another way to do it at the moment though :(

sophiedeziel added a commit to sophiedeziel/jquery-object-fit that referenced this issue Jan 5, 2015
Checks if the object is in the dom to avoid an infinite loop.
Checks if the parent exists. If not, the ratio is not applied
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant