Skip to content

Commit

Permalink
replace HTMLElement with just Element
Browse files Browse the repository at this point in the history
  • Loading branch information
HackbrettXXX committed Jul 21, 2020
1 parent a4b2ce1 commit 7dd687a
Show file tree
Hide file tree
Showing 27 changed files with 41 additions and 46 deletions.
4 changes: 2 additions & 2 deletions src/applyparseattributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { parseFill } from './fill/parseFill'
import { ColorFill } from './fill/ColorFill'
import { GState } from 'jspdf'

export function parseAttributes(context: Context, svgNode: SvgNode, node?: HTMLElement): void {
export function parseAttributes(context: Context, svgNode: SvgNode, node?: Element): void {
const domNode = node || svgNode.element
const visibility = getAttribute(domNode, context.styleSheets, 'visibility')
if (visibility) {
Expand Down Expand Up @@ -129,7 +129,7 @@ export function parseAttributes(context: Context, svgNode: SvgNode, node?: HTMLE
export function applyAttributes(
childContext: Context,
parentContext: Context,
node: HTMLElement
node: Element
): void {
let fillOpacity = 1.0,
strokeOpacity = 1.0
Expand Down
2 changes: 1 addition & 1 deletion src/context/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export interface ContextOptions {
}

export interface Svg2pdfParameters {
element: HTMLElement
element: Element
x?: number
y?: number
width?: number
Expand Down
6 changes: 3 additions & 3 deletions src/context/stylesheets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { compare as compareSpecificity } from 'specificity'
import { nodeIs } from '../utils/node'

export class StyleSheets {
private rootSvg: HTMLElement
private rootSvg: Element
private readonly loadExternalSheets: boolean
private readonly styleSheets: CSSStyleSheet[]

constructor(rootSvg: HTMLElement, loadExtSheets: boolean) {
constructor(rootSvg: Element, loadExtSheets: boolean) {
this.rootSvg = rootSvg
this.loadExternalSheets = loadExtSheets
this.styleSheets = []
Expand Down Expand Up @@ -159,7 +159,7 @@ export class StyleSheets {
)
}

getPropertyValue(node: HTMLElement, propertyCss: string): string | undefined {
getPropertyValue(node: Element, propertyCss: string): string | undefined {
const matchingRules = []
for (const sheet of this.styleSheets) {
for (let i = 0; i < sheet.cssRules.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/circle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class Circle extends EllipseBase {
return this.getR(context)
}

constructor(node: HTMLElement, children: SvgNode[]) {
constructor(node: Element, children: SvgNode[]) {
super(node, children)
}
}
2 changes: 1 addition & 1 deletion src/nodes/ellipse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { SvgNode } from './svgnode'
import { Context } from '../context/context'

export class Ellipse extends EllipseBase {
constructor(element: HTMLElement, children: SvgNode[]) {
constructor(element: Element, children: SvgNode[]) {
super(element, children)
}

Expand Down
2 changes: 1 addition & 1 deletion src/nodes/ellipsebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export abstract class EllipseBase extends GeometryNode {
abstract getRx(context: Context): number
abstract getRy(context: Context): number

protected constructor(element: HTMLElement, children: SvgNode[]) {
protected constructor(element: Element, children: SvgNode[]) {
super(false, element, children)
}

Expand Down
2 changes: 1 addition & 1 deletion src/nodes/geometrynode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export abstract class GeometryNode extends GraphicsNode {
private readonly hasMarkers: boolean
private cachedPath: Path | null = null

protected constructor(hasMarkers: boolean, element: HTMLElement, children: SvgNode[]) {
protected constructor(hasMarkers: boolean, element: Element, children: SvgNode[]) {
super(element, children)
this.hasMarkers = hasMarkers
}
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/gradient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export abstract class Gradient extends NonRenderedNode {

protected constructor(
pdfGradientType: ShadingPatternType,
element: HTMLElement,
element: Element,
children: SvgNode[]
) {
super(element, children)
Expand Down
5 changes: 2 additions & 3 deletions src/nodes/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class ImageNode extends GraphicsNode {
private readonly imageLoadingPromise: Promise<{ data: string; format: string }> | null = null
private readonly imageUrl: string | null

constructor(element: HTMLElement, children: SvgNode[]) {
constructor(element: Element, children: SvgNode[]) {
super(element, children)
this.imageUrl = this.element.getAttribute('xlink:href') || this.element.getAttribute('href')

Expand Down Expand Up @@ -45,8 +45,7 @@ export class ImageNode extends GraphicsNode {

if (format.indexOf('svg') === 0) {
const parser = new DOMParser()
const svgElement = parser.parseFromString(data, 'image/svg+xml')
.firstElementChild as HTMLElement
const svgElement = parser.parseFromString(data, 'image/svg+xml').firstElementChild as Element

// unless preserveAspectRatio starts with "defer", the preserveAspectRatio attribute of the svg is ignored
const preserveAspectRatio = this.element.getAttribute('preserveAspectRatio')
Expand Down
4 changes: 2 additions & 2 deletions src/nodes/line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { SvgNode } from './svgnode'
import { Matrix } from 'jspdf'

export class Line extends GeometryNode {
constructor(node: HTMLElement, children: SvgNode[]) {
constructor(node: Element, children: SvgNode[]) {
super(true, node, children)
}

Expand All @@ -31,7 +31,7 @@ export class Line extends GeometryNode {
return context.pdf.unitMatrix
}

isVisible(parentVisible: boolean, context:Context): boolean {
isVisible(parentVisible: boolean, context: Context): boolean {
return svgNodeIsVisible(this, parentVisible, context)
}

Expand Down
2 changes: 1 addition & 1 deletion src/nodes/lineargradient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Gradient } from './gradient'
import { SvgNode } from './svgnode'

export class LinearGradient extends Gradient {
constructor(element: HTMLElement, children: SvgNode[]) {
constructor(element: Element, children: SvgNode[]) {
super('axial', element, children)
}

Expand Down
2 changes: 1 addition & 1 deletion src/nodes/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { toCubic } from '../utils/geometry'
import { Matrix } from 'jspdf'

export class PathNode extends GeometryNode {
constructor(node: HTMLElement, children: SvgNode[]) {
constructor(node: Element, children: SvgNode[]) {
super(true, node, children)
}

Expand Down
2 changes: 1 addition & 1 deletion src/nodes/polygon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Traverse } from './traverse'
import { SvgNode } from './svgnode'

export class Polygon extends Traverse {
constructor(node: HTMLElement, children: SvgNode[]) {
constructor(node: Element, children: SvgNode[]) {
super(true, node, children)
}
}
2 changes: 1 addition & 1 deletion src/nodes/polyline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Traverse } from './traverse'
import { SvgNode } from './svgnode'

export class Polyline extends Traverse {
constructor(node: HTMLElement, children: SvgNode[]) {
constructor(node: Element, children: SvgNode[]) {
super(false, node, children)
}
}
2 changes: 1 addition & 1 deletion src/nodes/radialgradient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Gradient } from './gradient'
import { SvgNode } from './svgnode'

export class RadialGradient extends Gradient {
constructor(element: HTMLElement, children: SvgNode[]) {
constructor(element: Element, children: SvgNode[]) {
super('radial', element, children)
}

Expand Down
2 changes: 1 addition & 1 deletion src/nodes/rect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { SvgNode } from './svgnode'
import { Matrix } from 'jspdf'

export class Rect extends GeometryNode {
constructor(element: HTMLElement, children: SvgNode[]) {
constructor(element: Element, children: SvgNode[]) {
super(false, element, children)
}

Expand Down
4 changes: 2 additions & 2 deletions src/nodes/svgnode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { Rect } from '../utils/geometry'
import { Matrix } from 'jspdf'

export abstract class SvgNode {
readonly element: HTMLElement
readonly element: Element
readonly children: SvgNode[]

constructor(element: HTMLElement, children: SvgNode[]) {
constructor(element: Element, children: SvgNode[]) {
this.element = element
this.children = children
}
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class TextNode extends GraphicsNode {
)

for (let i = 0; i < this.element.childNodes.length; i++) {
const textNode = this.element.childNodes[i] as HTMLElement
const textNode = this.element.childNodes[i] as Element
if (!textNode.textContent) {
continue
}
Expand Down
4 changes: 2 additions & 2 deletions src/nodes/traverse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Matrix } from 'jspdf'
export abstract class Traverse extends GeometryNode {
private readonly closed: boolean

protected constructor(closed: boolean, node: HTMLElement, children: SvgNode[]) {
protected constructor(closed: boolean, node: Element, children: SvgNode[]) {
super(true, node, children)
this.closed = closed
}
Expand Down Expand Up @@ -43,7 +43,7 @@ export abstract class Traverse extends GeometryNode {
return path
}

isVisible(parentVisible: boolean, context:Context): boolean {
isVisible(parentVisible: boolean, context: Context): boolean {
return svgNodeIsVisible(this, parentVisible, context)
}

Expand Down
2 changes: 1 addition & 1 deletion src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import cssesc from 'cssesc'
import { ClipPath } from './nodes/clippath'
import { Symbol } from './nodes/symbol'

export function parse(node: HTMLElement, idMap?: { [id: string]: SvgNode }): SvgNode {
export function parse(node: Element, idMap?: { [id: string]: SvgNode }): SvgNode {
let svgnode: SvgNode
const children: SvgNode[] = []

Expand Down
4 changes: 2 additions & 2 deletions src/svg2pdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { StyleSheets } from './context/stylesheets'
import { Viewport } from './context/viewport'

export async function svg2pdf(
element: HTMLElement,
element: Element,
pdf: jsPDF,
options: Svg2PdfOptions = {}
): Promise<jsPDF> {
Expand Down Expand Up @@ -56,7 +56,7 @@ export async function svg2pdf(
}

jsPDF.API.svg = function(
element: HTMLElement,
element: Element,
options: Svg2PdfOptions = {}
): ReturnType<typeof svg2pdf> {
return svg2pdf(element, this, options)
Expand Down
4 changes: 2 additions & 2 deletions src/textchunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { ColorFill } from './fill/ColorFill'
export class TextChunk {
private readonly textNode: TextNode
private readonly texts: string[]
private readonly textNodes: HTMLElement[]
private readonly textNodes: Element[]
private readonly textAnchor: string
private readonly originX: number
private readonly originY: number
Expand All @@ -32,7 +32,7 @@ export class TextChunk {
this.originY = originY
}

add(tSpan: HTMLElement, text: string): void {
add(tSpan: Element, text: string): void {
this.texts.push(text)
this.textNodes.push(tSpan)
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/bbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function getBoundingBoxByChildren(context: Context, svgnode: SvgNode): nu
return boundingBox
}

export function defaultBoundingBox(element: HTMLElement, context: Context): Rect {
export function defaultBoundingBox(element: Element, context: Context): Rect {
const pf: any = parseFloat
// TODO: check if there are other possible coordinate attributes
const x1 =
Expand Down
8 changes: 4 additions & 4 deletions src/utils/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@ export function nodeIs(node: Element, tagsString: string): boolean {
return tagsString.split(',').indexOf((node.nodeName || node.tagName).toLowerCase()) >= 0
}

export function forEachChild(node: HTMLElement, fn: (n: number, e: HTMLElement) => void): void {
export function forEachChild(node: Element, fn: (n: number, e: Element) => void): void {
// copy list of children, as the original might be modified
const children = []
for (let i = 0; i < node.childNodes.length; i++) {
const childNode = node.childNodes[i]
if (childNode.nodeName.charAt(0) !== '#') children.push(childNode)
}
for (let i = 0; i < children.length; i++) {
fn(i, children[i] as HTMLElement)
fn(i, children[i] as Element)
}
}

// returns an attribute of a node, either from the node directly or from css
export function getAttribute(
node: HTMLElement,
node: Element,
styleSheets: StyleSheets,
propertyNode: string,
propertyCss = propertyNode
): string | undefined {
const attribute = node.style.getPropertyValue(propertyCss)
const attribute = (node as SVGElement).style.getPropertyValue(propertyCss)
if (attribute) {
return attribute
} else if (styleSheets.getPropertyValue(node, propertyCss)) {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function consolidateSpaces(str: string): string {
}

// applies text transformations to a text node
export function transformText(node: HTMLElement, text: string, context: Context): string {
export function transformText(node: Element, text: string, context: Context): string {
const textTransform = getAttribute(node, context.styleSheets, 'text-transform')
switch (textTransform) {
case 'uppercase':
Expand Down
2 changes: 1 addition & 1 deletion src/utils/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { parseFloats } from './parsing'
import { Matrix } from 'jspdf'

export function computeViewBoxTransform(
node: HTMLElement,
node: Element,
viewBox: number[],
eX: number,
eY: number,
Expand Down
10 changes: 3 additions & 7 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ declare module 'svg2pdf.js' {
* @param pdf The jsPDF object.
* @param options An object that may contain render options.
*/
export function svg2pdf(
element: HTMLElement,
pdf: jsPDF,
options?: Svg2PdfOptions
): Promise<jsPDF>
export function svg2pdf(element: Element, pdf: jsPDF, options?: Svg2PdfOptions): Promise<jsPDF>
}

declare module 'jspdf' {
Expand All @@ -23,10 +19,10 @@ declare module 'jspdf' {
* @param element The svg element, which will be cloned, so the original stays unchanged.
* @param options An object that may contain render options.
*/
svg(element: HTMLElement, options?: Svg2PdfOptions): Promise<jsPDF>
svg(element: Element, options?: Svg2PdfOptions): Promise<jsPDF>
}
interface jsPDFAPI {
svg(this: jsPDF, element: HTMLElement, options?: Svg2PdfOptions): Promise<jsPDF>
svg(this: jsPDF, element: Element, options?: Svg2PdfOptions): Promise<jsPDF>
}
}

Expand Down

0 comments on commit 7dd687a

Please sign in to comment.