1

Is there a way to wrap marked text in element using xforms? For example:

Put tag around this word --> Put tag around this <tag>word</tag>

I'm new to xforms, currently using Xsltxforms on exist-db server and I'm trying to figure out if I'm missing something or there's no way to make such a thing in textarea box for example. Thank you in advance!

nassoo
  • 59
  • 1
  • 7

2 Answers2

2

There is no mechanism in XForms Recommendation for such a processing.

But this has been added as an extension in XSLTForms: an extra action named "xf:wrap" allows to indicate with control is to be considered and what is to be added before and after the selection.

<?xml-stylesheet href="xsl/xsltforms.xsl" type="text/xsl"?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:xf="http://www.w3.org/2002/xforms" xmlns:ev="http://www.w3.org/2001/xml-events">
<head>
    <title>Wrap Selection</title>
    <xf:model>
        <xf:instance>
            <data xmlns="">Sed ut perspiciatis, unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam eaque ipsa, quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt, explicabo.</data>
        </xf:instance>
    </xf:model>
</head>
<body>
    <xf:trigger>
        <xf:label>&lt;a&gt;</xf:label>
        <xf:wrap ev:event="DOMActivate" control="t" pre="&lt;a&gt;" post="&lt;/a&gt;"/>
    </xf:trigger>
    <xf:trigger>
        <xf:label>&lt;b&gt;</xf:label>
        <xf:wrap ev:event="DOMActivate" control="t" pre="&lt;b&gt;" post="&lt;/b&gt;"/>
    </xf:trigger>
    <xf:trigger>
        <xf:label>&lt;c&gt;</xf:label>
        <xf:wrap ev:event="DOMActivate" control="t" pre="&lt;c&gt;" post="&lt;/c&gt;"/>
    </xf:trigger>
    <br/>
    <xf:textarea id="t" ref="." incremental="true"/>
    <br/>
    <xf:output value=".">
        <xf:label>Value: </xf:label>
    </xf:output>
</body>
</html>

There is yet another possibility with the xf:setselection action:

<?xml-stylesheet href="xsl/xsltforms.xsl" type="text/xsl"?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:xf="http://www.w3.org/2002/xforms" xmlns:ev="http://www.w3.org/2001/xml-events">
<head>
    <title>Set Selection</title>
    <xf:model>
        <xf:instance>
            <data xmlns="">Sed ut perspiciatis, unde omnis iste natus error sit voluptatem accusantium doloremque laudantium...</data>
        </xf:instance>
    </xf:model>
</head>
<body>
    <xf:trigger>
        <xf:label>&lt;span&gt;</xf:label>
        <xf:setselection ev:event="DOMActivate" control="t" value="concat('&lt;span start=&quot;', control-property('t', 'selectionStart'), '&quot; end=&quot;', control-property('t', 'selectionEnd'), '&quot;&gt;', selection('t'), '&lt;/span&gt;')"/>
    </xf:trigger>
    <br/>
    <xf:textarea id="t" ref="." incremental="true"/>
    <br/>
    <xf:output value=".">
        <xf:label>Value: </xf:label>
    </xf:output>
</body>
</html>

Live demo: http://www.agencexml.com/direct/wrap/setselection.xml

What do you think?

Alain Couthures
  • 1,488
  • 9
  • 5
  • Thank you, Alain! It seems to be exactly what I needed... but I'm not sure how to implement this functionality. When I try to open your file on exist-db server, it returns XSLTforms exception: Error initializing: xforms:wrap is not supported. I suppose I have to add some extra code to xsltforms.xsl to add this extra action (as you've written "an extension in XSLTForms")? – nassoo Mar 17 '16 at 08:21
  • 1
    @nassoo In eXist-db, you can delete the current contents of the `xsltforms` directory (using WebDAV or the eXist Java Admin Client). Then clone the latest XSLTForms code from GitHub and repopulate the `xsltforms` directory in eXist with the contents of the `build` directory in the XSLTForms repository. I assume you're using eXist 2.2? I don't think eXist 3.0 RC1 works with the latest versions of XSLTForms yet. – tat Mar 17 '16 at 16:49
2

Amended answer: Some possible solutions are to integrate a library such as Rangy (https://github.com/timdown/rangy) into XSLTForms, or even a rich-text editor such as TinyMCE (which XSLTForms does in this example: http://www.agencexml.com/xsltforms/tinymce.xml). Another option would be to look at Teian(https://sourceforge.net/projects/teian/) which is designed for TEI, but could be a step in the right direction.

wsalesky
  • 55
  • 5