-
Notifications
You must be signed in to change notification settings - Fork 242
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
[JENKINS-72136] Remove inline onclick handlers for generate directive button #678
[JENKINS-72136] Remove inline onclick handlers for generate directive button #678
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested your changes locally, with the CSP plugins, and it works fine!
I'm not able to see the previous CSP violations!
...rces/org/jenkinsci/plugins/pipeline/modeldefinition/generator/DirectiveGenerator/index.jelly
Outdated
Show resolved
Hide resolved
...rces/org/jenkinsci/plugins/pipeline/modeldefinition/generator/DirectiveGenerator/index.jelly
Outdated
Show resolved
Hide resolved
...rces/org/jenkinsci/plugins/pipeline/modeldefinition/generator/DirectiveGenerator/index.jelly
Outdated
Show resolved
Hide resolved
...s/org/jenkinsci/plugins/pipeline/modeldefinition/generator/DirectiveGenerator/indexScript.js
Outdated
Show resolved
Hide resolved
13f816c
to
f18663b
Compare
Hi, that's great. Sorry for the extra spaces. Will take care in future contributions. |
Hi @Kevin-CB can we merge this before end of October? I would like it to be a part of my hacktoberfest contribution. |
Hi @NeetigyaPod, even though I approved this PR, I can't merge it as I'm not a maintainer of this plugin. |
@jglick Can you please help me in this? Would appreciate a lot |
// TODO JSON.stringify fails in some circumstances: https://gist.github.com/jglick/70ec4b15c1f628fdf2e9 due to Array.prototype.toJSON | ||
// TODO simplify when Prototype.js is removed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
likely needs to be revisited in another PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be done. Will work on it after this one
@@ -78,34 +78,10 @@ | |||
</f:dropdownList> | |||
<j:set var="id" value="${h.generateId()}"/> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could probably get rid of the generateId
trick now, by having the button look for a sibling textarea.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sibling in the sense- I can allocate a similar class name to the text area and remove all the mentions of the id variable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would have to look up some example, since I do not normally work on JS at all, but IIRC the point is that now that the behavior function is passed an element it would be able to use that argument to perform operations without a (unique) ID—either directly on the element in the argument, or on some sibling in the DOM.
In other words: when using inline <script>
with a Jelly facet that might be included multiple times on a page, like the config snippet for a build step in a freestyle project config screen, you have the problem that
<body>
<!-- first occurrence -->
<div>
<button onclick="handle()"/>
<textarea id="spot"/>
<script>
function handle() {
document.getElementById('spot').text = 'handled';
}
</script>
</div>
</body>
will not work as soon as you add a second <div>
with the same content: clicking either button will update the same textarea, perhaps the wrong one. generateId
used to be used as a workaround:
<body>
<!-- first occurrence -->
<div>
<j:set var="id" value="${h.generateId()}"/>
<button onclick="handle()"/>
<textarea id="spot-${id}"/>
<script>
function handle() {
document.getElementById('spot-${id}').text = 'handled';
}
</script>
</div>
</body>
When switching to a proper adjunct, you do not need this trick since
<body>
<!-- first occurrence -->
<div>
<button class="mybutton"/>
<textarea/>
<st:adjunct …/>
</div>
</body>
function handle(button) {
button.getSiblingByType('textarea').text = 'handled';
}
will work regardless of how many copies there are. (All code above is pseudocode, actual method names etc. are going to be a bit different.)
Of course in many cases the Jelly fragment could never be included more than once in a page because of what it is for, so whether a unique id was even needed here to begin with, I am unsure; it may have been included “just in case”.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ohhhh makes sense. Thank you, will treat it as nit for future contributions in Jenkins.
@Kevin-CB have you retested after the latest changes? |
Yes, last time I tested, it was on f18663b |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks OK at a glance (relying on other reviewers for details & testing).
Testing:
Screenshots:
Before:
After:
mvn tests run and changes appear in mvn:hpi run