From 0ca6cc4306a144cec6e595a959613f34bd787ece Mon Sep 17 00:00:00 2001 From: Sebastian Helzle Date: Mon, 7 Oct 2019 11:47:35 +0200 Subject: [PATCH 1/2] Allow creating namespaced domelements --- src/dom/createelement.js | 3 ++- tests/dom/createelement.js | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/dom/createelement.js b/src/dom/createelement.js index 75f69a3..7e34ee1 100644 --- a/src/dom/createelement.js +++ b/src/dom/createelement.js @@ -26,7 +26,8 @@ import { isString } from 'lodash-es'; * @returns {Element} Created element. */ export default function createElement( doc, name, attributes = {}, children = [] ) { - const element = doc.createElement( name ); + const namespace = attributes instanceof Object ? attributes.xmlns : null; + const element = namespace ? doc.createElementNS( namespace, name ) : doc.createElement( name ); for ( const key in attributes ) { element.setAttribute( key, attributes[ key ] ); diff --git a/tests/dom/createelement.js b/tests/dom/createelement.js index febd2d2..eed9c41 100644 --- a/tests/dom/createelement.js +++ b/tests/dom/createelement.js @@ -23,6 +23,15 @@ describe( 'createElement', () => { expect( p.getAttribute( 'class' ) ).to.equal( 'foo' ); } ); + it( 'should create element with namespace', () => { + const namespace = 'http://www.w3.org/2000/svg'; + const svg = createElement( document, 'svg', { xmlns: namespace } ); + + expect( svg.tagName.toLowerCase() ).to.equal( 'svg' ); + expect( svg.getAttribute( 'xmlns' ) ).to.equal( namespace ); + expect( svg.createSVGRect instanceof Function ).to.equal( true ); + } ); + it( 'should create element with child text node', () => { const p = createElement( document, 'p', null, 'foo' ); From c146c533cc18b4d181fdab6cf1c1a3eaa1b4c30a Mon Sep 17 00:00:00 2001 From: Sebastian Helzle Date: Mon, 7 Oct 2019 17:52:31 +0200 Subject: [PATCH 2/2] Use shorthand function comparison MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Piotrek KoszuliƄski --- tests/dom/createelement.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/dom/createelement.js b/tests/dom/createelement.js index eed9c41..e0d4eb2 100644 --- a/tests/dom/createelement.js +++ b/tests/dom/createelement.js @@ -29,7 +29,7 @@ describe( 'createElement', () => { expect( svg.tagName.toLowerCase() ).to.equal( 'svg' ); expect( svg.getAttribute( 'xmlns' ) ).to.equal( namespace ); - expect( svg.createSVGRect instanceof Function ).to.equal( true ); + expect( svg.createSVGRect ).to.be.a( 'function' ); } ); it( 'should create element with child text node', () => {