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

fix(virtual-node): default and lowercase type property #2350

Merged
merged 3 commits into from
Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 15 additions & 1 deletion lib/core/base/virtual-node/serial-virtual-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,25 @@ function normaliseProps(serialNode) {
`nodeName has to be a string, got '${nodeName}'`
);

nodeName = nodeName.toLowerCase();
let type = null;
if (nodeName === 'input') {
type = (
serialNode.type ||
(serialNode.attributes && serialNode.attributes.type) ||
straker marked this conversation as resolved.
Show resolved Hide resolved
'text'
).toLowerCase();
}

const props = {
...serialNode,
nodeType,
nodeName: nodeName.toLowerCase()
nodeName
};
if (type) {
props.type = type;
}

delete props.attributes;
return Object.freeze(props);
}
Expand Down
16 changes: 14 additions & 2 deletions lib/core/base/virtual-node/virtual-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ class VirtualNode extends AbstractVirtualNode {
}
this._isXHTML = isXHTMLGlobal;

this._nodeName = this._isXHTML
? node.nodeName
: node.nodeName.toLowerCase();

// we will normalize the type prop for inputs by looking strictly
// at the attribute and not what the browser resolves the type
// to be
if (node.nodeName.toLowerCase() === 'input') {
const type = node.getAttribute('type') || 'text';
straker marked this conversation as resolved.
Show resolved Hide resolved
this._type = this._isXHTML ? type : type.toLowerCase();
}

// TODO: es-modules_cache
if (axe._cache.get('nodeMap')) {
axe._cache.get('nodeMap').set(node, this);
Expand All @@ -35,13 +47,13 @@ class VirtualNode extends AbstractVirtualNode {
// abstract Node properties so we can run axe in DOM-less environments.
// add to the prototype so memory is shared across all virtual nodes
get props() {
const { nodeType, nodeName, id, type, multiple } = this.actualNode;
const { nodeType, nodeName, id, multiple } = this.actualNode;

return {
nodeType,
nodeName: this._isXHTML ? nodeName : nodeName.toLowerCase(),
id,
type,
type: this._type,
multiple
};
}
Expand Down
42 changes: 42 additions & 0 deletions test/core/base/virtual-node/serial-virtual-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,48 @@ describe('SerialVirtualNode', function() {
});
assert.isUndefined(vNode.props.attributes);
});

it('converts type prop to lower case', function() {
var types = ['text', 'COLOR', 'Month', 'uRL'];
types.forEach(function(type) {
var vNode = new SerialVirtualNode({
nodeName: 'input',
type: type
});
assert.equal(vNode.props.type, type.toLowerCase());
});
});

it('converts type attribute to lower case', function() {
var types = ['text', 'COLOR', 'Month', 'uRL'];
types.forEach(function(type) {
var vNode = new SerialVirtualNode({
nodeName: 'input',
attributes: {
type: type
}
});
assert.equal(vNode.props.type, type.toLowerCase());
});
});

it('defaults type prop to "text"', function() {
var vNode = new SerialVirtualNode({
nodeName: 'input'
});
assert.equal(vNode.props.type, 'text');
});

it('uses the type property over the type attribute', function() {
var vNode = new SerialVirtualNode({
nodeName: 'input',
type: 'month',
attributes: {
type: 'color'
}
});
assert.equal(vNode.props.type, 'month');
});
});

describe('attr', function() {
Expand Down
8 changes: 8 additions & 0 deletions test/core/base/virtual-node/virtual-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ describe('VirtualNode', function() {
assert.equal(vNode.props.type, 'text');
});

it('should lowercase type', function() {
var node = document.createElement('input');
node.setAttribute('type', 'COLOR');
var vNode = new VirtualNode(node);

assert.equal(vNode.props.type, 'color');
});

it('should lowercase nodeName', function() {
var node = {
nodeName: 'FOOBAR'
Expand Down