mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 07:22:45 +00:00 
			
		
		
		
	LibWeb: Set consistent positions for the start and end of HTML tags
To illustrate the previous behavior, consider these tags and their start
and end positions (shown inclusively below):
    Start tag:    End tag:
    <span>        </span>
     ^ start       ^ start
         ^end           ^end
The start position of a tag is the first ASCII-alpha code point after
the opening brace. The start position of a close tag is the slash just
before the first ASCII-alpha code point. And the end position of both
is the closing brace. So the opening brace is not included in the
emitted tag, but the closing brace is. And the end tag including the
slash is an oddity that had to be worked around in its only use case
(syntax highlighting).
We now consistently exclude the braces from the emitted tag, and also
exclude the slash from the end tag, so that it does not need to be
accounted for in syntax highlighting. That is, we now have:
    Start tag:    End tag:
    <span>        </span>
     ^ start        ^ start
        ^end           ^end
The tokenizer unit test has been extended to test these positions.
			
			
This commit is contained in:
		
							parent
							
								
									70a87795e4
								
							
						
					
					
						commit
						5b2bc90b50
					
				
					 3 changed files with 39 additions and 47 deletions
				
			
		|  | @ -132,8 +132,6 @@ void SyntaxHighlighter::rehighlight(Palette const& palette) | |||
|             continue; | ||||
|         } | ||||
| 
 | ||||
|         size_t token_start_offset = token->is_end_tag() ? 1 : 0; | ||||
| 
 | ||||
|         if (token->is_comment()) { | ||||
|             highlight( | ||||
|                 token->start_position().line, | ||||
|  | @ -150,25 +148,25 @@ void SyntaxHighlighter::rehighlight(Palette const& palette) | |||
|         } else if (token->is_start_tag() || token->is_end_tag()) { | ||||
|             highlight( | ||||
|                 token->start_position().line, | ||||
|                 token->start_position().column + token_start_offset, | ||||
|                 token->start_position().column, | ||||
|                 token->start_position().line, | ||||
|                 token->start_position().column + token_start_offset + token->tag_name().length(), | ||||
|                 token->start_position().column + token->tag_name().length(), | ||||
|                 { palette.syntax_keyword(), {}, true }, | ||||
|                 token->is_start_tag() ? AugmentedTokenKind::OpenTag : AugmentedTokenKind::CloseTag); | ||||
| 
 | ||||
|             token->for_each_attribute([&](auto& attribute) { | ||||
|                 highlight( | ||||
|                     attribute.name_start_position.line, | ||||
|                     attribute.name_start_position.column + token_start_offset, | ||||
|                     attribute.name_start_position.column, | ||||
|                     attribute.name_end_position.line, | ||||
|                     attribute.name_end_position.column + token_start_offset, | ||||
|                     attribute.name_end_position.column, | ||||
|                     { palette.syntax_identifier(), {} }, | ||||
|                     AugmentedTokenKind::AttributeName); | ||||
|                 highlight( | ||||
|                     attribute.value_start_position.line, | ||||
|                     attribute.value_start_position.column + token_start_offset, | ||||
|                     attribute.value_start_position.column, | ||||
|                     attribute.value_end_position.line, | ||||
|                     attribute.value_end_position.column + token_start_offset, | ||||
|                     attribute.value_end_position.column, | ||||
|                     { palette.syntax_string(), {} }, | ||||
|                     AugmentedTokenKind::AttributeValue); | ||||
|                 return IterationDecision::Continue; | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Timothy Flynn
						Timothy Flynn