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

form:textarea doesn't retain linebreaks #2

Open
makenosound opened this issue Sep 30, 2009 · 9 comments
Open

form:textarea doesn't retain linebreaks #2

makenosound opened this issue Sep 30, 2009 · 9 comments

Comments

@makenosound
Copy link

Nick, started using this for a new project. Really great stuff.

I've come across one limitation thus far which is that the form:textarea template doesn't retain the linebreaks when populating the value. It's happening because of the normalize-space functions use there, and is easily fixed by changing:

<xsl:variable name="initial-value" select="normalize-space($value)"/>

to:

<xsl:variable name="initial-value" select="$value"/>

However that causes there to be a discrepancy between the $postback-value and the $initial-value. Not sure of the best way to get around that—perhaps adding a param to the form:postback-value template to allow the non-normalised value to be returned?

@nickdunn
Copy link
Owner

Good bug! What if the normalized versions were used only for the comparisons?

A new textarea template with both original and normalized variables:

<xsl:template name="form:textarea">
    <xsl:param name="handle"/>
    <xsl:param name="value"/>
    <xsl:param name="class"/>
    <xsl:param name="title"/>
    <xsl:param name="rows"/>
    <xsl:param name="cols"/>
    <xsl:param name="section" select="'fields'"/>
    <xsl:param name="event" select="$form:event"/>

    <xsl:variable name="initial-value" select="$value"/>
    <xsl:variable name="initial-value-normalized" select="normalize-space($value)"/>

    <xsl:variable name="postback-value">
        <xsl:call-template name="form:postback-value">
            <xsl:with-param name="event" select="$event"/>
            <xsl:with-param name="handle" select="$handle"/>
            <xsl:with-param name="section" select="$section"/>
            <xsl:with-param name="normalize" select="'no'"/>
        </xsl:call-template>
    </xsl:variable>

    <xsl:variable name="postback-value-normalized">
        <xsl:call-template name="form:postback-value">
            <xsl:with-param name="event" select="$event"/>
            <xsl:with-param name="handle" select="$handle"/>
            <xsl:with-param name="section" select="$section"/>
        </xsl:call-template>
    </xsl:variable>

    <xsl:element name="textarea" use-attribute-sets="form:attributes-general">

        <xsl:if test="$rows">
            <xsl:attribute name="rows"><xsl:value-of select="$rows"/></xsl:attribute>
        </xsl:if>

        <xsl:if test="$cols">
            <xsl:attribute name="cols"><xsl:value-of select="$cols"/></xsl:attribute>
        </xsl:if>

        <xsl:choose>
            <xsl:when test="$event and ($initial-value-normalized != $postback-value-normalized)">
                <xsl:value-of select="$postback-value"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$initial-value"/>
            </xsl:otherwise>
        </xsl:choose>

    </xsl:element>

</xsl:template>

And the postback template modified to allow normalization to be set to "no" (is "yes" by default):

<!--
Name: form:postback-value
Description: determines the postback value of a control if an Event has been triggered
Returns: string
-->
<xsl:template name="form:postback-value">
    <xsl:param name="handle"/>
    <xsl:param name="section"/>
    <xsl:param name="normalize" select="'yes'"/>
    <xsl:param name="event" select="$form:event"/>

    <xsl:variable name="index-key">
        <xsl:call-template name="form:section-index-key">
            <xsl:with-param name="section" select="$section"/>
        </xsl:call-template>
    </xsl:variable>

    <xsl:variable name="section-handle">
        <xsl:call-template name="form:section-handle">
            <xsl:with-param name="section" select="$section"/>
        </xsl:call-template>
    </xsl:variable>

    <xsl:choose>
        <xsl:when test="$section!='fields' and $index-key!=''">
            <xsl:choose>
                <xsl:when test="$normalize='yes'">
                    <xsl:value-of select="normalize-space($event/entry[@section-handle=$section-handle and @index-key=$index-key]/post-values/*[name()=$handle])"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$event/entry[@section-handle=$section-handle and @index-key=$index-key]/post-values/*[name()=$handle]"/>
                </xsl:otherwise>
            </xsl:choose>           
        </xsl:when>
        <xsl:when test="$section!='fields'">
            <xsl:choose>
                <xsl:when test="$normalize='yes'">
                    <xsl:value-of select="normalize-space($event/entry[@section-handle=$section-handle]/post-values/*[name()=$handle])"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$event/entry[@section-handle=$section-handle]/post-values/*[name()=$handle]"/>
                </xsl:otherwise>
            </xsl:choose>           
        </xsl:when>
        <xsl:otherwise>
            <xsl:for-each select="$event/post-values/*[name()=$handle]">
                <xsl:choose>
                    <xsl:when test="$normalize='yes'">
                        <value><xsl:value-of select="normalize-space(.)"/></value>
                    </xsl:when>
                    <xsl:otherwise>
                        <value><xsl:value-of select="."/></value>
                    </xsl:otherwise>
                </xsl:choose>               
            </xsl:for-each>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

Does that do the trick? If I'm honest, I can't really remember why I normalize in the first place...

@makenosound
Copy link
Author

Nice one—I think that should do the trick. My only worry is if you're comparing the two normalised values for deciding whether the $postback value has changed then you might think they're the same when they're not. For example:

Hello, World!

Would normalise to the same as:

Hello,
World!

Right? I imagine you were normalising as a "just in case" check, it's certainly possible that a textformatter would introduce some difference in whitespace while passing the initial value into the form. Perhaps we could include the same param in the form:textarea template to allow whether to set normalise or not. What do you reckon?

@michael-e
Copy link

If I'm honest, I can't really remember why I normalize in the first place...

I can't imagine any reason to do this in form-controls.

@nickdunn
Copy link
Owner

It is definitely required. It has been there since the very first version (I've been using various versions of this for 18 months) and is required certainly for select boxes. Perhaps it was if the HTML contained breaks:

<option>
  Hello World
<option>

I suppose the only field that needs this is the textarea since it's the only one that accepts multi-line input. So it shouldn't really be an option, but hidden complexity within the template that the developer need not worry about.

Max, if you remove the normalization for the textarea, does it still work? I'm trying to remember the use cases where I needed normalize-space for desired behaviour.

@makenosound
Copy link
Author

Yup, still works. I have mine setup to normalize for the check if $postback-value = $initial-value but the $initial-value I'm outputting is raw (not normalized).

@nickdunn
Copy link
Owner

nickdunn commented Oct 7, 2009

Cool. Do you think it's an acceptable compromise to go into the next release for textareas?

@makenosound
Copy link
Author

For sure. Integrate away.

@rainerborene
Copy link

This bug were fixed and integrated?

@nickdunn
Copy link
Owner

Tested locally, but never pushed to Github. It's on my to-do list :-)

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

No branches or pull requests

4 participants