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

No check of overwriting id attribute for dynamic snippets #242

Closed
dakur opened this issue Dec 12, 2019 · 2 comments
Closed

No check of overwriting id attribute for dynamic snippets #242

dakur opened this issue Dec 12, 2019 · 2 comments

Comments

@dakur
Copy link

@dakur dakur commented Dec 12, 2019

Version: 3.0.2

Bug Description

When I set up my own id attribute on an HTML element which also holds n:snippet attribute with dynamic name ('snippet-post' . $post->id), it turns up that a check which is implemented for static snippets is not triggered in case of dynamic ones.

That leads to obscure situations when rerendering of snippets suddenly stops working and you wonder how it is possible when you just added id attribute to HTML (!) and do not know under-the-hood of snippets.

Steps To Reproduce

Use this code and see that the date is not changing when hitting the button:

final class SomePresenter extends Presenter
{
  public function createComponentSomeForm()
  {
    return new Multiplier(function () {
      $form = new Form();
      $form->addSubmit('submit');
      $form->onSuccess[] = function () {
        $this->template->timeOfGeneratingThisValue = \date('d.m.Y H:i:s');
        $this->redrawControl();
      };
      return $form;
    });
  }
}
<script src="https://unpkg.com/naja@1.7.0/dist/Naja.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', () => {
  naja.uiHandler.selector = '[data-naja]';
  naja.initialize();
});
</script>
<div n:foreach="[1, 2, 3] as $id" id="post-{$id}" n:snippet="'snippet-post' . $id">
  {$timeOfGeneratingThisValue}

  <form n:name="'someForm-post' . $id">
    <button n:name="submit">rerenderer</button>
  </form>
</div>

Expected Behavior

Shout the same thing as on static snippets.

Possible Solution

Actually using id attribute to handle snippet is not clean/semantic solution. id attribute is too generic and can be used by anyone with access to DOM so it should be frontendist's privilege to decide what it will be used for and how and backend should not/must not force its own usage. I understand that it came from time of HTML 4, but it should be reconsidered now.

I suggest to take advantage of data-* attribute and set up custom attribute something like data-nette-snippetId which is sure enough not to be collided with anyone else's interest.

dg added a commit to nette/latte that referenced this issue Jan 3, 2020
dg added a commit to nette/latte that referenced this issue Jan 3, 2020
@dg

This comment has been minimized.

Copy link
Member

@dg dg commented Jan 3, 2020

Fixed.

You can change snippet's HTML attribute this way:

final class SomePresenter extends Presenter
{
	public function templatePrepareFilters($template): void
	{
		$template->getLatte()->getCompiler()->getMacros()['snippet'][0]->snippetAttribute = 'data-snippet';
	}
}

I know this is a very complicated way, but there must first be some discussion of how to change it in order to not disrupt the function of components that rely on the original attribute name.

@dg dg closed this Jan 3, 2020
@dakur

This comment has been minimized.

Copy link
Author

@dakur dakur commented Jan 6, 2020

😮 That's crazy, but I understand. Thank you for the fix anyway!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.