tiny.ui.style.media
Defined in style.
API (10)
Actions
Public operations.
Types and contracts
Public types and contracts.
Values and defaults
Public values and defaults.
Source
Source: lib/ui/src/style/media.zig
zig
const std = @import("std");const css = @import("css");const abi = @import("../abi/root.zig");pub const ColorScheme = css.media.ColorScheme;pub const ReducedMotion = css.media.ReducedMotion;pub const resolution_media = "ui.lint.resolution_media";pub const Environment = struct { width: f32, height: f32, root_font_size: f32, color_scheme: ColorScheme, reduced_motion: ReducedMotion, pub fn fromHeader(header: abi.Header, color_scheme: ColorScheme, reduced_motion: ReducedMotion) Environment { return .{ .width = header.viewport.width, .height = header.viewport.height, .root_font_size = header.root_font_size, .color_scheme = color_scheme, .reduced_motion = reduced_motion, }; } pub fn cssEnvironment(self: Environment) css.media.MediaEnvironment { return .{ .media_type = .screen, .width = self.width, .height = self.height, .font_size = self.root_font_size, .color_scheme = self.color_scheme, .reduced_motion = self.reduced_motion, }; } pub fn parse(self: Environment, workspace: *css.Workspace, source: []const u8) !css.StyleSheet { return workspace.parse(source, self.cssEnvironment()); }};pub fn applies(query_list: []const u8, environment: Environment) bool { return css.media.mediaListApplies(query_list, environment.cssEnvironment());}pub const Report = struct { code: []const u8 = resolution_media, start: usize, end: usize,};pub fn lintResolutionMedia(query_list: []const u8) ?Report { var index: usize = 0; while (index < query_list.len) { const byte = query_list[index]; if (byte == '\'' or byte == '"') { const quote = byte; index += 1; while (index < query_list.len) : (index += 1) { if (query_list[index] == '\\' and index + 1 < query_list.len) { index += 1; continue; } if (query_list[index] == quote) { index += 1; break; } } continue; } if (byte == '/' and index + 1 < query_list.len and query_list[index + 1] == '*') { index += 2; while (index + 1 < query_list.len and !(query_list[index] == '*' and query_list[index + 1] == '/')) : (index += 1) {} index = @min(query_list.len, index + 2); continue; } if (!identifierByte(byte)) { index += 1; continue; } const start = index; while (index < query_list.len and identifierByte(query_list[index])) : (index += 1) {} if (isResolutionFeature(query_list[start..index])) return .{ .start = start, .end = index }; } return null;}fn isResolutionFeature(raw: []const u8) bool { var name = raw; for ([_][]const u8{ "-webkit-", "-moz-", "-o-", "-ms-" }) |prefix| { if (startsWithIgnoreCase(name, prefix)) { name = name[prefix.len..]; break; } } if (startsWithIgnoreCase(name, "min-")) name = name[4..]; if (startsWithIgnoreCase(name, "max-")) name = name[4..]; return std.ascii.eqlIgnoreCase(name, "resolution") or std.ascii.eqlIgnoreCase(name, "device-pixel-ratio");}fn startsWithIgnoreCase(text: []const u8, prefix: []const u8) bool { return text.len >= prefix.len and std.ascii.eqlIgnoreCase(text[0..prefix.len], prefix);}fn identifierByte(byte: u8) bool { return std.ascii.isAlphanumeric(byte) or byte == '-' or byte == '_';}test "ui media evaluates viewport and preference inputs without unit scale" { var header = abi.Header{ .viewport = .{ .width = 1200, .height = 800 }, .root_font_size = 16, .unit_scale = 1, }; const first = Environment.fromHeader(header, .dark, .reduce); header.unit_scale = 3; const second = Environment.fromHeader(header, .dark, .reduce); try std.testing.expectEqualDeep(first, second); try std.testing.expect(applies("(min-width: 1000px) and (prefers-color-scheme: dark)", first)); try std.testing.expect(applies("(aspect-ratio: 3/2) and (prefers-reduced-motion: reduce)", first)); try std.testing.expect(!applies("not (resolution: 2dppx)", first));}test "ui lint resolution_media reports unsupported feature tokens" { for ([_][]const u8{ "(resolution: 2dppx)", "(min-resolution: 96dpi)", "(-webkit-device-pixel-ratio: 2)", "(-moz-max-device-pixel-ratio: 2)", }) |query| { const report = lintResolutionMedia(query).?; try std.testing.expectEqualStrings(resolution_media, report.code); try std.testing.expect(report.start < report.end); } try std.testing.expect(lintResolutionMedia("(prefers-color-scheme: dark)") == null); try std.testing.expect(lintResolutionMedia("(width: 10px) /* resolution */") == null); try std.testing.expect(lintResolutionMedia("(width: 10px) and ('resolution': 2)") == null);}Source: lib/ui/src/style/root.zig:6
zig
pub const media = @import("media.zig");Audit
| Definitions | 11 |
|---|---|
| Public names | 11 |
| Members | 8 |
| Version | 26.7.0 |
| Revision | daab053ee433 |