|
| 1 | +let createExpectedPropertiesOrder = require('../createExpectedPropertiesOrder'); |
| 2 | +let getComments = require('../getComments'); |
| 3 | +let getPropertiesOrderData = require('../getPropertiesOrderData'); |
| 4 | +let isCustomProperty = require('../isCustomProperty'); |
| 5 | +let isRuleWithNodes = require('../isRuleWithNodes'); |
| 6 | +let isStandardSyntaxProperty = require('../isStandardSyntaxProperty'); |
| 7 | +let sorting = require('../sorting'); |
| 8 | +let vendor = require('../vendor'); |
| 9 | + |
| 10 | +module.exports = function sortNodeProperties(node, { order, unspecifiedPropertiesPosition }) { |
| 11 | + if (!isRuleWithNodes(node)) { |
| 12 | + return; |
| 13 | + } |
| 14 | + |
| 15 | + let isAlphabetical = order === 'alphabetical'; |
| 16 | + let expectedOrder = isAlphabetical ? null : createExpectedPropertiesOrder(order); |
| 17 | + |
| 18 | + let allRuleNodes = []; |
| 19 | + let declarations = []; |
| 20 | + |
| 21 | + node.each((childNode, index) => { |
| 22 | + if ( |
| 23 | + childNode.type === 'decl' && |
| 24 | + isStandardSyntaxProperty(childNode.prop) && |
| 25 | + !isCustomProperty(childNode.prop) |
| 26 | + ) { |
| 27 | + let unprefixedPropName = vendor.unprefixed(childNode.prop); |
| 28 | + |
| 29 | + // Hack to allow -moz-osx-font-smoothing to be understood |
| 30 | + // just like -webkit-font-smoothing |
| 31 | + if (unprefixedPropName.indexOf('osx-') === 0) { |
| 32 | + unprefixedPropName = unprefixedPropName.slice(4); |
| 33 | + } |
| 34 | + |
| 35 | + let propData = { |
| 36 | + name: childNode.prop, |
| 37 | + unprefixedName: unprefixedPropName, |
| 38 | + orderData: isAlphabetical |
| 39 | + ? null |
| 40 | + : getPropertiesOrderData(expectedOrder, unprefixedPropName), |
| 41 | + node: childNode, |
| 42 | + initialIndex: index, |
| 43 | + unspecifiedPropertiesPosition, |
| 44 | + }; |
| 45 | + |
| 46 | + // add a marker |
| 47 | + childNode.sortProperty = true; |
| 48 | + |
| 49 | + // If comment on separate line before node, use node's indexes for comment |
| 50 | + let commentsBefore = getComments.beforeDeclaration([], childNode.prev(), propData); |
| 51 | + |
| 52 | + // If comment on same line with the node and node, use node's indexes for comment |
| 53 | + let commentsAfter = getComments.afterDeclaration([], childNode.next(), propData); |
| 54 | + |
| 55 | + declarations = declarations.concat(commentsBefore, propData, commentsAfter); |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + if (isAlphabetical) { |
| 60 | + declarations.sort(sorting.sortDeclarationsAlphabetically); |
| 61 | + } else { |
| 62 | + declarations.sort(sorting.sortDeclarations); |
| 63 | + } |
| 64 | + |
| 65 | + let foundDeclarations = false; |
| 66 | + |
| 67 | + node.each((childNode) => { |
| 68 | + if (childNode.sortProperty) { |
| 69 | + if (!foundDeclarations) { |
| 70 | + foundDeclarations = true; |
| 71 | + |
| 72 | + declarations.forEach((item) => { |
| 73 | + allRuleNodes.push(item.node); |
| 74 | + }); |
| 75 | + } |
| 76 | + } else { |
| 77 | + allRuleNodes.push(childNode); |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + node.removeAll(); |
| 82 | + node.append(allRuleNodes); |
| 83 | +}; |
0 commit comments