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

Commit

Permalink
Refactor to support older node versions
Browse files Browse the repository at this point in the history
  • Loading branch information
gilbert committed Nov 10, 2016
1 parent 329eff3 commit 568ee36
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 27 deletions.
44 changes: 44 additions & 0 deletions src/extend-domino.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// Strict mode disallows us to overwrite Document.prototype properties.
// This file is to stay out of strict mode.
//
var domino = require("domino");
var Document = require('domino/lib/Document');
var Element = require('domino/lib/Element');


module.exports = function (newHTMLElement, _createElement) {
var result = {};

//
// Patch document.createElement
//
Document.prototype.createElement = function(tagName, options) {
return _createElement(this, tagName, options, true);
};

//
// Patch HTMLElement
//
result.HTMLElement = newHTMLElement;
result.HTMLElement.prototype = Object.create(domino.impl.HTMLElement.prototype, {
constructor: {value: result.HTMLElement, configurable: true, writable: true},
});


//
// Patch doc.createElementNS
//
var HTMLNS = 'http://www.w3.org/1999/xhtml';
var _origCreateElementNS = Document.prototype.createElementNS;

Document.prototype.createElementNS = function(namespaceURI, qualifiedName) {
if (namespaceURI === 'http://www.w3.org/1999/xhtml') {
return this.createElement(qualifiedName);
} else {
return _origCreateElementNS.call(this, namespaceURI, qualifiedName);
}
};

return result;
};
34 changes: 7 additions & 27 deletions src/registry.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"use strict";
var domino = require("domino");
var Document = require('domino/lib/Document');
var Element = require('domino/lib/Element');
Expand Down Expand Up @@ -210,9 +211,11 @@ exports = module.exports = CustomElementRegistry;


//
// Overwrite domino's new element constructor
// - Overwrite domino's new element constructor
// - Patch domino's document.createElement
//
const origHTMLElement = domino.impl.HTMLElement;
const _origCreateElement = Document.prototype.createElement;

const newHTMLElement = function HTMLElement() {
const customElements = _customElements();
Expand All @@ -231,16 +234,6 @@ const newHTMLElement = function HTMLElement() {
}
throw new Error('Unknown constructor. Did you call customElements.define()?');
};
exports.HTMLElement = newHTMLElement;
exports.HTMLElement.prototype = Object.create(domino.impl.HTMLElement.prototype, {
constructor: {value: exports.HTMLElement, configurable: true, writable: true},
});


//
// Patch document.createElement
//
const _origCreateElement = Document.prototype.createElement;

/**
* Creates a new element and upgrades it if it's a custom element.
Expand All @@ -262,23 +255,10 @@ function _createElement(doc, tagName, options, callConstructor) {
}
return element;
}
Document.prototype.createElement = function(tagName, options) {
return _createElement(this, tagName, options, true);
};

//
// Patch doc.createElementNS
//
const HTMLNS = 'http://www.w3.org/1999/xhtml';
const _origCreateElementNS = Document.prototype.createElementNS;

Document.prototype.createElementNS = function(namespaceURI, qualifiedName) {
if (namespaceURI === 'http://www.w3.org/1999/xhtml') {
return this.createElement(qualifiedName);
} else {
return _origCreateElementNS.call(this, namespaceURI, qualifiedName);
}
};

var patched = require('./extend-domino')(newHTMLElement, _createElement);
exports.HTMLElement = patched.HTMLElement;


/**
Expand Down

0 comments on commit 568ee36

Please sign in to comment.