Skip to content

Commit

Permalink
feat: trim whitespace around create
Browse files Browse the repository at this point in the history
This simplifies creating larger SVG constructs using template strings.
  • Loading branch information
nikku committed May 29, 2024
1 parent 0b2fef9 commit 82ae019
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lib/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import ns from './util/ns';
export default function create(name, attrs) {
var element;

name = name.trim();

if (name.charAt(0) === '<') {
element = parse(name).firstChild;
element = document.importNode(element, true);
Expand Down
27 changes: 25 additions & 2 deletions test/spec/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,31 @@ describe('create', function() {

// then
expect(svg.nodeName).to.eql('svg');
expect(svg.childNodes.length).to.eql(1);
expect(svg.childNodes[0].nodeName).to.eql('g');
expect(svg.children.length).to.eql(1);
expect(svg.children[0].nodeName).to.eql('g');
});


it('should create <svg> from multi-line markup (trimming whitespace)', function() {

// given
var container = createContainer();

// when
var svg = create(`
<svg>
<g>
<circle cx="10" cy="10" r="2"></circle>
</g>
</svg>
`);

append(container, svg);

// then
expect(svg.nodeName).to.eql('svg');
expect(svg.children.length).to.eql(1);
expect(svg.children[0].nodeName).to.eql('g');
});


Expand Down

0 comments on commit 82ae019

Please sign in to comment.