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

Support namespaced attributes with default attr binding #1066

Merged
merged 2 commits into from
Sep 29, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion spec/defaultBindings/attrBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@ describe('Binding: Attr', function() {
expect(testNode.childNodes[0].getAttribute("second-attribute")).toEqual("true");
});

it('Should be able to set namespaced attribute values', function() {
var model = { myValue: "first value" };
testNode.innerHTML = [
'<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">',
'<g>',
'<a data-bind="attr: { \'xlink:href\': myValue }">',
'<text>foo</text>',
'</a>',
'</g>',
'</svg>'
].join('');

ko.applyBindings(model, testNode);
var anchor = testNode.childNodes[0]/*svg*/.childNodes[0]/*g*/.childNodes[0]/*a*/;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since I've heard this is a bit unclear:

 testNode.childNodes[0].  // the <svg> is the first child of the testNode
          childNodes[0].  // the <g> is the first child of the <svg>
          childNodes[0];  // the <a> is the first child of the <g>

I was trying to use the inline comments to make this more clear, but evidently that backfired :)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it looks a bit like intermixing weird XPath syntax and JS, the missing syntax highlighting doesn't help either. :)

expect( anchor.getAttributeNode('xlink:href').value ).toEqual( 'first value' );
expect( anchor.getAttributeNode('xlink:href').namespaceURI ).toEqual( 'http://www.w3.org/1999/xlink' );
});

it('Should be able to set \"name\" attribute, even on IE6-7', function() {
var myValue = ko.observable("myName");
testNode.innerHTML = "<input data-bind='attr: { name: myValue }' />";
Expand Down Expand Up @@ -62,4 +80,4 @@ describe('Binding: Attr', function() {
expect(testNode.childNodes[0].className).toEqual("");
expect(testNode.childNodes[0].getAttribute("class")).toEqual(null);
});
});
});
13 changes: 11 additions & 2 deletions src/binding/defaultBindings/attr.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@ ko.bindingHandlers['attr'] = {
ko.utils.objectForEach(value, function(attrName, attrValue) {
attrValue = ko.utils.unwrapObservable(attrValue);

// Find the namespace of this attribute, if any.
// Defaulting to `null` should be ok, as
//
// element.setAttributeNS( null, name, value ) ~ element.setAttribute( name, value )
// element.removetAttributeNS( null, name ) ~ element.removeAttribute( name )
//
var prefixLen = attrName.indexOf(':');
var namespace = prefixLen < 0 ? null : element.lookupNamespaceURI( attrName.substr(0, prefixLen) );

// To cover cases like "attr: { checked:someProp }", we want to remove the attribute entirely
// when someProp is a "no value"-like value (strictly null, false, or undefined)
// (because the absence of the "checked" attr is how to mark an element as not checked, etc.)
var toRemove = (attrValue === false) || (attrValue === null) || (attrValue === undefined);
if (toRemove)
element.removeAttribute(attrName);
element.removeAttributeNS(namespace, attrName);

// In IE <= 7 and IE8 Quirks Mode, you have to use the Javascript property name instead of the
// HTML attribute name for certain attributes. IE8 Standards Mode supports the correct behavior,
Expand All @@ -23,7 +32,7 @@ ko.bindingHandlers['attr'] = {
else
element[attrName] = attrValue;
} else if (!toRemove) {
element.setAttribute(attrName, attrValue.toString());
element.setAttributeNS(namespace, attrName, attrValue.toString());
}

// Treat "name" specially - although you can think of it as an attribute, it also needs
Expand Down