浏览代码

Allow creating namespaced domelements

Sebastian Helzle 6 年之前
父节点
当前提交
e7946ce07c

+ 2 - 1
packages/ckeditor5-utils/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 ] );

+ 9 - 0
packages/ckeditor5-utils/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' );