Skip to content

Commit

Permalink
Handle <link> nodes that have multiple rel values and/or use `rel…
Browse files Browse the repository at this point in the history
…="preload"` (#646)
  • Loading branch information
MatthewHerbst authored Sep 22, 2023
1 parent 293394a commit 5f1c144
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 7 deletions.
17 changes: 17 additions & 0 deletions examples/ComponentToPrint/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export class ComponentToPrint extends React.PureComponent<Props, State> {
href="../disabled.css"
/>
<style type="text/css" media="print">{"@page { size: landscape; }"}</style>
<link href="./as-style.css" rel="stylesheet" />
<div className="flash" />
<table className="testClass">
<thead>
Expand All @@ -66,6 +67,22 @@ export class ComponentToPrint extends React.PureComponent<Props, State> {
</tr>
</thead>
<tbody>
<tr>
<td>
Test loading {"<link>"}s with multi-value `rel`
</td>
<td>
<div className="multi-rel">Purple Background</div>
</td>
</tr>
<tr>
<td>
Test loading {"<link>"}s with `as="style"`
</td>
<td>
<div className="as-style">Blue Background</div>
</td>
</tr>
<tr>
<td>Canvass</td>
<td>
Expand Down
5 changes: 3 additions & 2 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<head>
<title>ReactToPrint Examples</title>
<link href="./title.css" rel="stylesheet" type="text/css" />
<link href="./double-rel.css" rel="prefetch stylesheet" type="text/css" />
<link href="./as-style.css" rel="preload" as="style" />
<link disabled href="./disabled.css" rel="stylesheet" type="text/css" />
<link disabled="true" href="./disabled.css" rel="stylesheet" type="text/css" />
<link disabled="false" href="./disabled.css" rel="stylesheet" type="text/css" />
Expand Down Expand Up @@ -40,5 +42,4 @@
</style>
<div id="app-root" />
</body>
</html>

</html>
5 changes: 5 additions & 0 deletions examples/styles/as-style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.as-style {
background-color: blue;
height: 20px;
width: 100%;
}
5 changes: 5 additions & 0 deletions examples/styles/double-rel.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.multi-rel {
background-color: purple;
height: 20px;
width: 100%;
}
11 changes: 6 additions & 5 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,14 @@ export default class ReactToPrint extends React.Component<IReactToPrintProps> {
const clonedContentNodes = contentNodes.cloneNode(true);
const isText = clonedContentNodes instanceof Text;

const globalStyleLinkNodes = document.querySelectorAll("link[rel='stylesheet']");
const globalLinkNodes = document.querySelectorAll("link[rel~='stylesheet'], link[as='style']");
const renderComponentImgNodes = isText ? [] : (clonedContentNodes as Element).querySelectorAll("img");
const renderComponentVideoNodes = isText ? [] : (clonedContentNodes as Element).querySelectorAll("video");

const numFonts = fonts ? fonts.length : 0;

this.numResourcesToLoad =
globalStyleLinkNodes.length +
globalLinkNodes.length +
renderComponentImgNodes.length +
renderComponentVideoNodes.length +
numFonts;
Expand Down Expand Up @@ -434,9 +434,10 @@ export default class ReactToPrint extends React.Component<IReactToPrintProps> {
}

if (copyStyles) {
const headEls = document.querySelectorAll("style, link[rel='stylesheet']");
for (let i = 0, headElsLen = headEls.length; i < headElsLen; ++i) {
const node = headEls[i];
const styleAndLinkNodes = document.querySelectorAll("style, link[rel~='stylesheet'], link[as='style']");

for (let i = 0, styleAndLinkNodesLen = styleAndLinkNodes.length; i < styleAndLinkNodesLen; ++i) {
const node = styleAndLinkNodes[i];

if (node.tagName.toLowerCase() === 'style') { // <style> nodes
const newHeadEl = domDoc.createElement(node.tagName);
Expand Down

0 comments on commit 5f1c144

Please sign in to comment.