Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #9709 (new defect)

Opened 8 months ago

Last modified 2 months ago

[PATCH] XML nodes and Element.readAttribute

Reported by: yanick.rochon Assigned to: sam
Priority: normal Milestone: 2.x
Component: Prototype Version: edge
Severity: minor Keywords: XML node readAttribute
Cc:

Description

As described in this post : http://groups.google.ca/group/prototype-core/browse_thread/thread/64dd0e29596259dd/42892c969503de76 I ran into a little problem while trying to read node attributes from an XML document. A test case should not be necessary as pretty much any XML node returned from an AJAX request to an onComplete even will cause this issue.

Here is a "fixed" version of the code I've been using so far without any trouble. The modification is not extensive and should not cause any other issue, except for maybe having negative effects on performances (if any).

   // @modified 2007-09-14 Yanick Rochon
  readAttribute: function(element, name) {
    //element = $(element);     // IE doesn't like this line (is it really necessary ?)
    if (Prototype.Browser.IE) {
      // XML nodes in IE has an array name attributes. Each index
      // is an object { name: [string], value: [mixed], ... }
      if ( element.attributes && element.attributes.length ) {
        // I'm sure some might find a better way to do this, but it works for now....
        var attribute = $A(element.attributes).find(function(attribute) {return attribute.name==name; });
        // if we do not return here, we might throw an exception, so rather return null if not found
        return attribute ? attribute.value : null;
      }
      var t = Element._attributeTranslations.read;
      if (t.values[name]) return t.values[name](element, name);
      if (t.names[name]) name = t.names[name];
      if (name.include(':')) {
        return (!element.attributes || !element.attributes[name]) ? null :
         element.attributes[name].value;
      }
    }
    return element.getAttribute(name);
  }

Attachments

fix_ie_xml.patch (0.5 kB) - added by jdalton on 03/27/08 14:43:50.
skips extending the xml node if it detects the .xml prop

Change History

09/27/07 15:31:11 changed by Tobie

There's an issue with the attributes collection and cloning nodes in IE.

We were originally using the attributes collection but had to revert to using getAttribute only.

See changeset [7222] for more details.

I suppose we could special case for attributes containing ":", as you probably don't intend to clone XML nodes anyway.

09/27/07 15:52:49 changed by yanick.rochon

Then there should be a way to detect if the node is an XML node... originally, I thought using a try...catch and only on exception I would test the attributes collection, but I just didn't like that idea.

Perhaps there should be a method DOM.readAttribute(node, attributeName) or something (which would make more sense than Element.readAttribute anyway....

09/27/07 16:01:17 changed by Tobie

Is reading attributes from XML nodes subject to the same nonsense as reading attributes of HTML elements in IE ?

03/27/08 14:42:51 changed by jdalton

I tried this with Prototype 1.6.0.2 and got errors when it tried to extend the nodes in IE. I have attached a patch to fix this. With this patch you cannot use:

$(node).readAttribute('foo');

You must use:

Element.readAttribute(node, 'foo');

03/27/08 14:43:50 changed by jdalton

  • attachment fix_ie_xml.patch added.

skips extending the xml node if it detects the .xml prop

03/27/08 14:44:44 changed by jdalton

  • priority changed from low to normal.
  • summary changed from XML nodes and Element.readAttribute to [PATCH] XML nodes and Element.readAttribute.