@@ -203,19 +203,16 @@ class NodeImpl extends EventTargetImpl {
return domSymbolTree . previousSibling ( this ) ;
}
insertBefore ( newChild , refChild ) {
if ( refChild === undefined ) {
refChild = null ;
insertBefore ( newChildImpl , refChildImpl ) {
if ( refChildImpl === undefined ) {
refChildImpl = null ;
}
const newChildImpl = idlUtils . implForWrapper ( newChild ) ;
const refChildImpl = refChild ? idlUtils . implForWrapper ( refChild ) : null ;
// TODO branding
if ( ! newChild || ! ( "nodeType" in newChild ) ) {
if ( ! newChildImpl || ! ( newChildImpl instanceof NodeImpl ) ) {
throw new TypeError ( "First argument to Node.prototype.insertBefore must be a Node" ) ;
}
if ( refChild !== null && ! ( "nodeType" in refChild ) ) {
if ( refChildImpl !== null && ! ( refChildImpl instanceof NodeImpl ) ) {
throw new TypeError ( "Second argument to Node.prototype.insertBefore must be a Node or null or undefined" ) ;
}
@@ -224,16 +221,16 @@ class NodeImpl extends EventTargetImpl {
}
// DocumentType must be implicitly adopted
if ( newChild . nodeType === NODE_TYPE . DOCUMENT_TYPE_NODE ) {
newChild . _ownerDocument = this . _ownerDocument ;
if ( newChildImpl . nodeType === NODE_TYPE . DOCUMENT_TYPE_NODE ) {
newChildImpl . _ownerDocument = this . _ownerDocument ;
}
// TODO - if (!newChild) then?
if ( this . nodeName !== "#document" && idlUtils . implForWrapper ( newChild ) . _ownerDocument !== this . _ownerDocument ) {
if ( this . nodeName !== "#document" && newChildImpl . _ownerDocument !== this . _ownerDocument ) {
throw new DOMException ( DOMException . WRONG_DOCUMENT_ERR ) ;
}
if ( newChild . nodeType && newChild . nodeType === NODE_TYPE . ATTRIBUTE_NODE ) {
if ( newChildImpl . nodeType && newChildImpl . nodeType === NODE_TYPE . ATTRIBUTE_NODE ) {
throw new DOMException ( DOMException . HIERARCHY_REQUEST_ERR ) ;
}
@@ -245,22 +242,22 @@ class NodeImpl extends EventTargetImpl {
}
// fragments are merged into the element (except parser-created fragments in <template>)
if ( newChild . nodeType === NODE_TYPE . DOCUMENT_FRAGMENT_NODE ) {
let grandChild ;
while ( ( grandChild = domSymbolTree . firstChild ( newChildImpl ) ) ) {
newChild . removeChild ( idlUtils . wrapperForImpl ( grandChild ) ) ;
this . insertBefore ( idlUtils . wrapperForImpl ( grandChild ) , refChild ) ;
if ( newChildImpl . nodeType === NODE_TYPE . DOCUMENT_FRAGMENT_NODE ) {
let grandChildImpl ;
while ( ( grandChildImpl = domSymbolTree . firstChild ( newChildImpl ) ) ) {
newChildImpl . removeChild ( grandChildImpl ) ;
this . insertBefore ( grandChildImpl , refChildImpl ) ;
}
} else if ( newChild === refChild ) {
return newChild ;
} else if ( newChildImpl === refChildImpl ) {
return newChildImpl ;
} else {
const oldParent = domSymbolTree . parent ( newChildImpl ) ;
const oldParentImpl = domSymbolTree . parent ( newChildImpl ) ;
// if the newChild is already in the tree elsewhere, remove it first
if ( oldParent ) {
oldParent . removeChild ( newChild ) ;
if ( oldParentImpl ) {
oldParentImpl . removeChild ( newChildImpl ) ;
}
if ( refChild === null ) {
if ( refChildImpl === null ) {
domSymbolTree . appendChild ( this , newChildImpl ) ;
} else {
if ( domSymbolTree . parent ( refChildImpl ) !== this ) {
@@ -272,26 +269,29 @@ class NodeImpl extends EventTargetImpl {
this . _modified ( ) ;
if ( this . _attached && newChild . _attach ) {
newChild . _attach ( ) ;
if ( this . _attached && newChildImpl . _attach ) {
newChildImpl . _attach ( ) ;
}
this . _descendantAdded ( this , newChild ) ;
this . _descendantAdded ( this , newChildImpl ) ;
}
if ( mutationEventsEnabled ( this ) ) {
const doc = getDocument ( this ) ;
let ev = doc . createEvent ( "MutationEvents" ) ;
let ev = idlUtils . implForWrapper ( doc . createEvent ( "MutationEvents" ) ) ;
if ( ev === undefined ) {
console . log ( this )
}
ev . initMutationEvent ( "DOMNodeInserted" , true , false , this , null , null , null , null ) ;
newChild . dispatchEvent ( ev ) ;
newChildImpl . dispatchEvent ( ev ) ;
ev = doc . createEvent ( "MutationEvents" ) ;
ev = idlUtils . implForWrapper ( doc . createEvent ( "MutationEvents" ) ) ;
ev . initMutationEvent ( "DOMSubtreeModified" , true , false , this , null , null , null , null ) ;
this . dispatchEvent ( ev ) ;
if ( this . nodeType === NODE_TYPE . DOCUMENT_NODE || this . _attachedToDocument ) {
ev = doc . createEvent ( "MutationEvents" ) ;
ev = idlUtils . implForWrapper ( doc . createEvent ( "MutationEvents" ) ) ;
ev . initMutationEvent ( "DOMNodeInsertedIntoDocument" , false , false , null , null , null , null , null ) ;
for ( const el of domSymbolTree . treeIterator ( newChildImpl ) ) {
@@ -303,7 +303,7 @@ class NodeImpl extends EventTargetImpl {
}
}
return newChild ;
return newChildImpl ;
} // raises(DOMException);
_modified ( ) {
@@ -422,7 +422,7 @@ class NodeImpl extends EventTargetImpl {
// TODO remove MutationEvents completely at some point
if ( value !== oldValue && this . _ownerDocument &&
this . _ownerDocument . implementation . _hasFeature ( "MutationEvents" ) ) {
const ev = this . _ownerDocument . createEvent ( "MutationEvents" ) ;
const ev = idlUtils . implForWrapper ( this . _ownerDocument . createEvent ( "MutationEvents" ) ) ;
let attrChange = MutationEvent . MODIFICATION ;
if ( value === null ) {
@@ -443,7 +443,7 @@ class NodeImpl extends EventTargetImpl {
if ( mutationEventsEnabled ( this ) && value !== oldValue ) {
const doc = getDocument ( this ) ;
const ev = doc . createEvent ( "MutationEvents" ) ;
const ev = idlUtils . implForWrapper ( doc . createEvent ( "MutationEvents" ) ) ;
ev . initMutationEvent ( "DOMSubtreeModified" , true , false , this , null , null , null , null ) ;
this . dispatchEvent ( ev ) ;
@@ -455,10 +455,10 @@ class NodeImpl extends EventTargetImpl {
throw new TypeError ( "Not enough arguments to Node.prototype.replaceChild" ) ;
}
// TODO branding
if ( ! node || ! ( "nodeType" in node ) ) {
if ( ! node || ! ( node instanceof NodeImpl ) ) {
throw new TypeError ( "First argument to Node.prototype.replaceChild must be a Node" ) ;
}
if ( ! child || ! ( "nodeType" in child ) ) {
if ( ! child || ! ( child instanceof NodeImpl ) ) {
throw new TypeError ( "Second argument to Node.prototype.replaceChild must be a Node" ) ;
}
@@ -497,46 +497,45 @@ class NodeImpl extends EventTargetImpl {
}
}
removeChild ( /* Node */ oldChild ) {
removeChild ( /* Node */ oldChildImpl ) {
if ( this . _readonly === true ) {
throw new DOMException ( DOMException . NO_MODIFICATION_ALLOWED_ERR ) ;
}
const oldChildImpl = idlUtils . implForWrapper ( oldChild ) ;
if ( ! oldChild || domSymbolTree . parent ( oldChildImpl ) !== this ) {
if ( ! oldChildImpl || domSymbolTree . parent ( oldChildImpl ) !== this ) {
throw new DOMException ( DOMException . NOT_FOUND_ERR ) ;
}
if ( mutationEventsEnabled ( this ) ) {
const doc = getDocument ( this ) ;
let ev = doc . createEvent ( "MutationEvents" ) ;
let ev = idlUtils . implForWrapper ( doc . createEvent ( "MutationEvents" ) ) ;
ev . initMutationEvent ( "DOMNodeRemoved" , true , false , this , null , null , null , null ) ;
oldChild . dispatchEvent ( ev ) ;
oldChildImpl . dispatchEvent ( ev ) ;
ev = doc . createEvent ( "MutationEvents" ) ;
ev = idlUtils . implForWrapper ( doc . createEvent ( "MutationEvents" ) ) ;
ev . initMutationEvent ( "DOMSubtreeModified" , true , false , this , null , null , null , null ) ;
this . dispatchEvent ( ev ) ;
ev = doc . createEvent ( "MutationEvents" ) ;
ev = idlUtils . implForWrapper ( doc . createEvent ( "MutationEvents" ) ) ;
ev . initMutationEvent ( "DOMNodeRemovedFromDocument" , false , false , null , null , null , null , null ) ;
for ( const el of domSymbolTree . treeIterator ( oldChild ) ) {
for ( const el of domSymbolTree . treeIterator ( oldChildImpl ) ) {
if ( el . nodeType === NODE_TYPE . ELEMENT_NODE ) {
el . dispatchEvent ( ev ) ;
el . _attachedToDocument = false ;
}
}
}
const oldPreviousSibling = oldChild . previousSibling ;
const oldPreviousSibling = oldChildImpl . previousSibling ;
domSymbolTree . remove ( oldChildImpl ) ;
this . _modified ( ) ;
oldChild . _detach ( ) ;
this . _descendantRemoved ( this , oldChild ) ;
oldChildImpl . _detach ( ) ;
this . _descendantRemoved ( this , oldChildImpl ) ;
if ( this . _ownerDocument ) {
this . _ownerDocument . _runRemovingSteps ( oldChild , this , oldPreviousSibling ) ;
this . _ownerDocument . _runRemovingSteps ( oldChildImpl , this , oldPreviousSibling ) ;
}
return oldChild ;
return oldChildImpl ;
} // raises(DOMException);
appendChild ( newChild ) {
@@ -560,7 +559,7 @@ class NodeImpl extends EventTargetImpl {
// Level2/core clean off empty nodes
if ( child . nodeValue === "" ) {
this . removeChild ( idlUtils . wrapperForImpl ( child ) ) ;
this . removeChild ( child ) ;
continue ;
}
@@ -569,7 +568,7 @@ class NodeImpl extends EventTargetImpl {
if ( prevChild && prevChild . nodeType === NODE_TYPE . TEXT_NODE && child . nodeType === NODE_TYPE . TEXT_NODE ) {
// merge text nodes
prevChild . appendData ( child . nodeValue ) ;
this . removeChild ( idlUtils . wrapperForImpl ( child ) ) ;
this . removeChild ( child ) ;
}
}
}
@@ -597,20 +596,19 @@ class NodeImpl extends EventTargetImpl {
return documentBaseURL ( this . _ownerDocument ) ;
}
compareDocumentPosition ( other ) {
compareDocumentPosition ( otherImpl ) {
// Let reference be the context object.
const reference = this ;
const otherImpl = idlUtils . implForWrapper ( other ) ;
if ( ! ( otherImpl instanceof NodeImpl ) ) {
throw new Error ( "Comparing position against non-Node values is not allowed" ) ;
}
if ( isObsoleteNodeType ( reference ) || isObsoleteNodeType ( other ) ) {
if ( isObsoleteNodeType ( reference ) || isObsoleteNodeType ( otherImpl ) ) {
throw new Error ( "Obsolete node type" ) ;
}
const result = domSymbolTree . compareTreePosition ( reference , other ) ;
const result = domSymbolTree . compareTreePosition ( reference , otherImpl ) ;
// “If other and reference are not in the same tree, return the result of adding DOCUMENT_POSITION_DISCONNECTED,
// DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC, and either DOCUMENT_POSITION_PRECEDING or
@@ -626,7 +624,7 @@ class NodeImpl extends EventTargetImpl {
}
contains ( other ) {
return Boolean ( idlUtils . implForWrapper ( other ) instanceof NodeImpl &&
return Boolean ( other instanceof NodeImpl &&
( this === other || this . compareDocumentPosition ( other ) & NODE_DOCUMENT_POSITION . DOCUMENT_POSITION_CONTAINED_BY ) ) ;
}