Skip to content

Commit

Permalink
Add support for Angular-style attributes to HTML lexer (#907)
Browse files Browse the repository at this point in the history
This commit adds support for `#` to be used in attribute names and
`[]()` to be used in attribute names with values.
  • Loading branch information
Runinho authored and pyrmont committed Jul 21, 2019
1 parent 45da28a commit 8544f86
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/rouge/lexers/html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ def self.detect?(text)

state :tag do
rule %r/\s+/m, Text
rule %r/[a-zA-Z0-9_:-]+\s*=\s*/m, Name::Attribute, :attr
rule %r/[a-zA-Z0-9_:-]+/, Name::Attribute
rule %r/[a-zA-Z0-9_:\[\]()*.-]+\s*=\s*/m, Name::Attribute, :attr
rule %r/[a-zA-Z0-9_:#*-]+/, Name::Attribute
rule %r(/?\s*>)m, Name::Tag, :pop!
end

Expand Down
38 changes: 38 additions & 0 deletions spec/lexers/html_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,44 @@
['Name.Tag', '<custom-element></custom-element>']
end
end
describe 'attribute names' do
it 'allow * to support Angular 2+ structural Directives' do
assert_tokens_equal '<custom-element *ng-structural-directive></custom-element>',
['Name.Tag', '<custom-element'],
['Text', ' '],
['Name.Attribute', '*ng-structural-directive'],
['Name.Tag', '></custom-element>']
end
end
describe 'attribute names' do
it 'allow # to support Angular 2+ template reference variables' do
assert_tokens_equal '<custom-element #ref></custom-element>',
['Name.Tag', '<custom-element'],
['Text', ' '],
['Name.Attribute', '#ref'],
['Name.Tag', '></custom-element>']
end
end
describe 'attribute names' do
it 'allow [] to support Angular 2+ data binding inputs' do
assert_tokens_equal '<custom-element [target]="expression"></custom-element>',
['Name.Tag', '<custom-element'],
['Text', ' '],
['Name.Attribute', '[target]='],
['Literal.String', '"expression"'],
['Name.Tag', '></custom-element>']
end
end
describe 'attribute names' do
it 'allow () to support Angular 2+ data binding outputs' do
assert_tokens_equal '<custom-element (target)="expression"></custom-element>',
['Name.Tag', '<custom-element'],
['Text', ' '],
['Name.Attribute', '(target)='],
['Literal.String', '"expression"'],
['Name.Tag', '></custom-element>']
end
end
end

describe 'guessing' do
Expand Down
5 changes: 5 additions & 0 deletions spec/visual/samples/html
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,8 @@
<!-- no tags -->
Hello tagless world!

<!-- custom Angular 2-style tags -->
<custom-element *ng-structural-directive></custom-element>
<custom-element #ref></custom-element>
<custom-element [target]="expression"></custom-element>
<custom-element (target)="expression"></custom-element>

0 comments on commit 8544f86

Please sign in to comment.