Skip to content

Commit 5151f22

Browse files
committed
address gradient-fill review feedback: recursive fill-stripping, input validation, and prop validator
1 parent 8508b5e commit 5151f22

2 files changed

Lines changed: 21 additions & 9 deletions

File tree

‎src/components/FontAwesomeIcon.js‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,18 @@ export default defineComponent({
156156
},
157157
gradientFill: {
158158
type: Object,
159-
default: null
159+
default: null,
160+
validator (value) {
161+
if (typeof value.id !== 'string' || !value.id) {
162+
console.warn('FontAwesomeIcon: gradientFill.id must be a non-empty string')
163+
return false
164+
}
165+
if (value.type !== 'linear' && value.type !== 'radial') {
166+
console.warn('FontAwesomeIcon: gradientFill.type must be "linear" or "radial"')
167+
return false
168+
}
169+
return true
170+
}
160171
}
161172
},
162173

‎src/converter.js‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,25 @@ function createGradientStop (stop, index) {
6363
* @param {Object} props The user-defined props.
6464
* @param {Object} attrs The user-defined native HTML attributes.
6565
* @param {Object} [gradientFill] Optional gradient fill config.
66+
* @param {boolean} [stripFills] Whether to strip fill from descendant path elements.
6667
* @returns {VNode}
6768
*/
68-
export default function convert (abstractElement, props = {}, attrs = {}, gradientFill = null) {
69+
export default function convert (abstractElement, props = {}, attrs = {}, gradientFill = null, stripFills = false) {
6970
// If the abstract element is a string, we'll just return a string render function
7071
if (typeof abstractElement === 'string') {
7172
return abstractElement
7273
}
7374

7475
// Converting abstract element children into Vue VNodes
75-
// If a gradientFill (or fill attr) is provided, strip fill from child path elements
76+
// If a gradientFill (or fill attr) is provided, strip fill from all descendant path elements
7677
// so the gradient/fill takes precedence over the icon's built-in fill
77-
const hasFillOverride = gradientFill || ('fill' in attrs)
78+
const shouldStripFills = stripFills || !!gradientFill || ('fill' in attrs)
7879
const children = (abstractElement.children || [])
7980
.map(child => {
80-
if (hasFillOverride && child.tag === 'path' && child.attributes && 'fill' in child.attributes) {
81-
return convert({ ...child, attributes: { ...child.attributes, fill: undefined } })
81+
if (shouldStripFills && child.tag === 'path' && child.attributes && 'fill' in child.attributes) {
82+
return convert({ ...child, attributes: { ...child.attributes, fill: undefined } }, {}, {}, null, true)
8283
}
83-
return convert(child)
84+
return convert(child, {}, {}, null, shouldStripFills)
8485
})
8586

8687
// Converting abstract element attributes into valid Vue format
@@ -112,8 +113,8 @@ export default function convert (abstractElement, props = {}, attrs = {}, gradie
112113
// Now, we'll return the VNode
113114
const { class: _aClass = {}, style: aStyle = {}, ...otherAttrs } = attrs
114115

115-
// If a gradientFill is provided, inject the gradient element and set fill to reference it
116-
if (gradientFill) {
116+
// If a valid gradientFill is provided, inject the gradient element and set fill to reference it
117+
if (gradientFill && gradientFill.id && (gradientFill.type === 'linear' || gradientFill.type === 'radial')) {
117118
const { type: gradientType, stops = [], id, ...gradientProps } = gradientFill
118119
const gradientTag = gradientType === 'linear' ? 'linearGradient' : 'radialGradient'
119120
const gradientVNode = h(gradientTag, { ...gradientProps, id }, stops.map(createGradientStop))

0 commit comments

Comments
 (0)