package textbender.o.rhinohide.core; // Copyright 2006-2007, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Textbender Software"), to deal in the Textbender Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Textbender Software, and to permit persons to whom the Textbender Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Textbender Software. THE TEXTBENDER SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE TEXTBENDER SOFTWARE OR THE USE OR OTHER DEALINGS IN THE TEXTBENDER SOFTWARE. import java.util.concurrent.atomic.*; import netscape.javascript.*; import org.w3c.dom.*; import textbender.g.lang.*; import textbender.g.util.logging.*; import textbender.o.rhinohide.*; import textbender.o.rhinohide.html.*; /** An element implemented as an overlay of a JavaScript element. */ public @ThreadSafe class RhiElement extends RhiNode implements Element { /** Constructs a RhiElement. * * @param window global object * @param jsObject bridge to underlying JavaScript element, * per {@linkplain Rhinohide#jsObject() jsObject}() * * @return element, or null if jsObject is null * * @throws IllegalArgumentException if jsObject is neither null * nor an element */ public static RhiElement wrapElement( RhiWindow window, JSObject jsObject ) { return wrapElement( window, jsObject, /*doTypeCheck*/true ); } static RhiElement wrapElement( RhiWindow window, JSObject jsObject, boolean doTypeCheck ) { if( jsObject == null ) return null; if( doTypeCheck ) ensureNodeType( new Rhinohide(window,jsObject), ELEMENT_NODE ); RhiElement element = new RhiElement( window, jsObject ); if( element.getOwnerDocument().getImplementation().hasFeature( "HTML", ""/*any version*/ )) // OPT to do make a direct JavaScript call via jsObject bridge, instead of creating element and other nodes { element = new RhiHTMLElement( window, jsObject ); // on second thought } return element; } protected RhiElement( RhiWindow window, JSObject jsObject ) { super( window, jsObject ); } // - E l e m e n t -------------------------------------------------------------------- public final String getAttribute( String name ) { String value = (String)call( "getAttribute", name ); if( value == null ) { value = ""; // Should be this, not null. I think this is a bug on http://reluk.ca/system/host/obsidian/linux-build-2007-02.xht#firefox. if( !_getAttribute_wasLoggedNullA.getAndSet( true )) { LoggerX.i(getClass()).fine( "correcting null '" + name + "' attribute: " + this ); } } return value; } private static final AtomicBoolean _getAttribute_wasLoggedNullA = new AtomicBoolean(); public final String getAttributeNS( String namespaceURI, String localName ) { return (String)call( "getAttributeNS", namespaceURI, localName ); } public final void setAttribute( String name, String value ) { call( "setAttribute", name, value ); } public final void setAttributeNS( String namespaceURI, String qualifiedName, String value ) { call( "setAttributeNS", namespaceURI, qualifiedName, value ); } public final Attr getAttributeNode( String name ) { // RhiAttr attr = RhiAttr.wrapAttr // ( window, (JSObject)call( "getAttributeNode", name ), /*doTypeCheck*/false ); // if( OperatingSystem.isWindows() && attr.getValue().length() == 0 ) // IE bug // { // if( !_getAttributeNode_wasLoggedEmptyA.getAndSet( true )) // { // LoggerX.i(getClass()).fine( "correcting empty '" + name + "' attribute: " + this ); // } // attr = null; // cannot have empty attributes // } // return attr; return RhiAttr.wrapAttr ( window, (JSObject)call( "getAttributeNode", name ), /*doTypeCheck*/false ); } // // // // private static final AtomicBoolean _getAttributeNode_wasLoggedEmptyA = new AtomicBoolean(); public final Attr setAttributeNode( Attr newAttr ) { Object o = call( "setAttributeNode", toJSObject((RhiAttr)newAttr) ); if( o == null ) return null; else return newAttr; } /** Untested. */ public final Attr getAttributeNodeNS( String namespaceURI, String localName ) { return RhiAttr.wrapAttr ( window, (JSObject)call( "getAttributeNodeNS", namespaceURI, localName ), /*doTypeCheck*/false ); } /** Untested. */ public final Attr setAttributeNodeNS( Attr newAttr ) { Object o = call( "setAttributeNodeNS", toJSObject((RhiAttr)newAttr) ); if( o == null ) return null; else return newAttr; } public final NodeList getElementsByTagName( String name ) { return RhiNodeList.wrapNodeList ( window, (JSObject)call( "getElementsByTagName", name )); } /** Untested. */ public final NodeList getElementsByTagNameNS( String namespaceURI, String localName ) { return RhiNodeList.wrapNodeList ( window, (JSObject)call( "getElementsByTagNameNS", namespaceURI, localName )); } /** Not yet coded. * * @throws UnsupportedOperationException */ public final TypeInfo getSchemaTypeInfo() { throw new UnsupportedOperationException(); } public final String getTagName() { return (String)getMember( "tagName" ); } /** Not yet coded. * * @throws UnsupportedOperationException */ public final boolean hasAttribute( String name ) { throw new UnsupportedOperationException(); } /** Not yet coded. * * @throws UnsupportedOperationException */ public final boolean hasAttributeNS( String namespaceURI, String localName ) { throw new UnsupportedOperationException(); } public final void removeAttribute( String name ) { call( "removeAttribute", name ); } /** Untested. */ public final void removeAttributeNS( String namespaceURI, String localName ) { call( "removeAttributeNS", namespaceURI, localName ); } public final Attr removeAttributeNode( Attr oldAttr ) { callV( "removeAttributeNode", toJSObject((RhiNode)oldAttr) ); return oldAttr; } /** Not yet coded. * * @throws UnsupportedOperationException */ public final void setIdAttribute( String name, boolean isId ) { throw new UnsupportedOperationException(); } /** Not yet coded. * * @throws UnsupportedOperationException */ public final void setIdAttributeNS( String namespaceURI, String localName, boolean isId ) { throw new UnsupportedOperationException(); } /** Not yet coded. * * @throws UnsupportedOperationException */ public final void setIdAttributeNode( Attr idAttr, boolean isId ) { throw new UnsupportedOperationException(); } // - N o d e -------------------------------------------------------------------------- public @Override final String getLocalName() { return (String)getMemberV( "localName" ); } // overridden on assumption it is never null for an element }