-
Notifications
You must be signed in to change notification settings - Fork 117
Extends the HTMLTableElement and HTMLTableRowElement #6
Comments
Hi, I found the way to solved the problem by change my script. document.registerElement('hi-table', { prototype: HiTable, extends:'table' } );
...
var tblObj = document.createElement('table', 'hi-table'); any idea why it has to be so? |
Hi @MagicJack , first of all thanks for using github instead :-) So here the thing … there are two ways, as you've realized already, to create Custom Elements
The main difference between these two methods is that the second one never requires Shadow DOM. You can think about In order to create same input functionality you need the Shadow DOM behind the scene or you can manually inject the input at runtime or you create an I believe same applies for However, I've no idea why the code was failing and I'd like to understand if it's a IE only thing or if it's the same in every browser but the short answer to your problem is: use this form for elements with a special behavior document.registerElement('hi-table', { prototype: HiTable, extends:'table' } );
var tblObj = document.createElement('table', 'hi-table');
document.registerElement('hi-input', { prototype: HiTable, extends:'input' } );
var tblObj = document.createElement('input', 'hi-input'); I hope this help, feel free to close this bug after whenever you want (or ask more) |
Hi, @WebReflection:
document.registerElement('hi-table', {
prototype: Object.create(
HTMLTableElement.prototype, {
setNamePref: { value: function() {
this.namePref = namePref;
}},
insertHiRow: { value: function() {
var rowObj = document.createElement('tr', 'hi-row');
var tbObj;
rowObj.init(this.namePref, idx);
if (this.tBodies.length==0) {
this.appendChild(document.createElement('tbody'));
}
tbObj = this.tBodies[0];
while (idx > tbObj.childNodes.length)
tbObj.appendChild(document.createElement('tr'));
if (idx == tbObj.childNodes.length)
tbObj.insertBefore(rowObj);
else
tbObj.insertBefore(rowObj, tbObj.childNodes[idx]);
return rowObj;
}}
}),
extends: 'table'
});
...
var tblObj = document.createElement('table', 'hi-table'); |
It would help a lot if you could actually write some code I can test … so many thing missing up there … The |
I've added a test that use Note: nothing actually changed in the code, not sure why your one is failing. |
P.S. bear in mind that registration order matters … if you need |
OK, Thanks. also, after some testing, I got 2 conclutions: var HiTable = Object.create(HTMLTableElement.prototype);
...
document.registerElement('hi-table', { prototype: HiTable, extends:'table' } );
...
var tblObj = document.createElement('table', 'hi-table');
The combination is still true when using the native suport of chrome v36 The codes below are what I used for the testing. <!DOCTYPE html>
<html>
<head><title>test of registerElement</title>
<script type="text/javascript" src="registerElement-max.js"></script>
<script type="text/javascript">
var HiElement = Object.create(HTMLElement.prototype);
HiElement.func1 = function func1() {
alert("This is function1 Hi-Element");
}
HiElement.func2 = function func2() {
alert("This is function2 Hi-Element");
}
document.registerElement('hi-element', { prototype: HiElement } );
// var HiTable = Object.create(HTMLElement.prototype); // Not workable
var HiTable = Object.create(HTMLTableElement.prototype);
HiTable.func1 = function func1() {
alert("This is function1 of Hi-Table");
}
HiTable.func2 = function func2() {
alert("This is function2 of Hi-Table");
}
document.registerElement('hi-table', { prototype: HiTable, extends:'table' } );
// document.registerElement('hi-table', { prototype: HiTable } ); // not workable
function fnInit() {
var elemObj = document.createElement('hi-element');
elemObj.innerHTML = "Hello World";
elemObj.func1(); // OK
var iPt = document.getElementById('insPt');
iPt.appendChild(elemObj);
// insertAdjacentHTML() belongs HTMLElement
elemObj.insertAdjacentHTML('BeforeBegin', 'BBHeader[');
elemObj.insertAdjacentHTML('AfterEnd', ']');
// var tblObj = document.createElement('hi-table'); // This make createCaption() fail
var tblObj = document.createElement('table', 'hi-table'); // This OK
// This fail if registerElement() without "extends:'table'"
// In chrome v36: tblObj.func1() is undefined
tblObj.func1();
// This fail if createElement() without extendsion
tblObj.createCaption();
tblObj.caption.innerHTML = "The Table Header";
iPt.appendChild(tblObj);
}
window.addEventListener('load', fnInit);
</script>
</head>
<body>
Before The Insert Point
<div id="insPt"></div>
After The Insert Point
</body>
</html> |
Thanks for the code and the clarification, my last question would be: is Chrome native producing same results? If yes, we are good .. if not, I might put a big warning in the front page … however, I think the var HiTable = Object.create(HTMLTableElement.prototype);
HiTable.createdCallback = function () {
this.table = this.appendChild(
document.createElement('table')
);
};
document.registerElement('hi-table', { prototype: HiTable }); And use the internal table instead |
Yes. I think you could add another example for people who want to add extends the derivation of |
added example and explanation. Best Regards |
Hi,
I try to create 2 Custom Elements by extend the HTMLTableElement and HTMLTableRowElement, but it fail.
After further debuging, it failed on the code for prototype chain clone.
defineProperty(o, key, gOPD(p, key));
gOPD(p,key)
return the correct Object, butdefineProperty()
fail to define it in new object.I tested it on IE10, here's part of the js code for extend HTMLTableElement:
It fails at
createCaption()
in functionmyCreateTable()
if theprototype
is created from HTMLElement, cause the function belongs to HTMLTableElement only.It fails at the same point if the
prototype
is created from HTMLTableElement, cause:defineProperty()
fail to define object returned bygOPD(p,key)
(line 110)The text was updated successfully, but these errors were encountered: