|
27 | 27 | // convert an array-like object to array
|
28 | 28 | var toArray = function (list) {
|
29 | 29 | var items = [];
|
30 |
| - var i; |
| 30 | + var i = 0; |
| 31 | + var listLength = list.length; |
31 | 32 |
|
32 |
| - for (i in list) { |
| 33 | + for (; i < listLength; i++) { |
33 | 34 | items.push(list[i]);
|
34 | 35 | }
|
35 | 36 |
|
|
39 | 40 | // get host of stylesheet
|
40 | 41 | var getCSSHost = function (href) {
|
41 | 42 | var fakeLinkOfSheet = document.createElement('a');
|
| 43 | + |
42 | 44 | fakeLinkOfSheet.href = href;
|
| 45 | + |
43 | 46 | return fakeLinkOfSheet.host;
|
44 | 47 | };
|
45 | 48 |
|
|
148 | 151 | return rules.sort(compareSpecificity);
|
149 | 152 | };
|
150 | 153 |
|
151 |
| - // Find correct matchesSelector implementation |
152 |
| - var _matchesSelector = function (element, selector) { |
153 |
| - if (!element.matches || !element.matchesSelector || !element.webkitMatchesSelector || !element.mozMatchesSelector || !element.msMatchesSelector) { |
154 |
| - var matches = (element.document || element.ownerDocument).querySelectorAll(selector); |
155 |
| - var i = 0; |
| 154 | + var customMatchesSelector = function (element, selector) { |
| 155 | + var matches = (element.document || element.ownerDocument).querySelectorAll(selector); |
| 156 | + var i = 0; |
156 | 157 |
|
157 |
| - while (matches[i] && matches[i] !== element) { |
158 |
| - i++; |
159 |
| - } |
160 |
| - |
161 |
| - matches[i] ? true : false; |
| 158 | + while (matches[i] && matches[i] !== element) { |
| 159 | + i++; |
162 | 160 | }
|
163 | 161 |
|
| 162 | + return matches[i] ? true : false; |
| 163 | + }; |
| 164 | + |
| 165 | + // Find correct matchesSelector implementation |
| 166 | + var _matchesSelector = function (element, selector) { |
164 | 167 | var matcher = function (selector) {
|
165 | 168 | if (element.matches) {
|
166 | 169 | return element.matches(selector);
|
|
172 | 175 | return element.webkitMatchesSelector(selector);
|
173 | 176 | } else if (element.msMatchesSelector) {
|
174 | 177 | return element.msMatchesSelector(selector);
|
| 178 | + } else { |
| 179 | + return customMatchesSelector(element, selector); |
175 | 180 | }
|
176 | 181 | };
|
177 | 182 |
|
|
310 | 315 |
|
311 | 316 | var defaultElement = iframe.contentWindow.document.querySelectorAll(element.nodeName.toLowerCase())[0];
|
312 | 317 | var defaultComputedStyle = this.getComputedStyle(defaultElement, iframe.contentWindow);
|
| 318 | + var value; |
| 319 | + var property; |
| 320 | + |
| 321 | + for (property in defaultComputedStyle) { |
| 322 | + if (defaultComputedStyle.getPropertyValue === true) { |
| 323 | + value = defaultComputedStyle.getPropertyValue(property); |
| 324 | + } else { |
| 325 | + value = defaultComputedStyle[property]; |
| 326 | + } |
313 | 327 |
|
314 |
| - for (var property in defaultComputedStyle) { |
315 |
| - var value = defaultComputedStyle.getPropertyValue ? defaultComputedStyle.getPropertyValue(property) : defaultComputedStyle[property]; |
316 | 328 | if (value !== null) {
|
317 | 329 | switch (property) {
|
318 | 330 | default:
|
|
348 | 360 |
|
349 | 361 | // get matched rules
|
350 | 362 | var rules = window.getMatchedCSSRules(element);
|
| 363 | + var i = rules.length; |
| 364 | + var r; |
| 365 | + var important; |
351 | 366 |
|
352 |
| - if (rules.length) { |
| 367 | + if (i) { |
353 | 368 | // iterate the rules backwards
|
354 | 369 | // rules are ordered by priority, highest last
|
355 |
| - for (var i = rules.length; i --> 0;){ |
356 |
| - var r = rules[i]; |
357 |
| - |
358 |
| - var important = r.style.getPropertyPriority(property); |
| 370 | + for (; i --> 0;) { |
| 371 | + r = rules[i]; |
| 372 | + important = r.style.getPropertyPriority(property); |
359 | 373 |
|
360 | 374 | // if set, only reset if important
|
361 | 375 | if (val === null || important) {
|
|
365 | 379 | if (important) {
|
366 | 380 | break;
|
367 | 381 | }
|
368 |
| - //return val; |
369 | 382 | }
|
370 | 383 | }
|
371 | 384 | }
|
|
390 | 403 | if (replacedElement.getAttribute('data-x-object-relation') !== 'wider') {
|
391 | 404 | replacedElement.setAttribute('data-x-object-relation','wider');
|
392 | 405 | replacedElement.className = 'x-object-fit-wider';
|
| 406 | + |
393 | 407 | if (this._debug && window.console) {
|
394 | 408 | console.log('x-object-fit-wider');
|
395 | 409 | }
|
|
398 | 412 | if (replacedElement.getAttribute('data-x-object-relation') !== 'taller') {
|
399 | 413 | replacedElement.setAttribute('data-x-object-relation','taller');
|
400 | 414 | replacedElement.className = 'x-object-fit-taller';
|
| 415 | + |
401 | 416 | if (this._debug && window.console) {
|
402 | 417 | console.log('x-object-fit-taller');
|
403 | 418 | }
|
|
441 | 456 | };
|
442 | 457 |
|
443 | 458 | objectFit.processElement = function(replacedElement, args) {
|
444 |
| - var property, value; |
445 |
| - |
| 459 | + var property; |
| 460 | + var value; |
446 | 461 | var replacedElementStyles = objectFit.getComputedStyle(replacedElement);
|
447 | 462 | var replacedElementDefaultStyles = objectFit.getDefaultComputedStyle(replacedElement);
|
448 |
| - |
449 | 463 | var wrapperElement = document.createElement('x-object-fit');
|
450 | 464 |
|
451 | 465 | if (objectFit._debug && window.console) {
|
452 | 466 | console.log('Applying to WRAPPER-------------------------------------------------------');
|
453 | 467 | }
|
| 468 | + |
454 | 469 | for (property in replacedElementStyles) {
|
455 | 470 | switch (property) {
|
456 | 471 | default:
|
457 |
| - value = objectFit.getMatchedStyle(replacedElement,property); |
| 472 | + value = objectFit.getMatchedStyle(replacedElement, property); |
| 473 | + |
458 | 474 | if (value !== null && value !== '') {
|
459 | 475 | if (objectFit._debug && window.console) {
|
460 | 476 | console.log(property + ': ' + value);
|
461 | 477 | }
|
| 478 | + |
462 | 479 | wrapperElement.style[property] = value;
|
463 | 480 | }
|
464 | 481 | break;
|
|
484 | 501 | console.log('Indexed style properties (`' + property + '`) not supported in: ' + window.navigator.userAgent);
|
485 | 502 | }
|
486 | 503 | }
|
| 504 | + |
487 | 505 | if (replacedElement.style[property]) {
|
488 | 506 | replacedElement.style[property] = value; // should work in Firefox 35+ and all other browsers
|
489 | 507 | } else {
|
|
504 | 522 | objectFit.orientation(replacedElement);
|
505 | 523 |
|
506 | 524 | var resizeTimer = null;
|
507 |
| - var resizeAction = function(){ |
| 525 | + var resizeAction = function () { |
508 | 526 | if (resizeTimer !== null) {
|
509 | 527 | window.cancelAnimationFrame(resizeTimer);
|
510 | 528 | }
|
|
531 | 549 | }
|
532 | 550 | };
|
533 | 551 |
|
534 |
| - objectFit.listen = function(args) { |
535 |
| - var domInsertedAction = function(element){ |
536 |
| - for (var i = 0, argsLength = args.length; i < argsLength; i++) { |
| 552 | + objectFit.listen = function (args) { |
| 553 | + var domInsertedAction = function (element){ |
| 554 | + var i = 0; |
| 555 | + var argsLength = args.length; |
| 556 | + |
| 557 | + for (; i < argsLength; i++) { |
537 | 558 | if ((element.mozMatchesSelector && element.mozMatchesSelector(args[i].selector)) ||
|
538 | 559 | (element.msMatchesSelector && element.msMatchesSelector(args[i].selector)) ||
|
539 | 560 | (element.oMatchesSelector && element.oMatchesSelector(args[i].selector)) ||
|
540 | 561 | (element.webkitMatchesSelector && element.webkitMatchesSelector(args[i].selector))
|
541 | 562 | ) {
|
542 | 563 | args[i].replacedElements = [element];
|
543 | 564 | objectFit.process(args[i]);
|
| 565 | + |
544 | 566 | if (objectFit._debug && window.console) {
|
545 | 567 | console.log('Matching node inserted: ' + element.nodeName);
|
546 | 568 | }
|
547 | 569 | }
|
548 | 570 | }
|
549 | 571 | };
|
550 | 572 |
|
551 |
| - var domInsertedObserverFunction = function(element){ |
| 573 | + var domInsertedObserverFunction = function (element) { |
552 | 574 | objectFit.observer.disconnect();
|
553 | 575 | domInsertedAction(element);
|
554 | 576 | objectFit.observer.observe(document.documentElement, {
|
|
557 | 579 | });
|
558 | 580 | };
|
559 | 581 |
|
560 |
| - var domInsertedEventFunction = function(event){ |
| 582 | + var domInsertedEventFunction = function (event) { |
561 | 583 | window.removeEventListener('DOMNodeInserted', domInsertedEventFunction, false);
|
562 | 584 | domInsertedAction(event.target);
|
563 | 585 | window.addEventListener('DOMNodeInserted', domInsertedEventFunction, false);
|
564 | 586 | };
|
565 | 587 |
|
566 |
| - var domRemovedAction = function(element){ |
| 588 | + var domRemovedAction = function (element) { |
567 | 589 | if (element.nodeName.toLowerCase() === 'x-object-fit') {
|
568 | 590 | element.parentNode.removeChild(element);
|
| 591 | + |
569 | 592 | if (objectFit._debug && window.console) {
|
570 | 593 | console.log('Matching node removed: ' + element.nodeName);
|
571 | 594 | }
|
572 | 595 | }
|
573 | 596 | };
|
574 | 597 |
|
575 |
| - var domRemovedObserverFunction = function(element){ |
| 598 | + var domRemovedObserverFunction = function (element) { |
576 | 599 | objectFit.observer.disconnect();
|
577 | 600 | domRemovedAction(element);
|
578 | 601 | objectFit.observer.observe(document.documentElement, {
|
|
581 | 604 | });
|
582 | 605 | };
|
583 | 606 |
|
584 |
| - var domRemovedEventFunction = function(event){ |
| 607 | + var domRemovedEventFunction = function (event) { |
585 | 608 | window.removeEventListener('DOMNodeRemoved', domRemovedEventFunction, false);
|
586 | 609 | domRemovedAction(event.target.parentNode);
|
587 | 610 | window.addEventListener('DOMNodeRemoved', domRemovedEventFunction, false);
|
|
591 | 614 | if (objectFit._debug && window.console) {
|
592 | 615 | console.log('DOM MutationObserver');
|
593 | 616 | }
|
| 617 | + |
594 | 618 | this.observer = new MutationObserver(function(mutations) {
|
595 | 619 | mutations.forEach(function(mutation) {
|
596 | 620 | if (mutation.addedNodes && mutation.addedNodes.length) {
|
|
604 | 628 | }
|
605 | 629 | });
|
606 | 630 | });
|
| 631 | + |
607 | 632 | this.observer.observe(document.documentElement, {
|
608 | 633 | childList: true,
|
609 | 634 | subtree: true
|
|
612 | 637 | if (objectFit._debug && window.console) {
|
613 | 638 | console.log('DOM Mutation Events');
|
614 | 639 | }
|
| 640 | + |
615 | 641 | window.addEventListener('DOMNodeInserted', domInsertedEventFunction, false);
|
616 | 642 | window.addEventListener('DOMNodeRemoved', domRemovedEventFunction, false);
|
617 | 643 | }
|
618 | 644 | };
|
619 | 645 |
|
620 |
| - objectFit.init = function(args) { |
| 646 | + objectFit.init = function (args) { |
621 | 647 | if (!args) {
|
622 | 648 | return;
|
623 | 649 | }
|
| 650 | + |
624 | 651 | if (!(args instanceof Array)) {
|
625 | 652 | args = [args];
|
626 | 653 | }
|
627 | 654 |
|
628 |
| - for (var i = 0, argsLength = args.length; i < argsLength; i++) { |
| 655 | + var i = 0; |
| 656 | + var argsLength = args.length; |
| 657 | + |
| 658 | + for (; i < argsLength; i++) { |
629 | 659 | args[i].replacedElements = document.querySelectorAll(args[i].selector);
|
630 | 660 | this.process(args[i]);
|
631 | 661 | }
|
632 | 662 |
|
633 | 663 | this.listen(args);
|
634 | 664 | };
|
635 | 665 |
|
636 |
| - objectFit.polyfill = function(args) { |
| 666 | + objectFit.polyfill = function (args) { |
637 | 667 | if('objectFit' in document.documentElement.style === false) {
|
638 | 668 | if (objectFit._debug && window.console) {
|
639 | 669 | console.log('object-fit not natively supported');
|
640 | 670 | }
|
| 671 | + |
641 | 672 | // If the library is loaded after document onload event
|
642 | 673 | if (document.readyState === 'complete') {
|
643 | 674 | objectFit.init(args);
|
|
0 commit comments