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

Open Beginner friendly
#11,643 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
82/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
css, typescript
Domain
frontend

Research direction

Start at packages/core/css-mediaquery/index.ts around line 138, where an unknown type falls back to all; then read matchQuery/checkIfMediaQueryMatches to see how the query type is compared against the context type. Follow it into packages/core/media-query-list (matchMedia) and the _populateRules path in packages/core/ui/styling/style-scope.ts. Run the repro spec with npx nx run core:test -t 'unknown and deprecated media types'; done means deprecated/unknown types never match, not tv matches, and existing media-query tests still pass.

Written by the indexing model from the issue text.

Description

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/

Dominant language
TypeScript
Stars
25.7k
Forks
1.7k
Avg merge
4d 2h
Merged PRs (30d)
41

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from NativeScript/NativeScript

All issues in NativeScript/NativeScript

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.