Skip to content

MIME regex matching uses the filename range #370

Description

@boazy

Description

A Plain-file rule that combines a filename regex with a MIME regex can fail even when both values should match. PlainSettings.test(filename:mimeType:) builds the MIME search range from filename rather than mimeType:

regex.firstMatch(
    in: mimeType,
    options: [],
    range: NSRange(filename.startIndex..., in: filename)
)

For short filenames this truncates the MIME search range. For example, the filename hproxy produces a six-character range, so the pattern text/x-shellscript cannot match the detected MIME value.

Reproduction

Syntax Highlight 2.1.30 (79):

  1. Create an executable extensionless file named hproxy beginning with #!/bin/bash.

  2. Confirm its metadata:

    /usr/bin/file --mime --brief hproxy
    text/x-shellscript; charset=us-ascii
    
    mdls -name kMDItemContentType hproxy
    kMDItemContentType = "public.unix-executable"
    
  3. Add a Plain text-file rule:

    • Filename pattern: .*
    • MIME pattern: text/x-shellscript
    • Regular expression: enabled
    • Syntax: Bash
  4. Preview the file with Quick Look.

Expected

Both regexes match and the file is highlighted as Bash.

Actual

The file is not highlighted because MIME matching is limited to a range derived from the six-character filename.

Proposed fix

Construct the MIME range from mimeType:

range: NSRange(mimeType.startIndex..., in: mimeType)

A focused test should cover filenames shorter and longer than their MIME values.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions