Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TIMOB-8952]iOS XML: appendChild Removes Namespace and Over Validation #2134

Merged
merged 5 commits into from
May 3, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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