Skip to content

Commit e12b971

Browse files
author
deathaxe
authored
[HTML] Add scopes for utf8 percent encoded characters (#2374)
* [HTML] Add scopes for utf8 percent encoded characters specs: https://url.spec.whatwg.org/#userinfo-percent-encode-set Addresses a "lively" discussion at https://forum.sublimetext.com/t/advanced-other-color-or-hide-html-20-in-st-user-interface/51447 * [HTML] add url sub scope * [HTML] Limit utf-8 percent encoding to src and href attributes * [HTML] Clarify test cases
1 parent 9532475 commit e12b971

File tree

2 files changed

+80
-7
lines changed

2 files changed

+80
-7
lines changed

HTML/HTML.sublime-syntax

+47
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,52 @@ contexts:
702702
0: meta.string.html string.quoted.single.html punctuation.definition.string.end.html
703703
- include: else-pop
704704

705+
tag-href-attribute:
706+
- match: (?i:href|src){{attribute_name_break}}
707+
scope: entity.other.attribute-name.href.html
708+
push:
709+
- meta_scope: meta.attribute-with-value.href.html
710+
- match: '='
711+
scope: punctuation.separator.key-value.html
712+
push: tag-href-attribute-value
713+
- include: else-pop
714+
715+
tag-href-attribute-value:
716+
- match: \"
717+
scope: punctuation.definition.string.begin.html
718+
set:
719+
- meta_scope: meta.string.html string.quoted.double.html
720+
- match: \"
721+
scope: punctuation.definition.string.end.html
722+
pop: 2
723+
- include: tag-attribute-value-separator
724+
- include: tag-href-entities
725+
- match: \'
726+
scope: punctuation.definition.string.begin.html
727+
set:
728+
- meta_scope: meta.string.html string.quoted.single.html
729+
- match: \'
730+
scope: punctuation.definition.string.end.html
731+
pop: 2
732+
- include: tag-attribute-value-separator
733+
- include: tag-href-entities
734+
- match: '{{unquoted_attribute_start}}'
735+
set:
736+
- meta_scope: meta.string.html string.unquoted.html
737+
- match: '{{unquoted_attribute_break}}'
738+
pop: 2
739+
- match: '["''`<]'
740+
scope: invalid.illegal.attribute-value.html
741+
- include: tag-href-entities
742+
- include: else-pop
743+
744+
tag-href-entities:
745+
- include: entities
746+
- match: (%)\h{2}
747+
scope: constant.character.escape.url.html
748+
captures:
749+
1: punctuation.definition.escape.html
750+
705751
# This is to prevent breaking syntaxes referencing the old context name
706752
tag-stuff:
707753
- include: tag-attributes
@@ -711,6 +757,7 @@ contexts:
711757
- include: tag-class-attribute
712758
- include: tag-style-attribute
713759
- include: tag-event-attribute
760+
- include: tag-href-attribute
714761
- include: tag-generic-attribute
715762

716763
tag-attribute-value-separator:

HTML/syntax_test_html.html

+33-7
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,38 @@
237237
## ^^^^^^^^^^^^^^^ string.unquoted
238238
## ^^ punctuation.definition.tag.end.html
239239

240-
<img src=/f̱oo⏡/bar/baz/ />
240+
<!-- Match utf-8 escapes (%2F) in src tags -->
241+
<img src=/f̱oo⏡/bar%2Fbaz/ />
241242
## ^ - meta.attribute-with-value
242243
## ^^^ entity.other.attribute-name
243-
## ^^^^^^^^^^^^^^^^^^^ meta.attribute-with-value
244-
## ^^^^^^^^^^^^^^^ string.unquoted
245-
## ^ - meta.attribute-with-value
246-
## ^^ punctuation.definition.tag.end.html
244+
## ^^^^^^^^^^^^^^^^^^^^^ meta.attribute-with-value
245+
## ^^^^^^^^^^^^^^^^^ string.unquoted
246+
## ^ - constant.character.escape
247+
## ^ constant.character.escape.url.html punctuation.definition.escape.html
248+
## ^^ constant.character.escape.url.html - punctuation.definition
249+
## ^ - constant.character.escape
250+
## ^ - meta.attribute-with-value
251+
## ^^ punctuation.definition.tag.end.html
252+
253+
<!-- Match utf-8 escapes (%2F) in href tags -->
254+
<img href="/foo%2Fbar.com" />
255+
## ^ - meta.attribute-with-value
256+
## ^^^^ entity.other.attribute-name
257+
## ^^^^^^^^^^^^^^^^^^^^^ meta.attribute-with-value
258+
## ^^^^^^^^^^^^^^^^ string.quoted.double
259+
## ^ constant.character.escape.url.html punctuation.definition.escape.html
260+
## ^^ constant.character.escape.url.html - punctuation.definition
261+
## ^ - meta.attribute-with-value
262+
## ^^ punctuation.definition.tag.end.html
263+
264+
<!-- Don't match utf-8 escapes (%2F) in other than href/src tags -->
265+
<img title="http://foo%2Fbar.com" />
266+
## ^ - meta.attribute-with-value
267+
## ^^^ entity.other.attribute-name
268+
## ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.attribute-with-value
269+
## ^^^^^^^^^^^^^^^^^^^^^^ string.quoted.double - constant.character.escape
270+
## ^ - meta.attribute-with-value
271+
## ^^ punctuation.definition.tag.end.html
247272

248273
<img title=foo"/>
249274
## ^ - meta.attribute-with-value
@@ -682,8 +707,9 @@
682707
## ^^^^ - constant.character.entity
683708

684709

685-
<a href="http://google.com/?one=1&amp;two=2"></a>
686-
## ^^^^^ constant.character.entity
710+
<a href="http://google%20api.com/?one=1&amp;two=2"></a>
711+
## ^^^ constant.character.escape.url
712+
## ^^^^^ constant.character.entity
687713
<a href="http://google.com/?one=1&two=2"></a>
688714
## ^ - constant.character.entity
689715
## ^ - invalid.illegal

0 commit comments

Comments
 (0)