| 3987 | } |
| 3988 | |
| 3989 | static xmlAttrPtr |
| 3990 | xmlCopyPropInternal(xmlDocPtr doc, xmlNodePtr target, xmlAttrPtr cur) { |
| 3991 | xmlAttrPtr ret = NULL; |
| 3992 | |
| 3993 | if (cur == NULL) return(NULL); |
| 3994 | if ((target != NULL) && (target->type != XML_ELEMENT_NODE)) |
| 3995 | return(NULL); |
| 3996 | if (target != NULL) |
| 3997 | ret = xmlNewDocProp(target->doc, cur->name, NULL); |
| 3998 | else if (doc != NULL) |
| 3999 | ret = xmlNewDocProp(doc, cur->name, NULL); |
| 4000 | else if (cur->parent != NULL) |
| 4001 | ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL); |
| 4002 | else if (cur->children != NULL) |
| 4003 | ret = xmlNewDocProp(cur->children->doc, cur->name, NULL); |
| 4004 | else |
| 4005 | ret = xmlNewDocProp(NULL, cur->name, NULL); |
| 4006 | if (ret == NULL) return(NULL); |
| 4007 | ret->parent = target; |
| 4008 | |
| 4009 | if ((cur->ns != NULL) && (target != NULL)) { |
| 4010 | xmlNsPtr ns; |
| 4011 | int res; |
| 4012 | |
| 4013 | res = xmlSearchNsSafe(target, cur->ns->prefix, &ns); |
| 4014 | if (res < 0) |
| 4015 | goto error; |
| 4016 | if (ns == NULL) { |
| 4017 | /* |
| 4018 | * Humm, we are copying an element whose namespace is defined |
| 4019 | * out of the new tree scope. Search it in the original tree |
| 4020 | * and add it at the top of the new tree |
| 4021 | */ |
| 4022 | res = xmlSearchNsSafe(cur->parent, cur->ns->prefix, &ns); |
| 4023 | if (res < 0) |
| 4024 | goto error; |
| 4025 | if (ns != NULL) { |
| 4026 | xmlNodePtr root = target; |
| 4027 | xmlNodePtr pred = NULL; |
| 4028 | |
| 4029 | while (root->parent != NULL) { |
| 4030 | pred = root; |
| 4031 | root = root->parent; |
| 4032 | } |
| 4033 | if (root == (xmlNodePtr) target->doc) { |
| 4034 | /* correct possibly cycling above the document elt */ |
| 4035 | root = pred; |
| 4036 | } |
| 4037 | ret->ns = xmlNewNs(root, ns->href, ns->prefix); |
| 4038 | if (ret->ns == NULL) |
| 4039 | goto error; |
| 4040 | } |
| 4041 | } else { |
| 4042 | /* |
| 4043 | * we have to find something appropriate here since |
| 4044 | * we can't be sure, that the namespace we found is identified |
| 4045 | * by the prefix |
| 4046 | */ |
no test coverage detected