Skip to content

Conversation

@osamalzabidi
Copy link
Contributor

@osamalzabidi osamalzabidi commented Nov 18, 2025

  1. Allows spaces and special characters commonly found in paths
  2. Platform-aware - distinguishes between Windows and Unix paths
  3. Excludes invalid characters (*, ?, ", <, >, |)
  4. Context-aware - considers file extensions and path structure
  5. Better false-positive prevention - excludes URLs and base64

Description

Test Code

# Test code for the review of this PR

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist

  • I signed the CLA.
  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • New and existing tests pass locally with my changes
  • I have made corresponding changes to the documentation (if applicable)

Screenshots

Additional details

Summary by Sourcery

Refine path and URL detection in isUrlOrPath by using a unified regex that supports Windows and Unix paths, standalone filenames, and HTTPS URLs while excluding invalid characters.

Enhancements:

  • Update URL regex to match both HTTP and HTTPS protocols
  • Replace generic file path regex with a platform-aware pattern for Windows drives/UNC, Unix absolute/relative paths, and filenames
  • Exclude invalid file path characters (*, ?, ", <, >, |) to prevent false positives

1. Allows spaces and special characters commonly found in paths
2. Platform-aware - distinguishes between Windows and Unix paths
3. Excludes invalid characters (*, ?, ", <, >, |)
4. Context-aware - considers file extensions and path structure
5. Better false-positive prevention - excludes URLs and base64
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've reviewed this pull request using the Sourcery rules engine

@osamalzabidi osamalzabidi changed the title Improvements Over Path check Regex Improvements Over Path check in Regex Nov 19, 2025
@ndonkoHenri ndonkoHenri requested a review from Copilot November 22, 2025 02:00
Copilot finished reviewing on behalf of ndonkoHenri November 22, 2025 02:02
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refines the path and URL detection logic in the isUrlOrPath function to be more robust and platform-aware, supporting both Windows and Unix path formats while excluding invalid file path characters.

Key Changes:

  • Updated URL regex to use https? shorthand for matching both HTTP and HTTPS protocols
  • Replaced simple file path regex with a comprehensive pattern supporting Windows drives/UNC paths, Unix absolute/relative paths, and standalone filenames
  • Added exclusion of invalid file path characters (*, ?, ", <, >, |) to prevent false positives
Comments suppressed due to low confidence (1)

packages/flet/lib/src/utils/images.dart:114

  • The function isUrlOrPath lacks test coverage. Consider adding tests to verify:
  • Empty string handling
  • Various Windows path formats (C:\path\file.txt, \server\share\file.txt)
  • Various Unix path formats (~/file.txt, ./file.txt, ../file.txt, /absolute/path)
  • Simple filenames (file.txt, image.png)
  • Edge cases with invalid characters (*, ?, ", <, >, |)
  • URL formats (http://, https://, www.)
  • False positives like base64 strings

This is especially important given the complexity of the new regex patterns.

bool isUrlOrPath(String value) {
  // Check for URL pattern
  final urlPattern = RegExp(r'^(https?:\/\/|www\.)');
  if (urlPattern.hasMatch(value)) {
    return true;
  }

  // Check for common file path characters
  final filePathPattern = RegExp(
    r'^('
    r'([a-zA-Z]:\\|\\\\)?([^\\/:*?"<>|\r\n]+\\)*[^\\/:*?"<>|\r\n]*'  // Windows paths
    r'|'  // OR
    r'(~?\/|\.\/|\.\.\/)?([^\/:*?"<>|\r\n]+\/)*[^\/:*?"<>|\r\n]*'    // Unix paths  
    r'|'  // OR
    r'[^\\/:*?"<>|\r\n]+'  // Just filenames without path separators
    r')$'  
  );
  if (filePathPattern.hasMatch(value)) {
    return true;
  }

  return false;
}

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +100 to +108
final filePathPattern = RegExp(
r'^('
r'([a-zA-Z]:\\|\\\\)?([^\\/:*?"<>|\r\n]+\\)*[^\\/:*?"<>|\r\n]*' // Windows paths
r'|' // OR
r'(~?\/|\.\/|\.\.\/)?([^\/:*?"<>|\r\n]+\/)*[^\/:*?"<>|\r\n]*' // Unix paths
r'|' // OR
r'[^\\/:*?"<>|\r\n]+' // Just filenames without path separators
r')$'
);
Copy link

Copilot AI Nov 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new file path regex can match empty strings, which is likely unintended. For example:

  • The Windows path pattern ([a-zA-Z]:\\|\\\\)?([^\\/:*?"<>|\r\n]+\\)*[^\\/:*?"<>|\r\n]* has optional components that can all be empty
  • The Unix path pattern (~?\/|\.\/|\.\.\/)?([^\/:*?"<>|\r\n]+\/)*[^\/:*?"<>|\r\n]* also has optional components with * quantifiers allowing zero matches

This means isUrlOrPath("") would return true, which is probably not the desired behavior. Consider using + instead of * for the final character class in each path pattern, or adding a length check before the regex matching.

Copilot uses AI. Check for mistakes.
bool isUrlOrPath(String value) {
// Check for URL pattern
final urlPattern = RegExp(r'^(http:\/\/|https:\/\/|www\.)');
final urlPattern = RegExp(r'^(https?:\/\/|www\.)');
Copy link

Copilot AI Nov 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The URL pattern ^(https?:\/\/|www\.) will match strings like "www.example" but also "www." or "http://" on their own. Consider adding a pattern to ensure there's something after the protocol/prefix, such as ^(https?:\/\/.+|www\..+) to prevent matching incomplete URLs.

Suggested change
final urlPattern = RegExp(r'^(https?:\/\/|www\.)');
final urlPattern = RegExp(r'^(https?:\/\/.+|www\..+)');

Copilot uses AI. Check for mistakes.
r'^('
r'([a-zA-Z]:\\|\\\\)?([^\\/:*?"<>|\r\n]+\\)*[^\\/:*?"<>|\r\n]*' // Windows paths
r'|' // OR
r'(~?\/|\.\/|\.\.\/)?([^\/:*?"<>|\r\n]+\/)*[^\/:*?"<>|\r\n]*' // Unix paths
Copy link

Copilot AI Nov 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Trailing whitespace detected at the end of the comment. Consider removing it for cleaner code.

Suggested change
r'(~?\/|\.\/|\.\.\/)?([^\/:*?"<>|\r\n]+\/)*[^\/:*?"<>|\r\n]*' // Unix paths
r'(~?\/|\.\/|\.\.\/)?([^\/:*?"<>|\r\n]+\/)*[^\/:*?"<>|\r\n]*' // Unix paths

Copilot uses AI. Check for mistakes.

return false;
}

Copy link

Copilot AI Nov 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function isUrlOrPath lacks documentation explaining its purpose, parameters, return value, and expected behavior. Given the complexity of the new regex patterns and the specific exclusions mentioned in the PR description (URLs, base64, invalid characters), adding a doc comment would improve maintainability. For example:

/// Checks if the given [value] is a valid URL or file path.
/// 
/// Returns `true` if [value] matches:
/// - HTTP/HTTPS URLs (e.g., "https://example.com", "http://site.org")
/// - Windows paths (e.g., "C:\path\file.txt", "\\server\share\file")
/// - Unix paths (e.g., "/absolute/path", "~/home/file", "./relative")
/// - Simple filenames (e.g., "image.png", "document.txt")
/// 
/// Invalid file path characters (*, ?, ", <, >, |) are excluded.
/// Base64-encoded strings should be checked with [isBase64ImageString].
bool isUrlOrPath(String value) {
Suggested change
/// Checks if the given [value] is a valid URL or file path.
///
/// Returns `true` if [value] matches:
/// - HTTP/HTTPS URLs (e.g., "https://example.com", "http://site.org")
/// - URLs starting with "www."
/// - Windows paths (e.g., "C:\path\file.txt", "\\server\share\file")
/// - Unix paths (e.g., "/absolute/path", "~/home/file", "./relative")
/// - Simple filenames (e.g., "image.png", "document.txt")
///
/// Invalid file path characters (*, ?, ", <, >, |) are excluded.
/// Base64-encoded strings should be checked with [isBase64ImageString].

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant