Skip to content

Commit

Permalink
Merge pull request #2134 from vishalduggal/timob-8952
Browse files Browse the repository at this point in the history
[TIMOB-8952]iOS XML: appendChild Removes Namespace and Over Validation
  • Loading branch information
srahim committed May 3, 2012
2 parents 848d711 + cf227a7 commit c8b4ff5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 34 deletions.
82 changes: 48 additions & 34 deletions iphone/Classes/TiDOMElementProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ -(void)setAttributeNS:(id)args
}
}
else {
GDataXMLNode* resultNode = (GDataXMLNode*)[GDataXMLElement attributeWithName:localName stringValue:val];
[element releaseCachedValues];
xmlNodePtr curNode = [element XMLNode];

xmlChar *href;
xmlChar *pre;
Expand All @@ -224,8 +225,9 @@ -(void)setAttributeNS:(id)args

xmlNsPtr theNewNs = xmlNewNs(NULL, // parent node
href, pre);
[resultNode XMLNode]->ns = theNewNs;
[element addAttribute: resultNode];
xmlNewNsProp(curNode, theNewNs, (xmlChar*)[localName UTF8String], (xmlChar*)[val UTF8String]);


}
}

Expand Down Expand Up @@ -429,6 +431,7 @@ -(id)setAttributeNodeNS:(id)args
[self throwException:@"mismatched documents" subreason:nil location:CODELOCATION];
return [NSNull null];
}

GDataXMLNode * attributeNode = [element attributeForLocalName:[GDataXMLNode localNameForName:name] URI:theURI];
if (attributeNode != nil) {
[attributeNode retain];
Expand Down Expand Up @@ -458,9 +461,16 @@ -(id)setAttributeNodeNS:(id)args
if(oldNodePtr != NULL) {
[TiDOMNodeProxy removeNodeForXMLNode:oldNodePtr];
}
//This adds by copying
[element addAttribute: [attProxy node]];
attributeNode = [element attributeForName:name];
//Duplicate methodology in setAttributeNS
[element releaseCachedValues];
xmlNodePtr curNode = [element XMLNode];
xmlNodePtr curAttr = [[attProxy node] XMLNode];
xmlNsPtr theNewNs = xmlCopyNamespace(curAttr->ns);
NSString* localName = [GDataXMLNode localNameForName:name];
NSString* val = [[attProxy node] stringValue];

xmlNewNsProp(curNode, theNewNs, (xmlChar*)[localName UTF8String], (xmlChar*)[val UTF8String]);
attributeNode = [element attributeForLocalName:localName URI:theURI];
[attProxy setNode:attributeNode];
[attProxy setAttribute:[attributeNode name] value:[attributeNode stringValue] owner:element];
[TiDOMNodeProxy setNode:attProxy forXMLNode:[attributeNode XMLNode]];
Expand Down Expand Up @@ -663,34 +673,38 @@ -(id)removeChild:(id)args

-(id)appendChild:(id)args
{
ENSURE_SINGLE_ARG(args, TiDOMNodeProxy);
TiDOMNodeProxy * newChild = (TiDOMNodeProxy*)args;
xmlNodePtr oldNodePtr = [[newChild node]XMLNode];
GDataXMLNode* resultElement = [element addChild:[newChild node]];

if (resultElement != nil)
{
//No longer part of tree set to free node since add child adds by creating copy
[[newChild node]setShouldFreeXMLNode:YES];
if (oldNodePtr != NULL)
{
[TiDOMNodeProxy removeNodeForXMLNode:oldNodePtr];
}
if ([newChild isKindOfClass:[TiDOMElementProxy class]])
{
[(TiDOMElementProxy*)newChild setElement:(GDataXMLElement*)resultElement];
}
else
{
[newChild setNode:resultElement];
}
[TiDOMNodeProxy setNode:newChild forXMLNode:[resultElement XMLNode]];
return newChild;
}
else
{
return [NSNull null];
}
ENSURE_SINGLE_ARG(args, TiDOMNodeProxy);
TiDOMNodeProxy * newChild = (TiDOMNodeProxy*)args;
xmlNodePtr oldNodePtr = [[newChild node] XMLNode];
xmlNodePtr parent = [element XMLNode];
xmlNodePtr resultPtr = xmlAddChild(parent, oldNodePtr);

if (resultPtr != NULL) {
[[self node] releaseCachedValues];
//Child added successfully
if (resultPtr == oldNodePtr) {
//Child pointer not modified
[[newChild node] setShouldFreeXMLNode:NO];
return newChild;
}
else {
//Child pointer modified
[[newChild node] setShouldFreeXMLNode:YES];
if (oldNodePtr != NULL) {
[TiDOMNodeProxy removeNodeForXMLNode:oldNodePtr];
}
TiDOMNodeProxy* result = [TiDOMNodeProxy nodeForXMLNode:resultPtr];
if (result == nil) {
GDataXMLNode * resultNode = [GDataXMLNode nodeBorrowingXMLNode:resultPtr];
id context = ([self executionContext]==nil) ? [self pageContext] : [self executionContext];
result = [self makeNode:resultNode context:context];
}
return result;
}
}
else {
return [NSNull null];
}
}

-(id)attributes
Expand Down
7 changes: 7 additions & 0 deletions iphone/Classes/TiDOMNodeProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ -(void)validateAttributeParameters:(NSString*)tagName withUri:(NSString*)theURI
return;
}
}
else if ( [prefix isEqualToString:@"xmlns"] ) {
if (![theURI isEqualToString:@"http://www.w3.org/2000/xmlns/"]) {
*error = @"Invalid URI for prefix";
*suberror = [NSString stringWithFormat:@"%@:%@",prefix,theURI];
return;
}
}
else {
//Check prefix validity
if (![TiDOMValidator checkNamespacePrefix:prefix]) {
Expand Down

0 comments on commit c8b4ff5

Please sign in to comment.