Skip to content

Commit

Permalink
fixed flow problems in PseudoNodeContent.js
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias-christen committed Dec 15, 2017
1 parent 78c3c7f commit 6d0cd2d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 39 deletions.
35 changes: 18 additions & 17 deletions src/Clone.js
Expand Up @@ -394,7 +394,7 @@ const inlinePseudoElement = (
node: HTMLElement,
clone: HTMLElement,
style: CSSStyleDeclaration,
contentItems: Array<PseudoContentItem>,
contentItems: ?Array<PseudoContentItem>,
pseudoElt: ':before' | ':after'
): ?HTMLElement => {
if (
Expand All @@ -408,24 +408,25 @@ const inlinePseudoElement = (
}

const anonymousReplacedElement = clone.ownerDocument.createElement('html2canvaspseudoelement');
const len = contentItems.length;

copyCSSStyles(style, anonymousReplacedElement);

for (var i = 0; i < len; i++) {
const item = contentItems[i];
switch (item.type) {
case PSEUDO_CONTENT_ITEM_TYPE.IMAGE:
const img = clone.ownerDocument.createElement('img');
img.src = parseBackgroundImage(`url(${item.value})`)[0].args[0];
img.style.opacity = '1';
anonymousReplacedElement.appendChild(img);
break;
case PSEUDO_CONTENT_ITEM_TYPE.TEXT:
anonymousReplacedElement.appendChild(
clone.ownerDocument.createTextNode(item.value)
);
break;
if (contentItems) {
const len = contentItems.length;
for (var i = 0; i < len; i++) {
const item = contentItems[i];
switch (item.type) {
case PSEUDO_CONTENT_ITEM_TYPE.IMAGE:
const img = clone.ownerDocument.createElement('img');
img.src = parseBackgroundImage(`url(${item.value})`)[0].args[0];
img.style.opacity = '1';
anonymousReplacedElement.appendChild(img);
break;
case PSEUDO_CONTENT_ITEM_TYPE.TEXT:
anonymousReplacedElement.appendChild(
clone.ownerDocument.createTextNode(item.value)
);
break;
}
}
}

Expand Down
43 changes: 21 additions & 22 deletions src/PseudoNodeContent.js
@@ -1,3 +1,6 @@
/* @flow */
'use strict';

import ListStyleTypeFormatter from 'liststyletype-formatter';

export const PSEUDO_CONTENT_ITEM_TYPE = {
Expand Down Expand Up @@ -27,13 +30,14 @@ export type PseudoContentItem = {

export type Token = {
type: $Values<typeof TOKEN_TYPE>,
value: ?string,
format: ?string,
glue: ?string
value?: string,
name?: string,
format?: string,
glue?: string
};

export const parseCounterReset = (
style: CSSStyleDeclaration,
style: ?CSSStyleDeclaration,
data: PseudoContentData
): Array<string> => {
if (!style || !style.counterReset || style.counterReset === 'none') {
Expand Down Expand Up @@ -66,9 +70,9 @@ export const popCounters = (counterNames: Array<string>, data: PseudoContentData

export const resolvePseudoContent = (
node: Node,
style: CSSStyleDeclaration,
style: ?CSSStyleDeclaration,
data: PseudoContentData
): Array<PseudoContentItem> => {
): ?Array<PseudoContentItem> => {
if (
!style ||
!style.content ||
Expand Down Expand Up @@ -100,24 +104,24 @@ export const resolvePseudoContent = (
const token = tokens[i];
switch (token.type) {
case TOKEN_TYPE.STRING:
s += token.value;
s += token.value || '';
break;

case TOKEN_TYPE.ATTRIBUTE:
if (node instanceof HTMLElement) {
s += node.getAttribute(token.value);
if (node instanceof HTMLElement && token.value) {
s += node.getAttribute(token.value) || '';
}
break;

case TOKEN_TYPE.COUNTER:
const counter = data.counters[token.name];
const counter = data.counters[token.name || ''];
if (counter) {
s += formatCounterValue([counter[counter.length - 1]], '', token.format);
}
break;

case TOKEN_TYPE.COUNTERS:
const counters = data.counters[token.name];
const counters = data.counters[token.name || ''];
if (counters) {
s += formatCounterValue(counters, token.glue, token.format);
}
Expand All @@ -138,7 +142,7 @@ export const resolvePseudoContent = (
contentItems.push({type: PSEUDO_CONTENT_ITEM_TYPE.TEXT, value: s});
s = '';
}
contentItems.push({type: PSEUDO_CONTENT_ITEM_TYPE.IMAGE, value: token.value});
contentItems.push({type: PSEUDO_CONTENT_ITEM_TYPE.IMAGE, value: token.value || ''});
break;
}
}
Expand All @@ -150,12 +154,7 @@ export const resolvePseudoContent = (
return contentItems;
};

type Token = {
type: string,
value: ?string
};

export const parseContent = (content: string, cache: ?{[string]: string}): Array<Token> => {
export const parseContent = (content: string, cache?: {[string]: Array<Token>}): Array<Token> => {
if (cache && cache[content]) {
return cache[content];
}
Expand Down Expand Up @@ -302,7 +301,7 @@ export const parseContent = (content: string, cache: ?{[string]: string}): Array
return tokens;
};
const addOtherToken = (tokens: Array<Token>, identifier: string): Token => {
const addOtherToken = (tokens: Array<Token>, identifier: string): void => {
switch (identifier) {
case 'open-quote':
tokens.push({type: TOKEN_TYPE.OPENQUOTE});
Expand All @@ -325,15 +324,15 @@ const getQuote = (style: CSSStyleDeclaration, isOpening: boolean, quoteDepth: nu
return quotes[idx].replace(/^["']|["']$/g, '');
};

const formatCounterValue = (counter, glue: string, format: string): string => {
const formatCounterValue = (counter, glue: ?string, format: ?string): string => {
const len = counter.length;
let result = '';

for (let i = 0; i < len; i++) {
if (i > 0) {
result += glue;
result += glue || '';
}
result += ListStyleTypeFormatter.format(counter[i], format, false);
result += ListStyleTypeFormatter.format(counter[i], format || 'decimal', false);
}

return result;
Expand Down

0 comments on commit 6d0cd2d

Please sign in to comment.