You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Oct 7, 2020. It is now read-only.
Repository navigation
This repository was archived by the owner on Oct 7, 2020. It is now read-only.
Extends the HTMLTableElement and HTMLTableRowElement #6
It fails at createCaption() in function myCreateTable()if the prototype 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 by gOPD(p,key) (line 110)
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 one using a tag with at least a dash in the name, x-bla, my-table
the one using the is attribute
The main difference between these two methods is that the second one never requires Shadow DOM.
You can think about input, if you create a my-field element from HTMLInputElement prototype but you chose your own my-field name instead of input with is attribute as my-field, you will not see the input the way you expect, you'll rather see an empty space.
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 <input>, and you use is attribute with your registered class.
I believe same applies for table element, and generally speaking all elements that are not already recognized by the browser and have a different behavior from HTMLElement or HTMLUnknownElement.
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
I test the code of my 1st post on chrome v36.0.1985.143 m, I got the same error as IE. They both stop at the function createCaption() when I create a new ht-table object.
on chrome I got 'Uncaught TypeError: Illegal invocation'
on IE10 it's a simular error message in chinese.
I also try to rewite the workable code in the style you've posted, as below.
But, it does not work! (what happen ?!?)
It would help a lot if you could actually write some code I can test … so many thing missing up there … setNamePref has no parameter, no idea where namePref comes from. Rest of the logic I don't know what it does, where is hi-row defined or anything else but for sure nothing happened since last time you told me that other form was working … no commits in here 'cause I don't know what to fix exactly.
The illegal invocation makes sense to me if you consider what I've said about inherited behavior VS "same kind of DOM node" which is the case of the is attribute.
P.S. bear in mind that registration order matters … if you need hi-row inside hi-table you should register hi-rowbefore the table one. Just in case that was your last issue.
OK, Thanks.
I find my bugs. Both code styles can work (thanks for the hint that setNamePref has no parameter).
also, after some testing, I got 2 conclutions:
1.If I'm just extending HTMLElement then create an object of what I'm registered (say some other than hi-table and hi-row of my code) will work fine.
2. If I want to extend HTMLTableElement or HTMLTableRowElement or anything else that's derivation of HTMLElement, the only possible workable combination is as below
(Still, I don't know why it has to be so.)
Have to create the element by orginal with extension
The combination is still true when using the native suport of chrome v36
The codes below are what I used for the testing.
<!DOCTYPEhtml><html><head><title>test of registerElement</title><scripttype="text/javascript"src="registerElement-max.js"></script><scripttype="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() {varelemObj=document.createElement('hi-element');elemObj.innerHTML="Hello World";elemObj.func1();// OKvariPt=document.getElementById('insPt');iPt.appendChild(elemObj);// insertAdjacentHTML() belongs HTMLElementelemObj.insertAdjacentHTML('BeforeBegin','BBHeader[');elemObj.insertAdjacentHTML('AfterEnd',']');// var tblObj = document.createElement('hi-table'); // This make createCaption() failvartblObj=document.createElement('table','hi-table');// This OK// This fail if registerElement() without "extends:'table'"// In chrome v36: tblObj.func1() is undefinedtblObj.func1();// This fail if createElement() without extendsiontblObj.createCaption();tblObj.caption.innerHTML="The Table Header";iPt.appendChild(tblObj);}
window.addEventListener('load', fnInit);
</script></head><body>
Before The Insert Point
<divid="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 is attribute js the preferred way in any case for backward compatibility, hopefully not a big deal … or you can still do something like:
Activity
MagicJack commentedon Aug 29, 2014
Hi, I found the way to solved the problem by change my script.
any idea why it has to be so?
WebReflection commentedon Aug 29, 2014
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
x-bla,my-tableisattributeThe main difference between these two methods is that the second one never requires Shadow DOM.
You can think about
input, if you create amy-fieldelement fromHTMLInputElementprototype but you chose your ownmy-fieldname instead ofinputwithisattribute asmy-field, you will not see theinputthe way you expect, you'll rather see an empty space.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
<input>, and you useisattribute with your registered class.I believe same applies for
tableelement, and generally speaking all elements that are not already recognized by the browser and have a different behavior fromHTMLElementorHTMLUnknownElement.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
I hope this help, feel free to close this bug after whenever you want (or ask more)
MagicJack commentedon Sep 1, 2014
Hi, @WebReflection:
createCaption()when I create a newht-tableobject.on chrome I got 'Uncaught TypeError: Illegal invocation'
on IE10 it's a simular error message in chinese.
But, it does not work! (what happen ?!?)
WebReflection commentedon Sep 1, 2014
It would help a lot if you could actually write some code I can test … so many thing missing up there …
setNamePrefhas no parameter, no idea wherenamePrefcomes from. Rest of the logic I don't know what it does, where ishi-rowdefined or anything else but for sure nothing happened since last time you told me that other form was working … no commits in here 'cause I don't know what to fix exactly.The
illegal invocationmakes sense to me if you consider what I've said about inherited behavior VS "same kind of DOM node" which is the case of theisattribute.WebReflection commentedon Sep 1, 2014
I've added a test that use
createCaptionwithout problems. I will close this bug but feel free to ping me back if you have other questions.Note: nothing actually changed in the code, not sure why your one is failing.
WebReflection commentedon Sep 1, 2014
P.S. bear in mind that registration order matters … if you need
hi-rowinsidehi-tableyou should registerhi-rowbefore the table one. Just in case that was your last issue.MagicJack commentedon Sep 2, 2014
OK, Thanks.
I find my bugs. Both code styles can work (thanks for the hint that
setNamePrefhas no parameter).also, after some testing, I got 2 conclutions:
1.If I'm just extending
HTMLElementthen create an object of what I'm registered (say some other thanhi-tableandhi-rowof my code) will work fine.2. If I want to extend
HTMLTableElementorHTMLTableRowElementor anything else that's derivation ofHTMLElement, the only possible workable combination is as below(Still, I don't know why it has to be so.)
extends:The combination is still true when using the native suport of chrome v36
The codes below are what I used for the testing.
WebReflection commentedon Sep 2, 2014
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
isattribute js the preferred way in any case for backward compatibility, hopefully not a big deal … or you can still do something like:And use the internal table instead
MagicJack commentedon Sep 2, 2014
Yes.
Chrome native, Chrome + extension code and IE10 + extension, they produce same results.
Maybe I'll try IE9 and IE11 later.
I think you could add another example for people who want to add extends the derivation of
HTMLElementnot justHTMLElementitself.WebReflection commentedon Sep 2, 2014
added example and explanation.
Best Regards