html-images-alt-minifier.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright 2010-2020 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * The code in this file is free software: you can redistribute it and/or
  8. * modify it under the terms of the GNU Affero General Public License
  9. * (GNU AGPL) as published by the Free Software Foundation, either version 3
  10. * of the License, or (at your option) any later version.
  11. *
  12. * The code in this file is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  15. * General Public License for more details.
  16. *
  17. * As additional permission under GNU AGPL version 3 section 7, you may
  18. * distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
  19. * AGPL normally required by section 4, provided you include this license
  20. * notice and a URL through which recipients can access the Corresponding
  21. * Source.
  22. */
  23. this.singlefile.lib.modules.imagesAltMinifier = this.singlefile.lib.modules.imagesAltMinifier || (() => {
  24. const singlefile = this.singlefile;
  25. const EMPTY_IMAGE = "data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==";
  26. return {
  27. process
  28. };
  29. function process(doc) {
  30. doc.querySelectorAll("picture").forEach(pictureElement => {
  31. const imgElement = pictureElement.querySelector("img");
  32. if (imgElement) {
  33. let { src, srcset } = getImgSrcData(imgElement);
  34. if (!src) {
  35. const data = getSourceSrcData(Array.from(pictureElement.querySelectorAll("source")).reverse());
  36. src = data.src;
  37. if (!srcset) {
  38. srcset = data.srcset;
  39. }
  40. }
  41. setSrc({ src, srcset }, imgElement, pictureElement);
  42. }
  43. });
  44. doc.querySelectorAll(":not(picture) > img[srcset]").forEach(imgElement => setSrc(getImgSrcData(imgElement), imgElement));
  45. }
  46. function getImgSrcData(imgElement) {
  47. let src = imgElement.getAttribute("src");
  48. if (src == EMPTY_IMAGE) {
  49. src = null;
  50. }
  51. let srcset = getSourceSrc(imgElement.getAttribute("srcset"));
  52. if (srcset == EMPTY_IMAGE) {
  53. srcset = null;
  54. }
  55. return { src, srcset };
  56. }
  57. function getSourceSrcData(sources) {
  58. let source = sources.find(source => source.src);
  59. let src = source && source.src;
  60. let srcset = source && source.srcset;
  61. if (!src) {
  62. source = sources.find(source => getSourceSrc(source.src));
  63. src = source && source.src;
  64. if (src == EMPTY_IMAGE) {
  65. src = null;
  66. }
  67. }
  68. if (!srcset) {
  69. source = sources.find(source => getSourceSrc(source.srcset));
  70. srcset = source && source.srcset;
  71. if (srcset == EMPTY_IMAGE) {
  72. srcset = null;
  73. }
  74. }
  75. return { src, srcset };
  76. }
  77. function setSrc(srcData, imgElement, pictureElement) {
  78. if (srcData.src) {
  79. imgElement.setAttribute("src", srcData.src);
  80. imgElement.setAttribute("srcset", "");
  81. imgElement.setAttribute("sizes", "");
  82. } else {
  83. imgElement.setAttribute("src", EMPTY_IMAGE);
  84. if (srcData.srcset) {
  85. imgElement.setAttribute("srcset", srcData.srcset);
  86. } else {
  87. imgElement.setAttribute("srcset", "");
  88. imgElement.setAttribute("sizes", "");
  89. }
  90. }
  91. if (pictureElement) {
  92. pictureElement.querySelectorAll("source").forEach(sourceElement => sourceElement.remove());
  93. }
  94. }
  95. function getSourceSrc(sourceSrcSet) {
  96. if (sourceSrcSet) {
  97. const srcset = singlefile.lib.vendor.srcsetParser.process(sourceSrcSet);
  98. if (srcset.length) {
  99. return (srcset.find(srcset => srcset.url)).url;
  100. }
  101. }
  102. }
  103. })();