Skip to content

Commit c6a392a

Browse files
Add CssBackgroundAttachment type to spark_css
- Implemented `CssBackgroundAttachment` sealed class. - Added support for keywords: `scroll`, `fixed`, `local`. - Added `multiple` factory for comma-separated values. - Added `variable`, `raw`, and `global` factories. - Added comprehensive tests.
1 parent 7e08f3a commit c6a392a

2 files changed

Lines changed: 145 additions & 0 deletions

File tree

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import 'css_value.dart';
2+
3+
/// CSS background-attachment property values.
4+
sealed class CssBackgroundAttachment implements CssValue {
5+
const CssBackgroundAttachment._();
6+
7+
// Keyword values
8+
static const CssBackgroundAttachment scroll = _CssBackgroundAttachmentKeyword(
9+
'scroll',
10+
);
11+
static const CssBackgroundAttachment fixed = _CssBackgroundAttachmentKeyword(
12+
'fixed',
13+
);
14+
static const CssBackgroundAttachment local = _CssBackgroundAttachmentKeyword(
15+
'local',
16+
);
17+
18+
/// Multiple background attachments.
19+
factory CssBackgroundAttachment.multiple(
20+
List<CssBackgroundAttachment> attachments,
21+
) = _CssBackgroundAttachmentMultiple;
22+
23+
/// CSS variable reference.
24+
factory CssBackgroundAttachment.variable(String varName) =
25+
_CssBackgroundAttachmentVariable;
26+
27+
/// Raw CSS value escape hatch.
28+
factory CssBackgroundAttachment.raw(String value) =
29+
_CssBackgroundAttachmentRaw;
30+
31+
/// Global keyword (inherit, initial, unset, revert).
32+
factory CssBackgroundAttachment.global(CssGlobal global) =
33+
_CssBackgroundAttachmentGlobal;
34+
}
35+
36+
final class _CssBackgroundAttachmentKeyword extends CssBackgroundAttachment {
37+
final String keyword;
38+
const _CssBackgroundAttachmentKeyword(this.keyword) : super._();
39+
40+
@override
41+
String toCss() => keyword;
42+
}
43+
44+
final class _CssBackgroundAttachmentMultiple extends CssBackgroundAttachment {
45+
final List<CssBackgroundAttachment> attachments;
46+
const _CssBackgroundAttachmentMultiple(this.attachments) : super._();
47+
48+
@override
49+
String toCss() => attachments.map((a) => a.toCss()).join(', ');
50+
}
51+
52+
final class _CssBackgroundAttachmentVariable extends CssBackgroundAttachment {
53+
final String varName;
54+
const _CssBackgroundAttachmentVariable(this.varName) : super._();
55+
56+
@override
57+
String toCss() => 'var(--$varName)';
58+
}
59+
60+
final class _CssBackgroundAttachmentRaw extends CssBackgroundAttachment {
61+
final String value;
62+
const _CssBackgroundAttachmentRaw(this.value) : super._();
63+
64+
@override
65+
String toCss() => value;
66+
}
67+
68+
final class _CssBackgroundAttachmentGlobal extends CssBackgroundAttachment {
69+
final CssGlobal global;
70+
const _CssBackgroundAttachmentGlobal(this.global) : super._();
71+
72+
@override
73+
String toCss() => global.toCss();
74+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import 'package:spark_css/src/css_types/css_background_attachment.dart';
2+
import 'package:spark_css/src/css_types/css_value.dart';
3+
import 'package:test/test.dart';
4+
5+
void main() {
6+
group('CssBackgroundAttachment', () {
7+
test('scroll keyword outputs correct CSS', () {
8+
expect(CssBackgroundAttachment.scroll.toCss(), equals('scroll'));
9+
});
10+
11+
test('fixed keyword outputs correct CSS', () {
12+
expect(CssBackgroundAttachment.fixed.toCss(), equals('fixed'));
13+
});
14+
15+
test('local keyword outputs correct CSS', () {
16+
expect(CssBackgroundAttachment.local.toCss(), equals('local'));
17+
});
18+
19+
test('multiple values output comma-separated string', () {
20+
expect(
21+
CssBackgroundAttachment.multiple([
22+
CssBackgroundAttachment.scroll,
23+
CssBackgroundAttachment.fixed,
24+
]).toCss(),
25+
equals('scroll, fixed'),
26+
);
27+
});
28+
29+
test('variable outputs correct CSS', () {
30+
expect(
31+
CssBackgroundAttachment.variable('bg-attachment').toCss(),
32+
equals('var(--bg-attachment)'),
33+
);
34+
});
35+
36+
test('raw value outputs as-is', () {
37+
expect(
38+
CssBackgroundAttachment.raw('scroll, fixed').toCss(),
39+
equals('scroll, fixed'),
40+
);
41+
});
42+
43+
test('global inherit outputs correct CSS', () {
44+
expect(
45+
CssBackgroundAttachment.global(CssGlobal.inherit).toCss(),
46+
equals('inherit'),
47+
);
48+
});
49+
50+
test('global initial outputs correct CSS', () {
51+
expect(
52+
CssBackgroundAttachment.global(CssGlobal.initial).toCss(),
53+
equals('initial'),
54+
);
55+
});
56+
57+
test('global unset outputs correct CSS', () {
58+
expect(
59+
CssBackgroundAttachment.global(CssGlobal.unset).toCss(),
60+
equals('unset'),
61+
);
62+
});
63+
64+
test('global revert outputs correct CSS', () {
65+
expect(
66+
CssBackgroundAttachment.global(CssGlobal.revert).toCss(),
67+
equals('revert'),
68+
);
69+
});
70+
});
71+
}

0 commit comments

Comments
 (0)