Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 568ee36

Browse files
committed
Refactor to support older node versions
1 parent 329eff3 commit 568ee36

File tree

2 files changed

+51
-27
lines changed

2 files changed

+51
-27
lines changed

src/extend-domino.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//
2+
// Strict mode disallows us to overwrite Document.prototype properties.
3+
// This file is to stay out of strict mode.
4+
//
5+
var domino = require("domino");
6+
var Document = require('domino/lib/Document');
7+
var Element = require('domino/lib/Element');
8+
9+
10+
module.exports = function (newHTMLElement, _createElement) {
11+
var result = {};
12+
13+
//
14+
// Patch document.createElement
15+
//
16+
Document.prototype.createElement = function(tagName, options) {
17+
return _createElement(this, tagName, options, true);
18+
};
19+
20+
//
21+
// Patch HTMLElement
22+
//
23+
result.HTMLElement = newHTMLElement;
24+
result.HTMLElement.prototype = Object.create(domino.impl.HTMLElement.prototype, {
25+
constructor: {value: result.HTMLElement, configurable: true, writable: true},
26+
});
27+
28+
29+
//
30+
// Patch doc.createElementNS
31+
//
32+
var HTMLNS = 'http://www.w3.org/1999/xhtml';
33+
var _origCreateElementNS = Document.prototype.createElementNS;
34+
35+
Document.prototype.createElementNS = function(namespaceURI, qualifiedName) {
36+
if (namespaceURI === 'http://www.w3.org/1999/xhtml') {
37+
return this.createElement(qualifiedName);
38+
} else {
39+
return _origCreateElementNS.call(this, namespaceURI, qualifiedName);
40+
}
41+
};
42+
43+
return result;
44+
};

src/registry.js

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"use strict";
12
var domino = require("domino");
23
var Document = require('domino/lib/Document');
34
var Element = require('domino/lib/Element');
@@ -210,9 +211,11 @@ exports = module.exports = CustomElementRegistry;
210211

211212

212213
//
213-
// Overwrite domino's new element constructor
214+
// - Overwrite domino's new element constructor
215+
// - Patch domino's document.createElement
214216
//
215217
const origHTMLElement = domino.impl.HTMLElement;
218+
const _origCreateElement = Document.prototype.createElement;
216219

217220
const newHTMLElement = function HTMLElement() {
218221
const customElements = _customElements();
@@ -231,16 +234,6 @@ const newHTMLElement = function HTMLElement() {
231234
}
232235
throw new Error('Unknown constructor. Did you call customElements.define()?');
233236
};
234-
exports.HTMLElement = newHTMLElement;
235-
exports.HTMLElement.prototype = Object.create(domino.impl.HTMLElement.prototype, {
236-
constructor: {value: exports.HTMLElement, configurable: true, writable: true},
237-
});
238-
239-
240-
//
241-
// Patch document.createElement
242-
//
243-
const _origCreateElement = Document.prototype.createElement;
244237

245238
/**
246239
* Creates a new element and upgrades it if it's a custom element.
@@ -262,23 +255,10 @@ function _createElement(doc, tagName, options, callConstructor) {
262255
}
263256
return element;
264257
}
265-
Document.prototype.createElement = function(tagName, options) {
266-
return _createElement(this, tagName, options, true);
267-
};
268258

269-
//
270-
// Patch doc.createElementNS
271-
//
272-
const HTMLNS = 'http://www.w3.org/1999/xhtml';
273-
const _origCreateElementNS = Document.prototype.createElementNS;
274-
275-
Document.prototype.createElementNS = function(namespaceURI, qualifiedName) {
276-
if (namespaceURI === 'http://www.w3.org/1999/xhtml') {
277-
return this.createElement(qualifiedName);
278-
} else {
279-
return _origCreateElementNS.call(this, namespaceURI, qualifiedName);
280-
}
281-
};
259+
260+
var patched = require('./extend-domino')(newHTMLElement, _createElement);
261+
exports.HTMLElement = patched.HTMLElement;
282262

283263

284264
/**

0 commit comments

Comments
 (0)