Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions lib/hacks/mask-composite.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,51 @@
let Browsers = require('../browsers')
let Declaration = require('../declaration')
let vendor = require('../vendor')

class MaskComposite extends Declaration {
/**

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This comment is not very useful. We need to explain for (let step of [-1, 1]) { and other complex part not explaining things obvious from isAlready

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Rewritten in c29be60. The comment now shows the rule that goes wrong (-webkit-mask, -webkit-mask-composite, mask) and why the default check stops early. The loop over [-1, 1] is gone. Each direction is a call to a small groupHas(decl, prefixed, direction) with its own comment, and the stop conditions sit in endsGroup(), each explained.

* Autoprefixer skips a declaration when its prefixed copy is already in the
* rule. The default check only looks at neighbours with the same unprefixed
* name, so in
*
* -webkit-mask: ...;
* -webkit-mask-composite: ...;
* mask: ...;
*
* it stops at -webkit-mask-composite and adds a second -webkit-mask.
* Here mask and mask-composite count as one group.
*/
isAlready(decl, prefixed) {
return this.groupHas(decl, prefixed, -1) || this.groupHas(decl, prefixed, 1)
}

/**
* Walk from decl up (direction -1) or down (direction 1) and tell whether
* the prefixed property comes before the group ends.
*/
groupHas(decl, prefixed, direction) {
let nodes = decl.parent.nodes
for (let i = nodes.indexOf(decl) + direction; nodes[i]; i += direction) {
let other = nodes[i]
if (other.type !== 'decl') continue
if (other.prop === prefixed) return true
if (this.endsGroup(decl, other)) return false
}
return false
}

/**
* The group ends at a property other than mask or mask-composite, at a
* copy this hack added (it is not the user's own), and at the next
* unprefixed declaration of the same property, like the default check.
*/
endsGroup(decl, other) {
let name = vendor.unprefixed(other.prop)
if (!MaskComposite.names.includes(name)) return true
if (other._autoprefixerMaskComposite) return true
return other.prop === decl.prop && !Browsers.withPrefix(other.value)
}

/**
* Prefix mask-composite for webkit
*/
Expand Down Expand Up @@ -65,6 +110,8 @@ class MaskComposite extends Declaration {
if (this.needCascade(decl)) {
compositeDecl.raws.before = this.calcBefore(prefixes, decl, prefix)
}
// A later mask-composite must not take this copy for the user's own
decl._autoprefixerMaskComposite = true
return decl.parent.insertBefore(decl, compositeDecl)
}
}
Expand Down
15 changes: 14 additions & 1 deletion test/cases/mask-composite.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,17 @@ a {

a {
mask-composite: add, subtract, exclude;
}
}

a {
-webkit-mask: url(full.png) padding-box, url(full.png);
-webkit-mask-composite: xor;
mask: url(full.png) padding-box exclude, url(full.png);
}

a {
-webkit-mask: url(full.png);
-webkit-mask-composite: xor;
mask: url(full.png);
mask-composite: exclude;
}
15 changes: 14 additions & 1 deletion test/cases/mask-composite.out.css
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,17 @@ a {
a {
-webkit-mask-composite: source-over, source-out, xor;
mask-composite: add, subtract, exclude;
}
}

a {
-webkit-mask: url(full.png) padding-box, url(full.png);
-webkit-mask-composite: xor;
mask: url(full.png) padding-box exclude, url(full.png);
}

a {
-webkit-mask: url(full.png);
-webkit-mask-composite: xor;
mask: url(full.png);
mask-composite: exclude;
}