MCPcopy Create free account
hub / github.com/ampproject/amphtml / createFormProxyConstr

Function createFormProxyConstr

extensions/amp-form/0.1/form-proxy.js:59–134  ·  view source on GitHub ↗

* @param {!Window} win * @return {function(new:Object, !HTMLFormElement)}

(win)

Source from the content-addressed store, hash-verified

57 * @return {function(new:Object, !HTMLFormElement)}
58 */
59function createFormProxyConstr(win) {
60 /**
61 * @param {!HTMLFormElement} form
62 * @constructor
63 */
64 function FormProxy(form) {
65 /** @private @const {!HTMLFormElement} */
66 this.form_ = form;
67 }
68
69 const FormProxyProto = FormProxy.prototype;
70 const {Object} = win;
71 const ObjectProto = Object.prototype;
72
73 // Hierarchy:
74 // Node <== Element <== HTMLElement <== HTMLFormElement
75 // EventTarget <== HTMLFormElement
76 const baseClasses = [win.HTMLFormElement, win.EventTarget];
77 const inheritance = baseClasses.reduce((all, klass) => {
78 let proto = klass && klass.prototype;
79 while (proto && proto !== ObjectProto) {
80 if (all.indexOf(proto) >= 0) {
81 break;
82 }
83 all.push(proto);
84 proto = Object.getPrototypeOf(proto);
85 }
86
87 return all;
88 }, []);
89
90 /** @type {!Array} */ (inheritance).forEach((proto) => {
91 for (const name in proto) {
92 const property = win.Object.getOwnPropertyDescriptor(proto, name);
93 if (
94 !property ||
95 // Exclude constants.
96 name.toUpperCase() == name ||
97 // Exclude on-events.
98 name.startsWith('on') ||
99 // Exclude properties that already been created.
100 ObjectProto.hasOwnProperty.call(FormProxyProto, name) ||
101 // Exclude some properties. Currently only used for testing.
102 (denylistedProperties && denylistedProperties.includes(name))
103 ) {
104 continue;
105 }
106 if (typeof property.value == 'function') {
107 // A method call. Call the original prototype method via `call`.
108 const method = property.value;
109 FormProxyProto[name] = function () {
110 return method.apply(
111 /** @type {!FormProxy} */ (this).form_,
112 arguments
113 );
114 };
115 } else {
116 // A read/write property. Call the original prototype getter/setter.

Callers 1

getFormProxyConstrFunction · 0.85

Calls 3

applyMethod · 0.80
pushMethod · 0.45
forEachMethod · 0.45

Tested by

no test coverage detected