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
11 changes: 7 additions & 4 deletions packages/react-native/Libraries/StyleSheet/StyleSheetTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -807,10 +807,13 @@ type RadialGradientValue = {

export type BackgroundImageValue = LinearGradientValue | RadialGradientValue;

export type BackgroundSizeValue = {
x: string | number,
y: string | number,
};
export type BackgroundSizeValue =
| {
x: string | number,
y: string | number,
}
| 'cover'
| 'contain';

export type BackgroundRepeatKeyword =
'repeat' | 'space' | 'round' | 'no-repeat';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,56 @@ describe('processBackgroundSize', () => {
]);
});

it('should return empty array for unsupported cover keyword', () => {
expect(processBackgroundSize('cover')).toEqual([]);
// Test cover and contain keywords
it('should parse cover', () => {
expect(processBackgroundSize('cover')).toEqual(['cover']);
});

it('should return empty array for unsupported contain keyword', () => {
expect(processBackgroundSize('contain')).toEqual([]);
it('should parse contain', () => {
expect(processBackgroundSize('contain')).toEqual(['contain']);
});

it('should parse mixed case cover and contain', () => {
expect(processBackgroundSize(' COVER , Contain')).toEqual([
'cover',
'contain',
]);
});

it('should keep other layers next to cover and contain', () => {
expect(processBackgroundSize('10px auto, cover, 50%, contain')).toEqual([
{x: 10, y: 'auto'},
'cover',
{x: '50%', y: 'auto'},
'contain',
]);
});

it('should match the array form for cover', () => {
expect(processBackgroundSize('10px auto, cover')).toEqual(
processBackgroundSize([{x: 10, y: 'auto'}, 'cover']),
);
});

it('should handle array with cover and contain', () => {
expect(processBackgroundSize([{x: 100, y: 'auto'}, 'contain'])).toEqual([
{x: 100, y: 'auto'},
'contain',
]);
});

it('should reject cover or contain combined with a size', () => {
expect(processBackgroundSize('cover 10px')).toEqual([]);
expect(processBackgroundSize('10px contain')).toEqual([]);
expect(processBackgroundSize('auto cover')).toEqual([]);
});

it('should reject cover and contain in one layer', () => {
expect(processBackgroundSize('cover contain')).toEqual([]);
expect(processBackgroundSize('cover cover')).toEqual([]);
});

it('should reject every layer when a cover layer is invalid', () => {
expect(processBackgroundSize('10px auto, cover 10px')).toEqual([]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ function parseBackgroundSizeCSSString(
return [];
}
} else if (parts.length === 1) {
const x = getValidLengthPercentageSizeOrNull(parts[0].toLowerCase());
const part = parts[0].toLowerCase();
if (part === 'cover' || part === 'contain') {
result.push(part);
continue;
}
const x = getValidLengthPercentageSizeOrNull(part);
if (x != null) {
result.push({
x,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,8 @@ internal class BackgroundImageDrawable(
backgroundSize: BackgroundSize?,
repeat: BackgroundRepeat?,
): Pair<Float, Float> {
// Gradients have no natural size or aspect ratio, so `cover` and `contain` size them to the
// positioning area, which is the image size passed in.
var finalWidth = imageWidth
var finalHeight = imageHeight

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ internal class BackgroundSizeLengthPercentage(
/**
* Sealed class representing CSS background-size property values.
*
* This class models the different ways a background size can be specified in CSS, currently
* supporting length/percentage/auto values for both dimensions.
* This class models the different ways a background size can be specified in CSS: the `cover` and
* `contain` keywords, or length/percentage/auto values for both dimensions.
*
* @see BackgroundSizeLengthPercentage
*/
Expand All @@ -124,11 +124,17 @@ internal sealed class BackgroundSize {
class LengthPercentageAuto(val lengthPercentage: BackgroundSizeLengthPercentage) :
BackgroundSize()

/** Represents the `cover` keyword. */
object Cover : BackgroundSize()

/** Represents the `contain` keyword. */
object Contain : BackgroundSize()

companion object {
/**
* Parses a Dynamic value into a BackgroundSize.
*
* Currently supports map values containing x/y dimensions.
* Supports the `cover` and `contain` keyword strings, and map values containing x/y dimensions.
*
* @param backgroundSizeValue The dynamic value to parse
* @return A BackgroundSize instance, or null if parsing fails
Expand All @@ -146,6 +152,12 @@ internal sealed class BackgroundSize {
null
}
}
ReadableType.String ->
when (backgroundSizeValue.asString()) {
"cover" -> Cover
"contain" -> Contain
else -> null
}
else -> null
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.react.uimanager.style

import com.facebook.react.bridge.DynamicFromObject
import com.facebook.react.bridge.JavaOnlyMap
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

/** Tests for [BackgroundSize] */
@RunWith(RobolectricTestRunner::class)
class BackgroundSizeTest {
@Test
fun testParseCover() {
assertThat(BackgroundSize.parse(DynamicFromObject("cover"))).isSameAs(BackgroundSize.Cover)
}

@Test
fun testParseContain() {
assertThat(BackgroundSize.parse(DynamicFromObject("contain"))).isSameAs(BackgroundSize.Contain)
}

@Test
fun testParseUnknownKeyword() {
assertThat(BackgroundSize.parse(DynamicFromObject("fill"))).isNull()
}

@Test
fun testParseLengthPercentage() {
val size = BackgroundSize.parse(DynamicFromObject(JavaOnlyMap.of("x", 10.0, "y", "auto")))

assertThat(size).isInstanceOf(BackgroundSize.LengthPercentageAuto::class.java)
val lengthPercentage = (size as BackgroundSize.LengthPercentageAuto).lengthPercentage
assertThat(lengthPercentage.isXAuto()).isFalse()
assertThat(lengthPercentage.isYAuto()).isTrue()
}
}
Loading
Loading