fixed flow problems in PseudoNodeContent.js

This commit is contained in:
Matthias Christen 2017-12-15 23:46:26 +01:00
parent 78c3c7fc71
commit 6d0cd2d226
2 changed files with 39 additions and 39 deletions

View File

@ -394,7 +394,7 @@ const inlinePseudoElement = (
node: HTMLElement, node: HTMLElement,
clone: HTMLElement, clone: HTMLElement,
style: CSSStyleDeclaration, style: CSSStyleDeclaration,
contentItems: Array<PseudoContentItem>, contentItems: ?Array<PseudoContentItem>,
pseudoElt: ':before' | ':after' pseudoElt: ':before' | ':after'
): ?HTMLElement => { ): ?HTMLElement => {
if ( if (
@ -408,24 +408,25 @@ const inlinePseudoElement = (
} }
const anonymousReplacedElement = clone.ownerDocument.createElement('html2canvaspseudoelement'); const anonymousReplacedElement = clone.ownerDocument.createElement('html2canvaspseudoelement');
const len = contentItems.length;
copyCSSStyles(style, anonymousReplacedElement); copyCSSStyles(style, anonymousReplacedElement);
for (var i = 0; i < len; i++) { if (contentItems) {
const item = contentItems[i]; const len = contentItems.length;
switch (item.type) { for (var i = 0; i < len; i++) {
case PSEUDO_CONTENT_ITEM_TYPE.IMAGE: const item = contentItems[i];
const img = clone.ownerDocument.createElement('img'); switch (item.type) {
img.src = parseBackgroundImage(`url(${item.value})`)[0].args[0]; case PSEUDO_CONTENT_ITEM_TYPE.IMAGE:
img.style.opacity = '1'; const img = clone.ownerDocument.createElement('img');
anonymousReplacedElement.appendChild(img); img.src = parseBackgroundImage(`url(${item.value})`)[0].args[0];
break; img.style.opacity = '1';
case PSEUDO_CONTENT_ITEM_TYPE.TEXT: anonymousReplacedElement.appendChild(img);
anonymousReplacedElement.appendChild( break;
clone.ownerDocument.createTextNode(item.value) case PSEUDO_CONTENT_ITEM_TYPE.TEXT:
); anonymousReplacedElement.appendChild(
break; clone.ownerDocument.createTextNode(item.value)
);
break;
}
} }
} }

View File

@ -1,3 +1,6 @@
/* @flow */
'use strict';
import ListStyleTypeFormatter from 'liststyletype-formatter'; import ListStyleTypeFormatter from 'liststyletype-formatter';
export const PSEUDO_CONTENT_ITEM_TYPE = { export const PSEUDO_CONTENT_ITEM_TYPE = {
@ -27,13 +30,14 @@ export type PseudoContentItem = {
export type Token = { export type Token = {
type: $Values<typeof TOKEN_TYPE>, type: $Values<typeof TOKEN_TYPE>,
value: ?string, value?: string,
format: ?string, name?: string,
glue: ?string format?: string,
glue?: string
}; };
export const parseCounterReset = ( export const parseCounterReset = (
style: CSSStyleDeclaration, style: ?CSSStyleDeclaration,
data: PseudoContentData data: PseudoContentData
): Array<string> => { ): Array<string> => {
if (!style || !style.counterReset || style.counterReset === 'none') { if (!style || !style.counterReset || style.counterReset === 'none') {
@ -66,9 +70,9 @@ export const popCounters = (counterNames: Array<string>, data: PseudoContentData
export const resolvePseudoContent = ( export const resolvePseudoContent = (
node: Node, node: Node,
style: CSSStyleDeclaration, style: ?CSSStyleDeclaration,
data: PseudoContentData data: PseudoContentData
): Array<PseudoContentItem> => { ): ?Array<PseudoContentItem> => {
if ( if (
!style || !style ||
!style.content || !style.content ||
@ -100,24 +104,24 @@ export const resolvePseudoContent = (
const token = tokens[i]; const token = tokens[i];
switch (token.type) { switch (token.type) {
case TOKEN_TYPE.STRING: case TOKEN_TYPE.STRING:
s += token.value; s += token.value || '';
break; break;
case TOKEN_TYPE.ATTRIBUTE: case TOKEN_TYPE.ATTRIBUTE:
if (node instanceof HTMLElement) { if (node instanceof HTMLElement && token.value) {
s += node.getAttribute(token.value); s += node.getAttribute(token.value) || '';
} }
break; break;
case TOKEN_TYPE.COUNTER: case TOKEN_TYPE.COUNTER:
const counter = data.counters[token.name]; const counter = data.counters[token.name || ''];
if (counter) { if (counter) {
s += formatCounterValue([counter[counter.length - 1]], '', token.format); s += formatCounterValue([counter[counter.length - 1]], '', token.format);
} }
break; break;
case TOKEN_TYPE.COUNTERS: case TOKEN_TYPE.COUNTERS:
const counters = data.counters[token.name]; const counters = data.counters[token.name || ''];
if (counters) { if (counters) {
s += formatCounterValue(counters, token.glue, token.format); s += formatCounterValue(counters, token.glue, token.format);
} }
@ -138,7 +142,7 @@ export const resolvePseudoContent = (
contentItems.push({type: PSEUDO_CONTENT_ITEM_TYPE.TEXT, value: s}); contentItems.push({type: PSEUDO_CONTENT_ITEM_TYPE.TEXT, value: s});
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; break;
} }
} }
@ -150,12 +154,7 @@ export const resolvePseudoContent = (
return contentItems; return contentItems;
}; };
type Token = { export const parseContent = (content: string, cache?: {[string]: Array<Token>}): Array<Token> => {
type: string,
value: ?string
};
export const parseContent = (content: string, cache: ?{[string]: string}): Array<Token> => {
if (cache && cache[content]) { if (cache && cache[content]) {
return cache[content]; return cache[content];
} }
@ -302,7 +301,7 @@ export const parseContent = (content: string, cache: ?{[string]: string}): Array
return tokens; return tokens;
}; };
const addOtherToken = (tokens: Array<Token>, identifier: string): Token => { const addOtherToken = (tokens: Array<Token>, identifier: string): void => {
switch (identifier) { switch (identifier) {
case 'open-quote': case 'open-quote':
tokens.push({type: TOKEN_TYPE.OPENQUOTE}); tokens.push({type: TOKEN_TYPE.OPENQUOTE});
@ -325,15 +324,15 @@ const getQuote = (style: CSSStyleDeclaration, isOpening: boolean, quoteDepth: nu
return quotes[idx].replace(/^["']|["']$/g, ''); 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; const len = counter.length;
let result = ''; let result = '';
for (let i = 0; i < len; i++) { for (let i = 0; i < len; i++) {
if (i > 0) { if (i > 0) {
result += glue; result += glue || '';
} }
result += ListStyleTypeFormatter.format(counter[i], format, false); result += ListStyleTypeFormatter.format(counter[i], format || 'decimal', false);
} }
return result; return result;