@@ -428,6 +428,27 @@ mod tests {
428428 assert ! ( highlighter. injection_query. is_none( ) ) ;
429429 }
430430
431+ #[ test]
432+ fn default_highlighter_keeps_repeated_highlights_stable ( ) {
433+ let source = "fn main() { let answer = 42; }\n " ;
434+ let mut highlighter = Highlighter :: default ( ) ;
435+
436+ assert ! ( highlighter. highlight( source) . is_empty( ) ) ;
437+ assert ! ( highlighter. set_language_from_path( "main.rs" ) ) ;
438+
439+ let first = highlighter. highlight ( source) ;
440+ let first_tree = highlighter. cached_tree . clone ( ) . unwrap ( ) ;
441+ let second = highlighter. highlight ( source) ;
442+ let second_tree = highlighter. cached_tree . clone ( ) . unwrap ( ) ;
443+
444+ assert_eq ! ( span_tuples( & first) , span_tuples( & second) ) ;
445+ assert_eq ! (
446+ first_tree. root_node( ) . to_sexp( ) ,
447+ second_tree. root_node( ) . to_sexp( )
448+ ) ;
449+ assert_eq ! ( highlighter. cached_source, source) ;
450+ }
451+
431452 #[ test]
432453 fn cloned_highlighter_preserves_markdown_injection_queries ( ) {
433454 let source = "```rust\n fn main() { let answer = 42; }\n ```\n " ;
@@ -484,6 +505,31 @@ mod tests {
484505 ) ;
485506 }
486507
508+ #[ test]
509+ fn gather_injections_ignores_queries_without_content_or_known_language ( ) {
510+ let html = get_language_by_name ( "html" ) . unwrap ( ) ;
511+ let source = "<script>const answer = 42;</script>" ;
512+ let tree = parse_tree ( & html, source) ;
513+
514+ let no_content_query =
515+ Query :: new ( & html. language , "(script_element (raw_text) @none)" ) . unwrap ( ) ;
516+ assert ! ( gather_injections( & no_content_query, & tree, source, 0 ) . is_empty( ) ) ;
517+
518+ let no_language_query = Query :: new (
519+ & html. language ,
520+ "(script_element (raw_text) @injection.content)" ,
521+ )
522+ . unwrap ( ) ;
523+ assert ! ( gather_injections( & no_language_query, & tree, source, 0 ) . is_empty( ) ) ;
524+
525+ let unknown_language_query = Query :: new (
526+ & html. language ,
527+ "((script_element (raw_text) @injection.content) (#set! injection.language \" madeup\" ))" ,
528+ )
529+ . unwrap ( ) ;
530+ assert ! ( gather_injections( & unknown_language_query, & tree, source, 0 ) . is_empty( ) ) ;
531+ }
532+
487533 #[ test]
488534 fn gather_injections_and_highlight_slice_preserve_source_ranges ( ) {
489535 let html = get_language_by_name ( "html" ) . unwrap ( ) ;
@@ -510,6 +556,18 @@ mod tests {
510556 . all( |span| span. start >= 7 && span. end > 7 ) ) ;
511557 }
512558
559+ #[ test]
560+ fn highlight_slice_returns_empty_for_invalid_queries ( ) {
561+ let rust = get_language_by_name ( "rust" ) . unwrap ( ) ;
562+ let invalid_query_support = LanguageSupport {
563+ language : rust. language ,
564+ highlight_query : "(" ,
565+ injection_query : None ,
566+ } ;
567+
568+ assert ! ( highlight_slice( & invalid_query_support, "fn main() {}\n " , 0 , 0 ) . is_empty( ) ) ;
569+ }
570+
513571 #[ test]
514572 fn merge_injection_spans_replaces_overlaps_and_sorts_output ( ) {
515573 let outer = vec ! [
0 commit comments