Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-1350] Ti.XML.DOMDocument.getElementById fails on iPhone

GitHub Issuen/a
TypeBug
PriorityTrivial
StatusClosed
ResolutionFixed
Resolution Date2011-04-17T01:55:56.000+0000
Affected Version/sn/a
Fix Version/sRelease 1.5.0
ComponentsiOS
Labelsdomelement, iphone, xml
ReporterBrion Vibber
AssigneeReggie Seagraves
Created2011-04-15T02:50:10.000+0000
Updated2011-04-17T01:55:56.000+0000

Description

getElementById in the DOM proxy classes on iPhone fails, consistently returning null instead of expected elements.

Example:
var dom = Ti.XML.parseString('

'); var foo = dom.getElementById('foo'); Titanium.API.info('#foo is ' + foo);

Output on iPhone:
[INFO] #foo is null

The Android implementation gives expected output:
[INFO] [306,493] #foo is ti.modules.titanium.xml.ElementProxy@43e66460

The bug:

The XPath expression used to implement getElementById doesn't quote the string, leading to an XPath parse failure, and no results:

NSArray *nodes = [document nodesForXPath:[NSString stringWithFormat:@"//*[@id=%@]",args] error:&error];

Suggested fix is to add quotes:

NSArray *nodes = [document nodesForXPath:[NSString stringWithFormat:@"//*[@id='%@']",args] error:&error];

Probably this should actually check for validity first as well, or else escape the string.

Comments

  1. Brion Vibber 2011-04-15

    Provisional fix as a git branch:
    http://github.com/brion/titanium_mobile/commit/14b88d7e760ed39146f5b59c6440aba23fec4a6a"> http://github.com/brion/titanium_mobile/commit/14b88d7e760ed39146f5...

  2. Brion Vibber 2011-04-15

    Adding "[PATCH]" to title.

  3. Brion Vibber 2011-04-15

    Further fix to return a single node instead of a NodeList. There really need to be some comprehensive unit tests on this module!

    http://github.com/brion/titanium_mobile/commit/d725741f9ec56997499001b20f54ca25dde412a9"> http://github.com/brion/titanium_mobile/commit/d725741f9ec569974990...

    Full branch: http://github.com/brion/titanium_mobile/commits/getelementbyid">http://github.com/brion/titanium_mobile/commits/getelementbyid

  4. Brion Vibber 2011-04-15

    Assigning open patches from StatusNet to our support contact per request.

  5. Brion Vibber 2011-04-15

    Nolan, any issues with this patch? Is there someone else we should be assigning bugs to?

  6. Jeff Haynie 2011-04-15

    merged.

  7. MistX 2011-04-15

    Hi,

    I have the same problem that getElementById() returns null. I changed the class TiDOMDocumentProxy.m, but there is still the same problem.

    var f = Titanium.Filesystem.getFile("test.xml");
    var textString = f.read();
    var textNode = textString.toString();

    var xml = Ti.XML.parseString(textNode);
    var foo = xml.getElementById('node_one');
    alert(foo);

    Have you any ideas?

    TiA,
    -Marc

  8. Pedro Enrique 2011-04-15

    Tested on iPhone 4 (4.2) and on simulator running 4.0, 4.1, and 4.2
    SDK 1.5 (12/8/10) - r47a03e70
    no problems found
    code:

       var win = Ti.UI.createWindow();
       var xml = '<all>';
       xml += '    <one>hello</one>';
       xml += '    <two>world</two>';
       xml += '    <three id="foo">hello world</three>';
       xml += '</all>';
       var dom = Ti.XML.parseString(xml); 
       var foo = dom.getElementById('foo'); 
       Ti.API.info('#foo is ' + foo);
       Ti.API.info('#foo\'s text is ' + foo.text);
       win.open();
       

    info: #foo is [object TiDOMElement]
    info: #foo's text is hello world

JSON Source