Skip to content

Commit bd1a2eb

Browse files
authored
Allow Colon : in HTML Attribute Names (#496)
This pull request updates the parser to allow HTML Attributes to start with a colon, which is typical in the Vue.js style: ```html <div :class="classes"></div> ``` Previously, this caused a syntax error: ```js @ UnexpectedError (location: (1:5)-(1:6)) β”œβ”€β”€ message: "Unexpected Token. Expected: `TOKEN_IDENTIFIER, TOKEN_AT, TOKEN_ERB_START,TOKEN_WHITESPACE, or TOKEN_NEWLINE`, found: `TOKEN_COLON`." β”œβ”€β”€ description: "Unexpected Token" β”œβ”€β”€ expected: "TOKEN_IDENTIFIER, TOKEN_AT, TOKEN_ERB_START,TOKEN_WHITESPACE, or TOKEN_NEWLINE" └── found: "TOKEN_COLON" ``` Now this parses as: ```js @ HTMLAttributeNode (location: (1:5)-(1:21)) β”œβ”€β”€ errors: [] β”œβ”€β”€ name: β”‚ └── @ HTMLAttributeNameNode (location: (1:5)-(1:11)) β”‚ β”œβ”€β”€ errors: [] β”‚ └── children: (1 item) β”‚ └── @ LiteralNode (location: (1:5)-(1:11)) β”‚ β”œβ”€β”€ errors: [] β”‚ └── content: ":class" β”‚ β”œβ”€β”€ equals: "=" (location: (1:11)-(1:12)) └── value: └── @ HTMLAttributeValueNode (location: (1:12)-(1:21)) β”œβ”€β”€ errors: [] β”œβ”€β”€ open_quote: """ (location: (1:12)-(1:13)) β”œβ”€β”€ children: (1 item) β”‚ └── @ LiteralNode (location: (1:13)-(1:20)) β”‚ β”œβ”€β”€ errors: [] β”‚ └── content: "classes" β”‚ β”‚ β”œβ”€β”€ close_quote: """ (location: (1:20)-(1:21)) └── quoted: true ```
1 parent 6336595 commit bd1a2eb

10 files changed

+474
-0
lines changed

β€Žsrc/parser.cβ€Ž

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,20 @@ static AST_HTML_OPEN_TAG_NODE_T* parser_parse_html_open_tag(parser_T* parser) {
805805
continue;
806806
}
807807

808+
if (parser->current_token->type == TOKEN_COLON) {
809+
lexer_T lexer_copy = *parser->lexer;
810+
token_T* next_token = lexer_next_token(&lexer_copy);
811+
812+
if (next_token && next_token->type == TOKEN_IDENTIFIER) {
813+
token_free(next_token);
814+
array_append(children, parser_parse_html_attribute(parser));
815+
816+
continue;
817+
}
818+
819+
token_free(next_token);
820+
}
821+
808822
parser_append_unexpected_error(
809823
parser,
810824
"Unexpected Token",

β€Žtest/parser/attributes_test.rbβ€Ž

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,5 +159,37 @@ class AttributesTest < Minitest::Spec
159159
test "attribute with backtick containing HTML (invalid)" do
160160
assert_parsed_snapshot(%(<div data-template=`<span>Hello</span>`></div>))
161161
end
162+
163+
test "Vue-style directive attribute with value" do
164+
assert_parsed_snapshot(%(<div :value="something"></div>))
165+
end
166+
167+
test "Vue-style directive attributes multiple" do
168+
assert_parsed_snapshot(%(<input :model="user" :disabled="isDisabled" :class="className"></input>))
169+
end
170+
171+
test "Vue-style directive attribute without value" do
172+
assert_parsed_snapshot(%(<div :disabled></div>))
173+
end
174+
175+
test "Mixed Vue directives and regular attributes" do
176+
assert_parsed_snapshot(%(<div id="app" :class="dynamicClass" data-test="static"></div>))
177+
end
178+
179+
test "Standalone colon with space is invalid" do
180+
assert_parsed_snapshot(%(<div : class="hello"></div>))
181+
end
182+
183+
test "Colon immediately followed by attribute name is valid" do
184+
assert_parsed_snapshot(%(<div :class="hello"></div>))
185+
end
186+
187+
test "Double colon is invalid" do
188+
assert_parsed_snapshot(%(<div ::value="hello"></div>))
189+
end
190+
191+
test "Vue directive with namespace-like syntax" do
192+
assert_parsed_snapshot(%(<div :v-model="user"></div>))
193+
end
162194
end
163195
end

β€Žtest/snapshots/parser/attributes_test/test_0038_Vue-style_directive_attribute_with_value_48a4fb492a8544d35ffb4d589f5a602c.txtβ€Ž

Lines changed: 42 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Žtest/snapshots/parser/attributes_test/test_0039_Vue-style_directive_attributes_multiple_4f1d1b98f3b9c869967323f090d9afee.txtβ€Ž

Lines changed: 89 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Žtest/snapshots/parser/attributes_test/test_0040_Vue-style_directive_attribute_without_value_14a36e2ed468092336d9f6080e939190.txtβ€Ž

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Žtest/snapshots/parser/attributes_test/test_0041_Mixed_Vue_directives_and_regular_attributes_355d0da3aae174783058e2ccec41ac05.txtβ€Ž

Lines changed: 82 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Žtest/snapshots/parser/attributes_test/test_0042_Standalone_colon_with_space_is_invalid_c037b25064b31d22c6c195361999c5b8.txtβ€Ž

Lines changed: 49 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
Β (0)