Skip to content

Commit e65af81

Browse files
jxm262shaunpatterson
authored andcommitted
Small changes to increase coverage and lint errors (#1)
* -fixing lint errors -adding tests * fixing test * removing unncessary attribute checks
1 parent 0a4d5ec commit e65af81

2 files changed

Lines changed: 124 additions & 34 deletions

File tree

lib/htmlparser-to-vdom.js

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,28 @@ var svg = require('./get-property-info/svg');
44
var SVGAttributeHook = require('./get-property-info/svg/svg-attribute-hook');
55

66
module.exports = function createConverter (VNode, VText) {
7+
function convertSvg(tag, attributes, children, key) {
8+
var _attributes = attributes.attributes;
9+
10+
for(var _key in _attributes) {
11+
var namespace = svg.SVGAttributeNamespace(_key);
12+
13+
if (namespace === void 0) { // not a svg attribute
14+
continue;
15+
}
16+
17+
var value = _attributes[_key];
18+
19+
if (namespace !== null) { // namespaced attribute
20+
attributes[_key] = new SVGAttributeHook(namespace, value);
21+
_attributes[_key] = void 0;
22+
continue;
23+
}
24+
}
25+
26+
return new VNode(tag.name, attributes, children, key, svg.getSVGNamespace());
27+
}
28+
729
var converter = {
830
convert: function (node, getVNodeKey) {
931
if (node.type === 'tag' || node.type === 'script' || node.type === 'style') {
@@ -35,39 +57,5 @@ module.exports = function createConverter (VNode, VText) {
3557
}
3658
};
3759

38-
function convertSvg(tag, attributes, children, key) {
39-
var _attributes = attributes.attributes;
40-
41-
for(var _key in _attributes) {
42-
if (!_attributes.hasOwnProperty(_key)) {
43-
continue;
44-
}
45-
46-
var namespace = svg.SVGAttributeNamespace(_key);
47-
48-
if (namespace === void 0) { // not a svg attribute
49-
continue;
50-
}
51-
52-
var value = _attributes[_key];
53-
54-
if (typeof value !== 'string' &&
55-
typeof value !== 'number' &&
56-
typeof value !== 'boolean'
57-
) {
58-
continue;
59-
}
60-
61-
if (namespace !== null) { // namespaced attribute
62-
attributes[_key] = SVGAttributeHook(namespace, value);
63-
_attributes[_key] = void 0;
64-
continue;
65-
}
66-
}
67-
68-
return new VNode(tag.name, attributes, children, key, svg.getSVGNamespace());
69-
}
70-
71-
7260
return converter;
7361
};
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
var SVGAttributeHook = require('../../../lib/get-property-info/svg/svg-attribute-hook');
2+
3+
4+
describe('SVGAttributeHook', function () {
5+
var testNamespace = "http://www.testnamespace.com";
6+
var testValue = "test-value";
7+
var attributeHook = SVGAttributeHook(testNamespace, testValue);
8+
9+
it('creates new AttributeBook with namespace and value', function () {
10+
attributeHook.namespace.should.equal(testNamespace)
11+
attributeHook.value.should.equal(testValue)
12+
});
13+
14+
describe('hook', function() {
15+
context('prev is the same AttributeHook', function() {
16+
it('does nothing', function() {
17+
var prev = attributeHook;
18+
var node = {};
19+
var prop = {};
20+
21+
var hooked = attributeHook.hook(node, prop, prev)
22+
23+
should.equal(hooked, undefined)
24+
});
25+
});
26+
27+
context('prev is different AttributeHook', function() {
28+
it('sets the attribute to the input node', function() {
29+
var otherNamespace = "othernamespace";
30+
var otherValue = "otherValue";
31+
var testProp = "testProp";
32+
var prev = SVGAttributeHook(otherNamespace, otherValue);
33+
var node = {
34+
setAttributeNS: function(namespace, prop, value) {
35+
this.namespace = namespace;
36+
this.value = value;
37+
this.prop = prop;
38+
},
39+
namespace: null,
40+
value: null,
41+
prop: null,
42+
};
43+
44+
attributeHook.hook(node, testProp, prev)
45+
46+
node.namespace.should.equal(testNamespace)
47+
node.value.should.equal(testValue)
48+
node.prop.should.equal(testProp)
49+
});
50+
});
51+
});
52+
53+
describe('unhook', function() {
54+
context('node is has same namespace as next', function() {
55+
it('does nothing', function() {
56+
var next = attributeHook;
57+
var node = {};
58+
var prop = {};
59+
60+
var hooked = attributeHook.unhook(node, prop, next)
61+
62+
should.equal(hooked, undefined)
63+
});
64+
});
65+
66+
context('node has different namespace as next', function() {
67+
it('removes attribute namespace from the node', function() {
68+
var otherNamespace = "othernamespace";
69+
var otherValue = "otherValue";
70+
var testProp = "color:red";
71+
var next = SVGAttributeHook(otherNamespace, otherValue);
72+
var node = {
73+
removeAttributeNS: function(namespace, name) {
74+
this.namespace = name;
75+
},
76+
namespace: testNamespace
77+
};
78+
79+
attributeHook.unhook(node, testProp, next)
80+
81+
node.namespace.should.equal('red')
82+
});
83+
84+
it('removes attribute namespace without colon in prop from the node', function() {
85+
var otherNamespace = "othernamespace";
86+
var otherValue = "otherValue";
87+
var testProp = "color";
88+
var next = SVGAttributeHook(otherNamespace, otherValue);
89+
var node = {
90+
removeAttributeNS: function(namespace, name) {
91+
this.namespace = name;
92+
},
93+
namespace: testNamespace
94+
};
95+
96+
attributeHook.unhook(node, testProp, next)
97+
98+
node.namespace.should.equal('color')
99+
});
100+
});
101+
});
102+
});

0 commit comments

Comments
 (0)