Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP injection inside :attribute="php_only" #33

Closed
EmranMR opened this issue Aug 30, 2023 · 3 comments
Closed

PHP injection inside :attribute="php_only" #33

EmranMR opened this issue Aug 30, 2023 · 3 comments
Labels
enhancement New feature or request

Comments

@EmranMR
Copy link
Owner

EmranMR commented Aug 30, 2023

Add support for php injection inside :attribute directives
see Passing Data

@EmranMR EmranMR added the enhancement New feature or request label Aug 30, 2023
@calebdw
Copy link
Contributor

calebdw commented Dec 28, 2023

The way I look at this, there's two ways to go about this:

  1. Duplicate a lot of the html parser to add support for blade / livewire components (<x-foo>)
  2. Extend the html injections to inject php_only into :attribute attributes inside elements matching a certain predicate
  1. would be the easiest (I've already done this for AlpineJS), but wouldn't be self contained in this parser

@EmranMR
Copy link
Owner Author

EmranMR commented Dec 28, 2023

@calebdw I had your option 2 in mind myself.

Basically define ::attribute #34 and :attributeusing a regex grammar. Then of course injecting JavaScript for one, and the php_only for the other.

In terms of highlighting, users then having the freedom to either paint them as attributes or framework.tag or whatever that works.

I haven't had a chance to give it a shot yet. It might be easier said than done when it comes to regex haha. But we shall see! I have a feeling the : and :: acting up once defined!

I had the same idea for #28 but I think it would be easier for that, because it's just a simple regex, I am not aware of any other framework using a similar syntax?

@calebdw
Copy link
Contributor

calebdw commented Dec 28, 2023

Basically define ::attribute and :attribute using a regex grammar. Then of course injecting JavaScript for one, and the php_only for the other.

It's not that simple---those injections are only valid inside a blade component element which you would have no way of knowing without copying a lot of the html parser to determine what's an element, attribute, etc.

The simplest thing to do is to just extend the default html injections, the below html injections solves this issue, #28, and #34 without any changes needed to the blade parser:

nvim/after/queries/html/injections.scm

;; extends

; AlpineJS attributes
(attribute
  (attribute_name) @_attr
    (#lua-match? @_attr "^x%-%l")
  (quoted_attribute_value
    (attribute_value) @injection.content)
  (#set! injection.language "javascript"))

; Blade escaped JS attributes
; <x-foo ::bar="baz" />
(element
  (_
    (tag_name) @_tag
      (#lua-match? @_tag "^x%-%l")
  (attribute
    (attribute_name) @_attr
      (#lua-match? @_attr "^::%l")
    (quoted_attribute_value
      (attribute_value) @injection.content)
    (#set! injection.language "javascript"))))

; Blade PHP attributes
; <x-foo :bar="$baz" />
(element
  (_
    (tag_name) @_tag
      (#lua-match? @_tag "^x%-%l")
    (attribute
      (attribute_name) @_attr
        (#lua-match? @_attr "^:%l")
      (quoted_attribute_value
        (attribute_value) @injection.content)
      (#set! injection.language "php_only"))))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants