Skip to content

Commit

Permalink
Process computed styles for backwards compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
crabbly committed May 26, 2019
1 parent b330398 commit 54d228a
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 53 deletions.
52 changes: 1 addition & 51 deletions src/js/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function collectStyles (element, params) {
}
})

// Print friendly defaults
// Print friendly defaults (deprecated)
elementStyle += 'max-width: ' + params.maxWidth + 'px !important;' + params.font_size + ' !important;'

return elementStyle
Expand All @@ -39,56 +39,6 @@ function targetStylesMatch (styles, value) {
return false
}

export function loopNodesCollectStyles (elements, params) {
for (let n = 0; n < elements.length; n++) {
let currentElement = elements[n]

// Check if we are skiping this element
if (params.ignoreElements.indexOf(currentElement.getAttribute('id')) !== -1) {
currentElement.parentNode.removeChild(currentElement)
continue
}

// Form Printing - check if is element Input
let tag = currentElement.tagName
if (tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT') {
// Save style to variable
let textStyle = collectStyles(currentElement, params)

// Remove INPUT element and insert a text node
let parent = currentElement.parentNode

// Get text value
let textNode = tag === 'SELECT'
? document.createTextNode(currentElement.options[currentElement.selectedIndex].text)
: document.createTextNode(currentElement.value)

// Create text element
let textElement = document.createElement('div')
textElement.appendChild(textNode)

// Add style to text
textElement.setAttribute('style', textStyle)

// Add text
parent.appendChild(textElement)

// Remove input
parent.removeChild(currentElement)
} else {
// Get all styling for print element
currentElement.setAttribute('style', collectStyles(currentElement, params))
}

// Check if we have more elements in the tree
let children = currentElement.children

if (children && children.length) {
loopNodesCollectStyles(children, params)
}
}
}

export function addHeader (printElement, params) {
// Create the header container div
let headerContainer = document.createElement('div')
Expand Down
6 changes: 5 additions & 1 deletion src/js/html.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { addHeader } from './functions'
import { collectStyles, addHeader } from './functions'
import Print from './print'

export default {
Expand Down Expand Up @@ -43,6 +43,10 @@ function cloneElement (element, params) {
clone.appendChild(clonedChild)
}

// Get all styling for print element (for nodes of type element only)
if (element.nodeType === 1)
clone.setAttribute('style', collectStyles(element, params))

// Check if the element needs any state processing (copy user input data)
switch (element.tagName) {
case 'SELECT':
Expand Down
2 changes: 1 addition & 1 deletion src/js/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export default {
printFrame.srcdoc = '<html><head><title>' + params.documentTitle + '</title>'

// Attach css files
if (params.css !== null) {
if (params.css) {
// Add support for single file
if (!Array.isArray(params.css)) params.css = [params.css]

Expand Down

0 comments on commit 54d228a

Please sign in to comment.