karthick
karthick

Reputation: 12176

What is the suitable replacement for this.__LZtextclip.text in Open laszlo 5.0

I want to know what is the suitable replacement for this line.

this.__LZtextclip.text

I am using this to get the string present in the text node. This works fine in Openlaszlo 3.3 but in 4.9 and 5.0 it's giving a problem

I tried updating it to

this.sprite.__LZtextclip.text

And i am getting an error:

79: Error: Access of possibly undefined property __LZtextclip through a reference with static type LzSprite, in line: Debug.write(this.sprite.__LZtextclip.text);

Any idea why this problem is happening?

Upvotes: 2

Views: 153

Answers (2)

raju-bitter
raju-bitter

Reputation: 8996

If you are trying to access the text content of a text field, why don't you just access the attribute text?

<canvas> 

    <text name="sample" id="gRead" />

    <handler name="oninit">
      gRead.setAttribute('text',"HI");
      Debug.info(gRead.text);
    </handler>

</canvas>

In OpenLaszlo 3.3 there is method getText(), which gives you the same value. Accessing mx.textfield in your code does not work for the DHTML runtime.

Edit: Added information regarding the stripping of HTML tags
The Flash Textfield class flash.text.Textfield provides an API to enable HTML tag content in a Textfield instance. There are two different properties, one called text, the other one htmlText. If you want to directly access the Flash Textfield object of an lz.text instance, it's a property of the display object of the lz.text instance:

// Flash Textfield instance
gRead.getDisplayObject().textfield
// Pure text content
gRead.getDisplayObject().textfield.text
// Formatted text
gRead.getDisplayObject().textfield.htmlText

You should be aware of the fact that Flash automatically adds HTML format to any textstring you set as content. When you do

gRead.setAttribute('text',"HI");

the textfield.htmlText value is

<P ALIGN="LEFT"><FONT FACE="Verdana" SIZE="11" COLOR="#000000" LETTERSPACING="0" KERNING="1">HI</FONT></P> 

For the DHTML runtime, the text content is added as the innerHTML of a <div> tag, and there is no standardized API to retrieve the pure text content of a DOM structure for a tag with content. You could write your own function to extract the text content, or use JavaScript functions from existing frameworks - like the jQuery text() function - to achieve the same result for the DHTML runtime.

I guess the reason is that Laszlo started using the Dojo based rich text editor for text input with HTML formatting since OpenLaszlo 4.0 or 4.1.

The best approach to have consistent behavior across runtimes when stripping tags is to do the conversion on the server-side. That's especially needed if you wan to have consistent whitespace treatment in multiline text, since there differences in how browsers treat whitespace. The question how to best strip tags from strings in JavaScript has been answered before on Stackoverflow, e.g. JavaScript: How to strip HTML tags from string?

Here is a cross-runtime example which works in DHTML with Firefox, Chrome, and it should work with IE9+:

<canvas> 

    <text name="sample" id="gRead" />

    <handler name="oninit"><![CDATA[
      gRead.setAttribute("text", 'Hello <b>World</b> <a href="http://www.openlaszlo.org">OL</a>');
      Debug.info("gRead.text=" + gRead.text);
      if ($dhtml) {
        Debug.info(gRead.getDisplayObject().textContent);
      } else {
        Debug.info(gRead.getDisplayObject().textfield.text);
      }
    ]]></handler>

</canvas>

Upvotes: 2

karthick
karthick

Reputation: 12176

I found what is the problem. The problem is that i have to declare a variable and have to refer the property from that.

<canvas> 
<library>
    <text name="sample" id="gRead">         
        <method name="getTextFrom">
            Debug.write("this.text" , this.sprite);
            var mx = this.sprite;           
            Debug.write("this.text" , mx.textfield.text);

        </method>
    </text>

</library>
<handler name="oninit">
    gRead.setAttribute('text',"HI");
    gRead.getTextFrom();

</handler>
</canvas>

Upvotes: 1

Related Questions