Can we delete the "getContext" property of HTML5 Canvas tag through script?
There is a HTML5 conformance test suite with a test for prototype for HTMLCanvasElement
.
This test fails for Safari, Firefox but passes for Opera on Windows 7.
The test has a script whic开发者_开发知识库h tries to delete the getContext
property of the HTMLCanvasElement
, and further trying to read getContext
should give undefined
.
delete window.HTMLCanvasElement.prototype.getContext;
_assertSame(window.HTMLCanvasElement.prototype.getContext, undefined, "window.HTMLCanvasElement.prototype.getContext", "undefined");
This test fails for WebKit (Safari) because the getContext
property has DontDelete
attribute and so it does not allow the script to delete this property.
Is there any description in the HTML5 specification which says deletion of getContext
property by script is valid?
Is there any description in the HTML5 specification which says deletion of getContext property by script is valid?
No, nothing explicit in the spec about that. No idea why webkit is different than FF/Opera here (It is Chrome too that disallows delete), but the spec itself does not demand DontDelete for anything on the Canvas. Something else in the ECMAScript spec might.
Firefox 7 and 8 alpha do not delete window.HTMLCanvasElement.prototype.getContext though. They just return true, but getContext is still there. In other words, the test you linked to fails in the exact same place and for the same reason.
Webkit of course still allows you to overwrite whatever you want: window.HTMLCanvasElement.prototype.getContext = undefined
From what I understand, the configurability ([[DontDelete]] in ES3, [[Configurable]] in ES5) of getContext
method is described in WebIDL — as any other CanvasRenderingContext2D
methods.
Take a look at "Interface Prototype Object" section, which says:
There must exist an interface prototype object for every interface defined, regardless of whether the interface was declared with the [NoInterfaceObject] extended attribute. The interface prototype object for a particular interface has properties that correspond to the attributes and operations defined on that interface. These properties are described in more detail in sections 4.5.5 and 4.5.6 below.
And in 4.5.6, you can see:
For each unique identifier of an operation defined on the interface, there must be a corresponding property on the interface prototype object (if it is a regular operation) or the interface object (if it is a static operation), unless the effective overload set for that identifier and operation and with an argument count of 0 (for the ECMAScript language binding) has no entries.
The characteristics of such a corresponding property are as follows:
The name of the property is the identifier.
The property has attributes { [[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: true }.
Note the "[[Configurable]]: true" bit (emphasis mine).
精彩评论