001 
002 /*
003  *  JScripter Standard 1.0 - To Script In Java
004  *  Copyright (C) 2008-2011  J.J.Liu<jianjunliu@126.com> <http://www.jscripter.org>
005  *  
006  *  This program is free software: you can redistribute it and/or modify
007  *  it under the terms of the GNU Affero General Public License as published by
008  *  the Free Software Foundation, either version 3 of the License, or
009  *  (at your option) any later version.
010  *  
011  *  This program is distributed in the hope that it will be useful,
012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014  *  GNU Affero General Public License for more details.
015  *  
016  *  You should have received a copy of the GNU Affero General Public License
017  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
018  */
019 
020 package js.user;
021 
022 import js.*;
023 import js.core.*;
024 
025 /**
026  * <p>An <b>opaque</b> class representing JavaScript client-side objects of the global 
027  * {@link JsClient#Text} class.</p>
028  * <p>A {@link JsText} node represents a run of plain text in an HTML or XML document. 
029  * Plain text appears within HTML and XML elements and attributes, and {@link JsText} 
030  * nodes typically appear as children of {@link JsElement} and {@link JsAttr} nodes. 
031  * {@link JsText} nodes inherit from {@link JsCharacterData}, and the textual content of 
032  * a {@link JsText} node is available through the {@link JsCharacterData#data} property 
033  * inherited from {@link JsCharacterData} or through the {@link JsNode#nodeValue} 
034  * property inherited from {@link JsNode}. {@link JsText} nodes may be manipulated using 
035  * any of the methods inherited from {@link JsCharacterData} or with the {@link #splitText(Number)} 
036  * method defined by {@link JsText} itself. Create a new {@link JsText} node with {@link JsDocument#createTextNode(String)}. 
037  * {@link JsText} nodes never have children.</p>
038  * <p>See {@link JsNode#normalize()} for a way to remove empty {@link JsText} nodes and 
039  * merge adjacent {@link JsText} nodes from a subtree of a document.</p>
040  *
041  * @author <a href="mailto:jianjunliu@126.com">J.J.Liu (Jianjun Liu)</a> at <a href="http://www.jscripter.org" target="_blank">http://www.jscripter.org</a>
042  * @see JsNode#normalize()
043  *
044  * @javascript <b>Opaque</b> types can be resolved but no class objects for them can be
045  * generated into the target codes. Re-compilers must exit with error on the operations of
046  * accessing that kind of class objects.
047  * The <tt>checkcast</tt> operation to the class literal of this interface must be ignored
048  * and <tt>instanceof</tt> to it always <tt>true</tt>.
049  */
050 public class JsText extends JsClient.Text.Prototype
051 {
052     /**
053      * <p>An <b>opaque</b> class representing members of its enclosing <b>opaque</b> type.</p>
054      * <p>Note that, this class is <b>opaque</b> but its constructors are all <b>internal</b>. 
055      * This class and the subclasses of this class are used to declare either <b>opaque</b> 
056      * <tt>public</tt> instance fields of the opaque type {@link js.Var.Member} or the 
057      * <b>opaque</b> <tt>public</tt> static fields of other <b>opaque</b> types while their 
058      * constructors are used to define the fields inside <b>opaque</b> classes. Under 
059      * either circumstance, the field names must be exactly same as the member names, as 
060      * the <b>opaque</b> fields of <b>opaque</b> types are resolved by re-compilers directly 
061      * based on the field names.</p>
062      *
063      * @author <a href="mailto:jianjunliu@126.com">J.J.Liu (Jianjun Liu)</a> at <a href="http://www.jscripter.org" target="_blank">http://www.jscripter.org</a>
064      * 
065      * @javascript <b>Opaque</b> types can be resolved but no class objects for them can be created
066      * in the target codes. Re-compilers must exit with error on operations accessing that kind 
067      * of class objects.
068      * Re-compilers must resolve an <b>opaque</b> instance field declared by this class in
069      * {@link js.Var.Member} or its subclasses to the JavaScript identifier: 
070      * <pre>q.m</pre>
071      * where <tt>m</tt> is the identifier of the field name and <tt>q</tt> is the identifier
072      * resolved from the instance of the enclosing member. Re-compilers must resolve an 
073      * <b>opaque</b> static field declared by this class in <b>opaque</b> types other than 
074      * {@link js.Var.Member} and its subclasses to the JavaScript identifier: 
075      * <pre>m</pre>
076      * where <tt>m</tt> is the identifier of the field name. And re-compilers must report
077      * error on the access to <b>opaque</b> fields declared by this class under any other 
078      * circumstances.
079      */
080     public static class Member extends JsClient.Text.Prototype.Member
081     {
082         /**
083          * <p>Internally constructs a member based on a qualifying member.</p>
084          * <p>This constructor is <b>internal</b> and only called inside of <b>opaque</b>
085          * or <b>internal</b> classes or class members.</p>
086          * <p>Note that, this constructor is <b>internal</b> but its declaring class is
087          * <b>opaque</b>. This constructor is used to define <b>opaque</b> instance fields 
088          * declared in the declaring class of this constructor itself or its subclasses. 
089          * Under this circumstance, the field names must be exactly same as the member 
090          * names, as the <b>opaque</b> instance fields of the <b>opaque</b> type 
091          * {@link js.Var.Member} or its subclasses are resolved by re-compilers directly
092          * to their names appending to the name resolved from the specified qualifying 
093          * member with a dot in between.</p>
094          * @param q A qualifying member
095          * @param mid The ID of the member to construct
096          * @since 1.0
097          * @javascript Re-compilers must report error on the invocation to an <b>internal</b> constructor.
098          */
099         public Member(JsObject.Member q, Mid mid) {
100             super(q, mid);
101         }
102         /**
103          * <p>Internally constructs a member without a qualifying member.</p>
104          * <p>This constructor is <b>internal</b> and only called inside of <b>opaque</b>
105          * or <b>internal</b> classes or class members.</p>
106          * <p>Note that, this constructor is <b>internal</b> but its declaring class is
107          * <b>opaque</b>. This constructor is used to define <b>opaque</b> static fields, 
108          * declared in <b>opaque</b> types other than the declaring class of this constructor 
109          * itself and its subclasses. Under this circumstance, the field names must be
110          * exactly same as the member names, as the <b>opaque</b> static fields of <b>opaque</b>
111          * types are generally resolved by re-compilers directly to identifiers of their names.</p>
112          * @param mid The ID of the member to construct
113          * @since 1.0
114          * @javascript Re-compilers must report error on the invocation to an <b>internal</b> constructor.
115          */
116         public Member(Mid mid) {
117             super(mid);
118         }
119         @Override
120         /**
121          * <p>Evaluates the property, represented by the current member instance, of the
122          * argument object.</p>
123          * @param o The argument object
124          * @return The value of the current member based on the object argument.
125          * @since 1.0
126          * @javascript Re-compilers must convert the instance invocation of this method into
127          * the JavaScript expression: 
128          * <pre>o.m</pre>
129          * where <tt>m</tt> is the identifier name resolved from the current member
130          * instance of the invocation.
131          */
132         public JsText with(ObjectLike o) {
133             return new JsText(super.with(o));
134         }
135     }
136 
137     /**
138      * <p>Casts an <b>opaque</b> object to the current <b>opaque</b> type by wrapping it
139      * with the wrapping constructor.</p>
140      * @param var The argument of an <b>opaque</b> object.
141      * @since 1.0
142      * @javascript Re-compilers must ignore the construction operation of this constructor,
143      * that is, replacing it with its only argument.
144      */
145     public JsText(JsObject var) {
146         super(var);
147     }
148 
149     @Override
150     /**
151      * <p>Returns the primitive value associated with the current instance, if there is one.
152      * This invocation simply returns the instance itself for the current instance is an 
153      * object and there is no primitive value for it.</p>
154      * @return The current object itself.
155      * @since 1.0
156      * @javascript Re-compilers must convert the instance invocation of this method directly
157      * into a JavaScript invocation on its current object instance without changing the 
158      * method name, but expanding variable arguments, if any, into comma-separated values. 
159      */
160     public JsText valueOf() {
161         return new JsText((JsObject)var().valueOf());
162     }
163 
164     /**
165      * <p>Splits this {@link JsText} node into two at the specified character position 
166      * and returns the new {@link JsText} node.</p>
167      * <p>This method splits a {@link JsText} node in two at the specified <tt>offset</tt>. 
168      * The original {@link JsText} node is modified so that it contains all text content 
169      * up to, but not including, the character at position <tt>offset</tt>. A new {@link JsText} 
170      * node is created to contain all the characters from and including the position <tt>offset</tt> 
171      * to the end of the string. The new {@link JsText} node is the return value of the 
172      * method. Additionally, if the original {@link JsText} node has a {@link JsNode#parentNode}, 
173      * the new node is inserted into this parent node immediately after the original node.</p>
174      * <p>{@link JsCDATASection} inherits from {@link JsText}. The newly created node of 
175      * {@link JsCDATASection#splitText(Number)} is a {@link JsCDATASection} node rather 
176      * than a {@link JsText} node.</p>
177      * @param offset The character position at which to split the {@link JsText} node.
178      * @return The {@link JsText} node that was split from this node.
179      * @throws RuntimeException JavaScript throws a a {@link JsDOMException} object with 
180      * the {@link JsDOMException#code} property of the value {@link JsDOMException#NO_MODIFICATION_ALLOWED_ERR} 
181      * if the node is read-only and may not be modified or the value {@link JsDOMException#INDEX_SIZE_ERR} 
182      * if <tt>offset</tt> is negative or is greater than the length of the {@link JsText} or 
183      * {@link JsComment} node. 
184      * See {@link Js#err(Object)} for JS Simulation.
185      * @since 1.0
186      * @see #splitText(NumberLike)
187      * @javascript Re-compilers must convert the instance invocation of this method directly
188      * into a JavaScript invocation on its current object instance without changing the 
189      * method name, but expanding variable arguments, if any, into comma-separated values. 
190      */
191     public JsText splitText(Number offset) {
192         return call(splitText, offset);
193     }
194     /**
195      * <p>Splits this {@link JsText} node into two at the specified character position 
196      * and returns the new {@link JsText} node.</p>
197      * <p>This method splits a {@link JsText} node in two at the specified <tt>offset</tt>. 
198      * The original {@link JsText} node is modified so that it contains all text content 
199      * up to, but not including, the character at position <tt>offset</tt>. A new {@link JsText} 
200      * node is created to contain all the characters from and including the position <tt>offset</tt> 
201      * to the end of the string. The new {@link JsText} node is the return value of the 
202      * method. Additionally, if the original {@link JsText} node has a {@link JsNode#parentNode}, 
203      * the new node is inserted into this parent node immediately after the original node.</p>
204      * <p>{@link JsCDATASection} inherits from {@link JsText}. The newly created node of 
205      * {@link JsCDATASection#splitText(NumberLike)} is a {@link JsCDATASection} node rather 
206      * than a {@link JsText} node.</p>
207      * @param offset The character position at which to split the {@link JsText} node.
208      * @return The {@link JsText} node that was split from this node.
209      * @throws RuntimeException JavaScript throws a a {@link JsDOMException} object with 
210      * the {@link JsDOMException#code} property of the value {@link JsDOMException#NO_MODIFICATION_ALLOWED_ERR} 
211      * if the node is read-only and may not be modified or the value {@link JsDOMException#INDEX_SIZE_ERR} 
212      * if <tt>offset</tt> is negative or is greater than the length of the {@link JsText} or 
213      * {@link JsComment} node. 
214      * See {@link Js#err(Object)} for JS Simulation.
215      * @since 1.0
216      * @see #splitText(Number)
217      * @javascript Re-compilers must convert the instance invocation of this method directly
218      * into a JavaScript invocation on its current object instance without changing the 
219      * method name, but expanding variable arguments, if any, into comma-separated values. 
220      */
221     public JsText splitText(NumberLike<?> offset) {
222         return splitText(Js.valueOf(offset));
223     }
224 }