HTMLVideoElement.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. "use strict";
  2. const conversions = require("webidl-conversions");
  3. const utils = require("./utils.js");
  4. const HTMLConstructor_jsdom_living_helpers_html_constructor =
  5. require("../../jsdom/living/helpers/html-constructor.js").HTMLConstructor;
  6. const parseNonNegativeInteger_jsdom_living_helpers_strings =
  7. require("../../jsdom/living/helpers/strings.js").parseNonNegativeInteger;
  8. const ceReactionsPreSteps_jsdom_living_helpers_custom_elements =
  9. require("../../jsdom/living/helpers/custom-elements.js").ceReactionsPreSteps;
  10. const ceReactionsPostSteps_jsdom_living_helpers_custom_elements =
  11. require("../../jsdom/living/helpers/custom-elements.js").ceReactionsPostSteps;
  12. const serializeURLwhatwg_url = require("whatwg-url").serializeURL;
  13. const implSymbol = utils.implSymbol;
  14. const ctorRegistrySymbol = utils.ctorRegistrySymbol;
  15. const HTMLMediaElement = require("./HTMLMediaElement.js");
  16. const interfaceName = "HTMLVideoElement";
  17. exports.is = value => {
  18. return utils.isObject(value) && Object.hasOwn(value, implSymbol) && value[implSymbol] instanceof Impl.implementation;
  19. };
  20. exports.isImpl = value => {
  21. return utils.isObject(value) && value instanceof Impl.implementation;
  22. };
  23. exports.convert = (globalObject, value, { context = "The provided value" } = {}) => {
  24. if (exports.is(value)) {
  25. return utils.implForWrapper(value);
  26. }
  27. throw new globalObject.TypeError(`${context} is not of type 'HTMLVideoElement'.`);
  28. };
  29. function makeWrapper(globalObject, newTarget) {
  30. let proto;
  31. if (newTarget !== undefined) {
  32. proto = newTarget.prototype;
  33. }
  34. if (!utils.isObject(proto)) {
  35. proto = globalObject[ctorRegistrySymbol]["HTMLVideoElement"].prototype;
  36. }
  37. return Object.create(proto);
  38. }
  39. exports.create = (globalObject, constructorArgs, privateData) => {
  40. const wrapper = makeWrapper(globalObject);
  41. return exports.setup(wrapper, globalObject, constructorArgs, privateData);
  42. };
  43. exports.createImpl = (globalObject, constructorArgs, privateData) => {
  44. const wrapper = exports.create(globalObject, constructorArgs, privateData);
  45. return utils.implForWrapper(wrapper);
  46. };
  47. exports._internalSetup = (wrapper, globalObject) => {
  48. HTMLMediaElement._internalSetup(wrapper, globalObject);
  49. };
  50. exports.setup = (wrapper, globalObject, constructorArgs = [], privateData = {}) => {
  51. privateData.wrapper = wrapper;
  52. exports._internalSetup(wrapper, globalObject);
  53. Object.defineProperty(wrapper, implSymbol, {
  54. value: new Impl.implementation(globalObject, constructorArgs, privateData),
  55. configurable: true
  56. });
  57. wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
  58. if (Impl.init) {
  59. Impl.init(wrapper[implSymbol]);
  60. }
  61. return wrapper;
  62. };
  63. exports.new = (globalObject, newTarget) => {
  64. const wrapper = makeWrapper(globalObject, newTarget);
  65. exports._internalSetup(wrapper, globalObject);
  66. Object.defineProperty(wrapper, implSymbol, {
  67. value: Object.create(Impl.implementation.prototype),
  68. configurable: true
  69. });
  70. wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
  71. if (Impl.init) {
  72. Impl.init(wrapper[implSymbol]);
  73. }
  74. return wrapper[implSymbol];
  75. };
  76. const exposed = new Set(["Window"]);
  77. exports.install = (globalObject, globalNames) => {
  78. if (!globalNames.some(globalName => exposed.has(globalName))) {
  79. return;
  80. }
  81. const ctorRegistry = utils.initCtorRegistry(globalObject);
  82. class HTMLVideoElement extends globalObject.HTMLMediaElement {
  83. constructor() {
  84. return HTMLConstructor_jsdom_living_helpers_html_constructor(globalObject, interfaceName, new.target);
  85. }
  86. get width() {
  87. const esValue = this !== null && this !== undefined ? this : globalObject;
  88. if (!exports.is(esValue)) {
  89. throw new globalObject.TypeError(
  90. "'get width' called on an object that is not a valid instance of HTMLVideoElement."
  91. );
  92. }
  93. ceReactionsPreSteps_jsdom_living_helpers_custom_elements(globalObject);
  94. try {
  95. let value = esValue[implSymbol]._reflectGetTheContentAttribute("width");
  96. if (value !== null) {
  97. value = parseNonNegativeInteger_jsdom_living_helpers_strings(value);
  98. if (value !== null && value >= 0 && value <= 2147483647) {
  99. return value;
  100. }
  101. }
  102. return 0;
  103. } finally {
  104. ceReactionsPostSteps_jsdom_living_helpers_custom_elements(globalObject);
  105. }
  106. }
  107. set width(V) {
  108. const esValue = this !== null && this !== undefined ? this : globalObject;
  109. if (!exports.is(esValue)) {
  110. throw new globalObject.TypeError(
  111. "'set width' called on an object that is not a valid instance of HTMLVideoElement."
  112. );
  113. }
  114. V = conversions["unsigned long"](V, {
  115. context: "Failed to set the 'width' property on 'HTMLVideoElement': The provided value",
  116. globals: globalObject
  117. });
  118. ceReactionsPreSteps_jsdom_living_helpers_custom_elements(globalObject);
  119. try {
  120. const newValue = V <= 2147483647 && V >= 0 ? V : 0;
  121. esValue[implSymbol]._reflectSetTheContentAttribute("width", String(newValue));
  122. } finally {
  123. ceReactionsPostSteps_jsdom_living_helpers_custom_elements(globalObject);
  124. }
  125. }
  126. get height() {
  127. const esValue = this !== null && this !== undefined ? this : globalObject;
  128. if (!exports.is(esValue)) {
  129. throw new globalObject.TypeError(
  130. "'get height' called on an object that is not a valid instance of HTMLVideoElement."
  131. );
  132. }
  133. ceReactionsPreSteps_jsdom_living_helpers_custom_elements(globalObject);
  134. try {
  135. let value = esValue[implSymbol]._reflectGetTheContentAttribute("height");
  136. if (value !== null) {
  137. value = parseNonNegativeInteger_jsdom_living_helpers_strings(value);
  138. if (value !== null && value >= 0 && value <= 2147483647) {
  139. return value;
  140. }
  141. }
  142. return 0;
  143. } finally {
  144. ceReactionsPostSteps_jsdom_living_helpers_custom_elements(globalObject);
  145. }
  146. }
  147. set height(V) {
  148. const esValue = this !== null && this !== undefined ? this : globalObject;
  149. if (!exports.is(esValue)) {
  150. throw new globalObject.TypeError(
  151. "'set height' called on an object that is not a valid instance of HTMLVideoElement."
  152. );
  153. }
  154. V = conversions["unsigned long"](V, {
  155. context: "Failed to set the 'height' property on 'HTMLVideoElement': The provided value",
  156. globals: globalObject
  157. });
  158. ceReactionsPreSteps_jsdom_living_helpers_custom_elements(globalObject);
  159. try {
  160. const newValue = V <= 2147483647 && V >= 0 ? V : 0;
  161. esValue[implSymbol]._reflectSetTheContentAttribute("height", String(newValue));
  162. } finally {
  163. ceReactionsPostSteps_jsdom_living_helpers_custom_elements(globalObject);
  164. }
  165. }
  166. get videoWidth() {
  167. const esValue = this !== null && this !== undefined ? this : globalObject;
  168. if (!exports.is(esValue)) {
  169. throw new globalObject.TypeError(
  170. "'get videoWidth' called on an object that is not a valid instance of HTMLVideoElement."
  171. );
  172. }
  173. return esValue[implSymbol]["videoWidth"];
  174. }
  175. get videoHeight() {
  176. const esValue = this !== null && this !== undefined ? this : globalObject;
  177. if (!exports.is(esValue)) {
  178. throw new globalObject.TypeError(
  179. "'get videoHeight' called on an object that is not a valid instance of HTMLVideoElement."
  180. );
  181. }
  182. return esValue[implSymbol]["videoHeight"];
  183. }
  184. get poster() {
  185. const esValue = this !== null && this !== undefined ? this : globalObject;
  186. if (!exports.is(esValue)) {
  187. throw new globalObject.TypeError(
  188. "'get poster' called on an object that is not a valid instance of HTMLVideoElement."
  189. );
  190. }
  191. ceReactionsPreSteps_jsdom_living_helpers_custom_elements(globalObject);
  192. try {
  193. const impl = esValue[implSymbol];
  194. const value = impl._reflectGetTheContentAttribute("poster");
  195. if (value === null) {
  196. return "";
  197. }
  198. const document = impl._ownerDocument;
  199. if (this._posterURLCacheKey === value && this._baseURLCache === document._baseURLCache) {
  200. return this._posterURLCache;
  201. }
  202. this._posterURLCacheKey = value;
  203. this._baseURLCache = document._baseURLCache;
  204. const urlRecord = document.encodingParseAURL(value);
  205. if (urlRecord !== null) {
  206. this._posterURLCache = serializeURLwhatwg_url(urlRecord);
  207. return this._posterURLCache;
  208. }
  209. this._posterURLCache = conversions.USVString(value);
  210. return this._posterURLCache;
  211. } finally {
  212. ceReactionsPostSteps_jsdom_living_helpers_custom_elements(globalObject);
  213. }
  214. }
  215. set poster(V) {
  216. const esValue = this !== null && this !== undefined ? this : globalObject;
  217. if (!exports.is(esValue)) {
  218. throw new globalObject.TypeError(
  219. "'set poster' called on an object that is not a valid instance of HTMLVideoElement."
  220. );
  221. }
  222. V = conversions["USVString"](V, {
  223. context: "Failed to set the 'poster' property on 'HTMLVideoElement': The provided value",
  224. globals: globalObject
  225. });
  226. ceReactionsPreSteps_jsdom_living_helpers_custom_elements(globalObject);
  227. try {
  228. esValue[implSymbol]._reflectSetTheContentAttribute("poster", V);
  229. } finally {
  230. ceReactionsPostSteps_jsdom_living_helpers_custom_elements(globalObject);
  231. }
  232. }
  233. get playsInline() {
  234. const esValue = this !== null && this !== undefined ? this : globalObject;
  235. if (!exports.is(esValue)) {
  236. throw new globalObject.TypeError(
  237. "'get playsInline' called on an object that is not a valid instance of HTMLVideoElement."
  238. );
  239. }
  240. ceReactionsPreSteps_jsdom_living_helpers_custom_elements(globalObject);
  241. try {
  242. return esValue[implSymbol]._reflectGetTheContentAttribute("playsinline") !== null;
  243. } finally {
  244. ceReactionsPostSteps_jsdom_living_helpers_custom_elements(globalObject);
  245. }
  246. }
  247. set playsInline(V) {
  248. const esValue = this !== null && this !== undefined ? this : globalObject;
  249. if (!exports.is(esValue)) {
  250. throw new globalObject.TypeError(
  251. "'set playsInline' called on an object that is not a valid instance of HTMLVideoElement."
  252. );
  253. }
  254. V = conversions["boolean"](V, {
  255. context: "Failed to set the 'playsInline' property on 'HTMLVideoElement': The provided value",
  256. globals: globalObject
  257. });
  258. ceReactionsPreSteps_jsdom_living_helpers_custom_elements(globalObject);
  259. try {
  260. if (V) {
  261. esValue[implSymbol]._reflectSetTheContentAttribute("playsinline", "");
  262. } else {
  263. esValue[implSymbol]._reflectDeleteTheContentAttribute("playsinline");
  264. }
  265. } finally {
  266. ceReactionsPostSteps_jsdom_living_helpers_custom_elements(globalObject);
  267. }
  268. }
  269. }
  270. Object.defineProperties(HTMLVideoElement.prototype, {
  271. width: { enumerable: true },
  272. height: { enumerable: true },
  273. videoWidth: { enumerable: true },
  274. videoHeight: { enumerable: true },
  275. poster: { enumerable: true },
  276. playsInline: { enumerable: true },
  277. [Symbol.toStringTag]: { value: "HTMLVideoElement", configurable: true }
  278. });
  279. ctorRegistry[interfaceName] = HTMLVideoElement;
  280. Object.defineProperty(globalObject, interfaceName, {
  281. configurable: true,
  282. writable: true,
  283. value: HTMLVideoElement
  284. });
  285. };
  286. const Impl = require("../../jsdom/living/nodes/HTMLVideoElement-impl.js");