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

[JENKINS-27505] Preserve an empty line at the beginning of a textarea #1612

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions core/src/main/resources/lib/form/textarea.jelly
Expand Up @@ -89,6 +89,7 @@ THE SOFTWARE.
readonly="${attrs.readonly}"
codemirror-mode="${attrs['codemirror-mode']}"
codemirror-config="${attrs['codemirror-config']}">
<j:if test="${value != null &amp;&amp; !empty(value.toString()) &amp;&amp; (value.toString().codePointAt(0) == 10 || value.toString().codePointAt(0) == 13)}"><j:whitespace>&#10;</j:whitespace></j:if>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jelly doesn't accept backslashes (like \n)...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jelly and groovy has ability to unescape text, feel free to try.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried this in jelly

:\r\n
:${'\r\n'}
:${&quot;\r\n&quot;}
:<st:out value="\r\n" />
:<st:out value="${'\r\n'}" />
:<st:out value="${&quot;\r\n&quot;}" />

and that resulted like this:

:\r\n
:\r\n
:\r\n
:\r\n
:\r\n
:\r\n

backslashes doesn't seem handled.

Maybe I need another way to handle control characters.
Would you show me some examples to have jelly unescape texts?
I could not find good documentations for jelly...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documentation for Jelly is weak. Generally speaking if you are trying to do anything subtle you have to switch to Groovy to get more control.

<st:out value="${value}" />
</textarea>
<j:if test="${customizedFields != null and attrs.field != null and value != default}">
Expand Down
59 changes: 59 additions & 0 deletions test/src/test/groovy/lib/form/TextAreaTest.groovy
Expand Up @@ -14,6 +14,8 @@ import org.kohsuke.stapler.QueryParameter

import javax.inject.Inject

import static org.junit.Assert.*

/**
*
*
Expand Down Expand Up @@ -70,4 +72,61 @@ class TextAreaTest {
}

}

@Test
public void testText() {
def TEXT_TO_TEST = "some\nvalue\n";
def p = j.createFreeStyleProject();
def target = new TextareaTestBuilder(TEXT_TO_TEST);
p.buildersList.add(target);
j.configRoundtrip(p);
j.assertEqualDataBoundBeans(target, p.getBuildersList().get(TextareaTestBuilder.class));
}

@Issue("JENKINS-27505")
@Test
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW it would save test execution time to collapse these three test cases.

public void testTextBeginningWithEmptyLine() {
def TEXT_TO_TEST = "\nbegin\n\nwith\nempty\nline\n\n";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about two initial newlines?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(resolved)

def p = j.createFreeStyleProject();
def target = new TextareaTestBuilder(TEXT_TO_TEST);
p.buildersList.add(target);
j.configRoundtrip(p);
j.assertEqualDataBoundBeans(target, p.getBuildersList().get(TextareaTestBuilder.class));
}

@Issue("JENKINS-27505")
@Test
public void testTextBeginningWithTwoEmptyLines() {
def TEXT_TO_TEST = "\n\nbegin\n\nwith\ntwo\nempty\nline\n\n";
def p = j.createFreeStyleProject();
def target = new TextareaTestBuilder(TEXT_TO_TEST);
p.buildersList.add(target);
j.configRoundtrip(p);
j.assertEqualDataBoundBeans(target, p.getBuildersList().get(TextareaTestBuilder.class));
}

public static class TextareaTestBuilder extends Builder {
private text;

@DataBoundConstructor
TextareaTestBuilder(String text) {
this.text = text;
}

public String getText() { return text; }

@TestExtension
public static class DescriptorImpl extends BuildStepDescriptor<Builder> {
@Override
boolean isApplicable(Class<? extends AbstractProject> jobType) {
return true;
}

@Override
String getDisplayName() {
return this.class.name;
}
}

}
}
@@ -0,0 +1,7 @@
<!-- no config -->
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<f:entry field="text" title="${%Text}">
<f:textarea/>
</f:entry>
</j:jelly>