NAN_METHOD(XmlDocument::Root)
{
    Nan::HandleScope scope;
    XmlDocument* document = Nan::ObjectWrap::Unwrap<XmlDocument>(info.Holder());
    assert(document);

    xmlNode* root = xmlDocGetRootElement(document->xml_obj);

    if (info.Length() == 0 || info[0]->IsUndefined())
    {
        if (!root) {
            return info.GetReturnValue().Set(Nan::Null());
        }
        return info.GetReturnValue().Set(XmlElement::New(root));
    }

    if (root != NULL) {
        return Nan::ThrowError("Holder document already has a root node");
    }

    // set the element as the root element for the document
    // allows for proper retrieval of root later
    XmlElement* element = Nan::ObjectWrap::Unwrap<XmlElement>(Nan::To<v8::Object>(info[0]).ToLocalChecked());
    assert(element);
    xmlDocSetRootElement(document->xml_obj, element->xml_obj);
    element->ref_wrapped_ancestor();
    return info.GetReturnValue().Set(info[0]);
}
NAN_METHOD(XmlDocument::Root)
{
    Nan::HandleScope scope;
    XmlDocument* document = Nan::ObjectWrap::Unwrap<XmlDocument>(info.Holder());
    assert(document);

    xmlNode* root = xmlDocGetRootElement(document->xml_obj);

    if (info.Length() == 0 || info[0]->IsUndefined())
    {
        if (!root) {
            return info.GetReturnValue().Set(Nan::Null());
        }
        return info.GetReturnValue().Set(XmlElement::New(root));
    }

    if (root != NULL) {
        return Nan::ThrowError("Holder document already has a root node");
    }

     
     
    XmlElement* element = Nan::ObjectWrap::Unwrap<XmlElement>(Nan::To<v8::Object>(info[0]).ToLocalChecked());
    assert(element);
    xmlDocSetRootElement(document->xml_obj, element->xml_obj);
    element->ref_wrapped_ancestor();
    return info.GetReturnValue().Set(info[0]);
}