aboutsummaryrefslogtreecommitdiff
path: root/js/controllers/styles-controller.js
diff options
context:
space:
mode:
authorValerio Virgillito2012-06-27 10:58:44 -0700
committerValerio Virgillito2012-06-27 10:58:44 -0700
commit913f5cdce07072e7ba6ba8063a3bd19e7eb00a46 (patch)
tree57e0d06f786d57946c11ab881faeef4d9b9a6f78 /js/controllers/styles-controller.js
parentd8bb1bf1537166753a53000e31ece8f99c7daaaa (diff)
parent2ebf3e3ea24d0d580575dfa13d31588dac1de445 (diff)
downloadninja-913f5cdce07072e7ba6ba8063a3bd19e7eb00a46.tar.gz
Merge branch 'refs/heads/master' into google-components-fixes
Diffstat (limited to 'js/controllers/styles-controller.js')
-rwxr-xr-xjs/controllers/styles-controller.js79
1 files changed, 51 insertions, 28 deletions
diff --git a/js/controllers/styles-controller.js b/js/controllers/styles-controller.js
index 1c1e75ed..0f847653 100755
--- a/js/controllers/styles-controller.js
+++ b/js/controllers/styles-controller.js
@@ -604,51 +604,74 @@ var stylesController = exports.StylesController = Montage.create(Component, {
604 604
605 getRuleIndex : { 605 getRuleIndex : {
606 value : function(rule) { 606 value : function(rule) {
607 var rules = nj.toArray(rule.parentStyleSheet.rules), 607 var rules = nj.toArray(rule.parentStyleSheet.rules);
608 i;
609 608
610 return rules.indexOf(rule); 609 return rules.indexOf(rule);
611 } 610 }
612 }, 611 },
613 612
613 _getRuleWithCSSText : {
614 value: function(cssText, doc) {
615 var _doc = doc || this.currentDocument.model.views.design._document,
616 ruleIndex, rule;
617
618 for(var i = 0; i < _doc.styleSheets.length; i++) {
619 ruleIndex = nj.toArray(_doc.styleSheets[i].rules).map(function(rule) {
620 return rule.cssText;
621 }).indexOf(cssText);
622
623 if(ruleIndex !== -1) {
624 rule = _doc.styleSheets[i].rules[ruleIndex];
625 break;
626 }
627 }
628
629 if(!rule) {
630 ///// This should never be hit if providing cssText from existing rule (like those
631 ///// returned from getMatchedCSSRules()
632 console.warn('StylesController::_getRuleWithCSSText - No rule found with given cssText.');
633 }
634
635 return rule;
636 }
637 },
638
614 ///// Get All Matching Rules 639 ///// Get All Matching Rules
615 ///// Returns an array of css rules for an element 640 ///// Returns an array of css rules for an element
616 ///// Optionally sorted by specificity, and can omit pseudo elements 641 ///// Optionally sorted by specificity, and can omit pseudo elements
617 642
618 getMatchingRules : { 643 getMatchingRules : { //TODO: Remove omitPseudos from here and usages
619 value: function(element, omitPseudos, useStageStyleSheet) { 644 value: function(element, omitPseudos, useStageStyleSheet) {
620 var pseudos = [null], 645 var rules,
621 rules = [], 646 mappedRules,
622 win = element.ownerDocument.defaultView, 647 doc = element.ownerDocument,
623 self = this; 648 win = doc.defaultView;
624
625 if(!omitPseudos) {
626 pseudos.concat(['link', 'visited', 'active', 'hover', 'focus', 'first-letter',
627 'first-line', 'first-child', 'before', 'after', 'lang', 'target']);
628 }
629 649
630 try { 650 try {
631 pseudos.forEach(function(pseudo) { 651 mappedRules = nj.toArray(win.getMatchedCSSRules(element)).map(function(rule) {
632 rules = rules.concat(nj.toArray(win.getMatchedCSSRules(element, pseudo)).filter(function(rule) { 652 return this._getRuleWithCSSText(rule.cssText, doc);
633 //// useStageStyleSheet flag indicates whether to only return rules from the stylesheet, 653 }, this);
634 //// or only use rules for other stylesheets
635 654
636 var sheetId = (rule.parentStyleSheet) ? rule.parentStyleSheet.ownerNode.id : null, 655 rules = mappedRules.filter(function(rule) {
637 isStageStyleSheet = sheetId === this.CONST.STAGE_SHEET_ID; 656 //// useStageStyleSheet flag indicates whether to only return rules from the stylesheet,
657 //// or only use rules for other stylesheets
638 658
639 ///// filter out (return false) depending on flag 659 var sheetId = (rule.parentStyleSheet) ? rule.parentStyleSheet.ownerNode.id : null,
640 if(useStageStyleSheet && !isStageStyleSheet) { return false; } 660 isStageStyleSheet = sheetId === this.CONST.STAGE_SHEET_ID;
641 if(!useStageStyleSheet && isStageStyleSheet) { return false; }
642 661
643 ///// Non-filter code - just assigning specificity to the rule 662 ///// filter out (return false) depending on flag
644 if(!rule[this.CONST.SPECIFICITY_KEY]) { 663 if(useStageStyleSheet && !isStageStyleSheet) { return false; }
645 rule[this.CONST.SPECIFICITY_KEY] = this.getSpecificity(rule.selectorText); 664 if(!useStageStyleSheet && isStageStyleSheet) { return false; }
646 }
647 665
648 return true; 666 ///// Non-filter code - just assigning specificity to the rule
667 if(!rule[this.CONST.SPECIFICITY_KEY]) {
668 rule[this.CONST.SPECIFICITY_KEY] = this.getSpecificity(rule.selectorText);
669 }
670
671 return true;
649 672
650 }, this));
651 }, this); 673 }, this);
674
652 } catch(ERROR) { 675 } catch(ERROR) {
653 console.warn('StylesController::getMatchingRules - Un-attached element queried.'); 676 console.warn('StylesController::getMatchingRules - Un-attached element queried.');
654 } 677 }