HtmlInlineHtmlDependency.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. */
  4. "use strict";
  5. const { HTML_TYPE } = require("../ModuleSourceTypeConstants");
  6. const makeSerializable = require("../util/makeSerializable");
  7. const ModuleDependency = require("./ModuleDependency");
  8. /** @import { ReplaceSource } from "webpack-sources" */
  9. /** @import CodeGenerationResults from "../CodeGenerationResults" */
  10. /** @import Dependency, { UpdateHashContext } from "../Dependency" */
  11. /** @import { DependencyTemplateContext } from "../DependencyTemplate" */
  12. /** @import Module from "../Module" */
  13. /** @import { Range } from "../javascript/JavascriptParser" */
  14. /** @import Hash from "../util/Hash" */
  15. // One pass over the source: each char is replaced once and replacements aren't
  16. // re-scanned, so this matches the `&`-first sequential escape chain exactly.
  17. const ATTR_ESCAPE_REGEXP = /[&"']/g;
  18. /** @type {Record<string, string>} */
  19. const ATTR_ESCAPES = { "&": "&amp;", '"': "&quot;", "'": "&#39;" };
  20. /**
  21. * @param {string} c matched character
  22. * @returns {string} the HTML-attribute-escaped entity
  23. */
  24. const escapeAttrChar = (c) => ATTR_ESCAPES[c];
  25. /**
  26. * Represents an `<iframe srcdoc="...">` attribute — an entity-encoded HTML
  27. * document. The decoded document is fed back through webpack's HTML pipeline as
  28. * a virtual `data:text/html` module so its asset URLs resolve relative to the
  29. * host HTML file. At render time the attribute value is replaced with the
  30. * processed HTML read from the nested module's `html` code-generation data,
  31. * re-escaped so the surrounding attribute stays valid.
  32. */
  33. class HtmlInlineHtmlDependency extends ModuleDependency {
  34. /**
  35. * @param {string} request virtual request resolving to the inline HTML (data URI)
  36. * @param {Range} range range of the `srcdoc` attribute value
  37. */
  38. constructor(request, range) {
  39. super(request);
  40. this.range = range;
  41. }
  42. get type() {
  43. return "html inline html";
  44. }
  45. get category() {
  46. return "html-srcdoc";
  47. }
  48. /**
  49. * Updates the hash with the data contributed by this instance.
  50. * @param {Hash} hash hash to be updated
  51. * @param {UpdateHashContext} context context
  52. * @returns {void}
  53. */
  54. updateHash(hash, context) {
  55. // Recurse so the inline HTML's transitive deps (assets) propagate up.
  56. // The module is guaranteed — it is registered as a code-generation dependency.
  57. const module = /** @type {Module} */ (
  58. context.chunkGraph.moduleGraph.getModule(this)
  59. );
  60. module.updateHash(hash, context);
  61. }
  62. }
  63. HtmlInlineHtmlDependency.Template = class HtmlInlineHtmlDependencyTemplate extends (
  64. ModuleDependency.Template
  65. ) {
  66. /**
  67. * Applies the plugin by registering its hooks on the compiler.
  68. * @param {Dependency} dependency the dependency for which the template should be applied
  69. * @param {ReplaceSource} source the current replace source which can be modified
  70. * @param {DependencyTemplateContext} templateContext the context object
  71. * @returns {void}
  72. */
  73. apply(dependency, source, { moduleGraph, runtime, codeGenerationResults }) {
  74. const dep = /** @type {HtmlInlineHtmlDependency} */ (dependency);
  75. // The module is guaranteed — it is registered as a code-generation dependency.
  76. const module = /** @type {Module} */ (moduleGraph.getModule(dep));
  77. const codeGen =
  78. /** @type {CodeGenerationResults} */
  79. (codeGenerationResults).get(module, runtime);
  80. const htmlSource = codeGen.sources.get(HTML_TYPE);
  81. // The attribute value was entity-decoded before parsing, so re-escape the
  82. // processed HTML to keep the surrounding `srcdoc="..."` attribute valid.
  83. const htmlText = (
  84. htmlSource ? /** @type {string} */ (htmlSource.source()) : ""
  85. ).replace(ATTR_ESCAPE_REGEXP, escapeAttrChar);
  86. source.replace(dep.range[0], dep.range[1] - 1, htmlText);
  87. }
  88. };
  89. makeSerializable(
  90. HtmlInlineHtmlDependency,
  91. "webpack/lib/dependencies/HtmlInlineHtmlDependency"
  92. );
  93. module.exports = HtmlInlineHtmlDependency;