Media queries treat unknown and deprecated media types as `all` (`@media tv` matches, `not tv` does not)

未关闭 适合新手
#11,643 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
82/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
css, typescript
领域
frontend

调研方向

从 packages/core/css-mediaquery/index.ts 第 138 行附近开始,这里未知类型会回退到 all;然后阅读 matchQuery/checkIfMediaQueryMatches,了解查询类型如何与上下文类型比较。沿着它进入 packages/core/media-query-list(matchMedia)以及 _populateRules 路径所在的 packages/core/ui/styling/style-scope.ts 中。用 npx nx run core:test -t 'unknown and deprecated media types' 运行 repro spec;完成意味着 deprecated/unknown 类型从不匹配,not tv 匹配,并且现有的 media-query 测试仍然通过。

由索引模型根据 Issue 内容生成。

描述

Issue Description

Runtime bug in @nativescript/core CSS media query parsing (packages/core/css-mediaquery/index.ts, line 138):

type: MediaQueryType[type ? type.toLowerCase() : 'all'] ?? 'all',

Because MediaQueryType only contains all, print, and screen, any other media type (tv, speech, handheld, projection, or an unknown identifier) falls back to 'all' instead of remaining distinct from 'screen'. Since checkIfMediaQueryMatches evaluates queries with type: MediaQueryType.screen, @media tv { ... } and @media speech { ... } rules are applied on every screen, matchMedia('tv').matches returns true, and @media not tv { ... } / matchMedia('not tv').matches evaluate to false.

Per Media Queries Level 4 section 2.3 and section 3.2 (and in browsers and upstream ericf/css-mediaquery), deprecated and unknown media types never match, and negating them with not evaluates to true.

Reproduction

Save as packages/core/css-mediaquery/repro.spec.ts and run npx nx run core:test -t 'unknown and deprecated media types':

import { matchQuery, MediaQueryType } from '.';
import { matchMedia } from '../media-query-list';
import { parse } from '../css';
import { StyleSheetSelectorScope } from '../ui/styling/css-selector';
import { _populateRules } from '../ui/styling/style-scope';

describe('unknown and deprecated media types', () => {
	it('repro', () => {
		const screen = { type: MediaQueryType.screen, width: 400, height: 800, orientation: 'portrait' };
		console.log('matchQuery tv:', matchQuery('tv', screen));
		console.log('matchQuery speech:', matchQuery('speech', screen));
		console.log('matchQuery tv and portrait:', matchQuery('tv and (orientation: portrait)', screen));
		console.log('matchQuery not tv:', matchQuery('not tv', screen));
		console.log('matchQuery print:', matchQuery('print', screen));
		console.log('matchMedia tv:', matchMedia('tv').matches);
		console.log('matchMedia not tv:', matchMedia('not tv').matches);

		const ast = parse(`
			@media tv { button { color: red; } }
			@media speech { button { color: orange; } }
			@media not tv { button { color: green; } }
			@media print { button { color: blue; } }
		`);
		const rulesets = [];
		_populateRules(ast.stylesheet.rules, rulesets, []);
		const matched = new StyleSheetSelectorScope(rulesets)
			.query({ cssType: 'button' })
			.selectors.map((s) => s.ruleset.declarations[0].value);
		console.log('@media matched colors:', matched);
	});
});

Expected output:

matchQuery tv: false
matchQuery speech: false
matchQuery tv and portrait: false
matchQuery not tv: true
matchQuery print: false
matchMedia tv: false
matchMedia not tv: true
@media matched colors: [ 'green' ]
Relevant log output (if applicable)
matchQuery tv: true
matchQuery speech: true
matchQuery tv and portrait: true
matchQuery not tv: false
matchQuery print: false
matchMedia tv: true
matchMedia not tv: false
@media matched colors: [ 'red', 'orange' ]
Environment
  • @nativescript/core: 9.1.4 (npm latest) and main (2a41eb1)
  • Node.js: v26.13.0
  • Platform-independent (shared @nativescript/core styling and matchMedia code)
Please accept these terms

Credit: https://ucsbplse.github.io/

主要语言
TypeScript
星标
25.7k
派生
1.7k
平均合并
4 天 2 小时
30 天内合并 PR
41

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

NativeScript/NativeScript 的其他 Issue

查看 NativeScript/NativeScript 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。