Skip to content

Instantly share code, notes, and snippets.

@Xliff
Last active June 13, 2016 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xliff/fa743c54a5164bee0c0381a4d4c46390 to your computer and use it in GitHub Desktop.
Save Xliff/fa743c54a5164bee0c0381a4d4c46390 to your computer and use it in GitHub Desktop.

So I have the following routine:

method getAttributeNode($a) {
    my $ret = domGetAttrNode(self, $a);
    #my $retVal = $ret.clone;
    #xmlFree($ret);

    # cw: Returns CStruct allocated from libxml2!
    return $ret;
}

sub domGetAttrNode(xmlNode $n, Str $a) is export {
    my $name = $a.subst(/\s/, '');
    return unless $name;

    my $ret;
    unless $ret := nativecast(
        xmlAttr, xmlHasNsProp($n, $a, Str)
    ) {
        my ($prefix, $localname) = $a.split(':');

        if !$localname {
            $localname = $prefix;
            $prefix = Nil;
        }
        if $localname {
            my $ns = xmlSearchNs($n.doc, $n, $prefix);
            if $ns {
                $ret := nativecast(
                    xmlAttr, xmlHasNsProp($n, $localname, $ns.href)
                );
            }
        }
    }

    return if $ret && $ret.type != XML_ATTRIBUTE_NODE;

    return $ret;
}

And the following code:

my $attr = $elem.getAttributeNode($attname1);
# TEST
ok $attr, 'can retrieve attribute node';
# TEST
is $attr.name, $attname1, 'attribute name is properly set';
# TEST
is $attr.value, $attvalue1, 'attribute value is properly set';

$elem.setAttribute( $attname1, $attvalue2 );
# TEST -14
is $elem.getAttribute($attname1), $attvalue2, 'retrieved correct attribute value via element';
# TEST
is $attr.value, $attvalue2, 'new value successfully propagated to attribute';

Which produces the following output:

ok 10 - can retrieve attribute node
ok 11 - attribute name is properly set
ok 12 - attribute value is properly set
ok 13 - retrieved correct attribute value via element
not ok 14 - new value successfully propagated to attribute

But if the first block of code properly retrieves the pointer from libxml2, why isn't the change being reflected in the $attr object?

@FROGGS
Copy link

FROGGS commented Jun 13, 2016

I believe that NC caches attribute values like $attr.value. To refresh just call refresh($attr). See https://github.com/rakudo/rakudo/blob/nom/lib/NativeCall.pm6#L381 for reference.

If that helps here, we need to consider a properly working mechanism.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment