Update to use list style parser from ListItem

This commit is contained in:
Niklas von Hertzen 2017-12-24 17:08:54 +08:00
parent afa5d7cb8e
commit 9046e0d554
5 changed files with 93 additions and 53 deletions

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "html2canvas", "name": "html2canvas",
"version": "1.0.0-alpha.4", "version": "1.0.0-alpha.5",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View File

@ -77,7 +77,6 @@
"homepage": "https://html2canvas.hertzen.com", "homepage": "https://html2canvas.hertzen.com",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"punycode": "2.1.0", "punycode": "2.1.0"
"liststyletype-formatter": "latest"
} }
} }

View File

@ -92,7 +92,7 @@ export const inlineListItemElement = (
} }
} else if (typeof container.listIndex === 'number') { } else if (typeof container.listIndex === 'number') {
text = node.ownerDocument.createTextNode( text = node.ownerDocument.createTextNode(
createCounterText(container.listIndex, listStyle.listStyleType) createCounterText(container.listIndex, listStyle.listStyleType, true)
); );
wrapper.appendChild(text); wrapper.appendChild(text);
wrapper.style.top = `${container.bounds.top - MARGIN_TOP}px`; wrapper.style.top = `${container.bounds.top - MARGIN_TOP}px`;
@ -363,10 +363,10 @@ const createAdditiveCounter = (
max: number, max: number,
symbols, symbols,
fallback: ListStyleType, fallback: ListStyleType,
suffix: string = '. ' suffix: string
) => { ) => {
if (value < min || value > max) { if (value < min || value > max) {
return createCounterText(value, fallback); return createCounterText(value, fallback, suffix.length > 0);
} }
return ( return (
@ -404,7 +404,7 @@ const createCounterStyleFromRange = (
codePointRangeStart: number, codePointRangeStart: number,
codePointRangeEnd: number, codePointRangeEnd: number,
isNumeric: boolean, isNumeric: boolean,
suffix: string = '. ' suffix: string
): string => { ): string => {
const codePointRangeLength = codePointRangeEnd - codePointRangeStart + 1; const codePointRangeLength = codePointRangeEnd - codePointRangeStart + 1;
@ -451,7 +451,7 @@ const createCJKCounter = (
flags: number flags: number
): string => { ): string => {
if (value < -9999 || value > 9999) { if (value < -9999 || value > 9999) {
return createCounterText(value, LIST_STYLE_TYPE.CJK_DECIMAL); return createCounterText(value, LIST_STYLE_TYPE.CJK_DECIMAL, suffix.length > 0);
} }
let tmp = Math.abs(value); let tmp = Math.abs(value);
let string = suffix; let string = suffix;
@ -490,7 +490,14 @@ const CHINESE_FORMAL_MULTIPLIERS = '拾佰仟萬';
const JAPANESE_NEGATIVE = 'マイナス'; const JAPANESE_NEGATIVE = 'マイナス';
const KOREAN_NEGATIVE = '마이너스 '; const KOREAN_NEGATIVE = '마이너스 ';
const createCounterText = (value: number, type: ListStyleType): string => { export const createCounterText = (
value: number,
type: ListStyleType,
appendSuffix: boolean
): string => {
const defaultSuffix = appendSuffix ? '. ' : '';
const cjkSuffix = appendSuffix ? '、' : '';
const koreanSuffix = appendSuffix ? ', ' : '';
switch (type) { switch (type) {
case LIST_STYLE_TYPE.DISC: case LIST_STYLE_TYPE.DISC:
return '•'; return '•';
@ -499,48 +506,64 @@ const createCounterText = (value: number, type: ListStyleType): string => {
case LIST_STYLE_TYPE.SQUARE: case LIST_STYLE_TYPE.SQUARE:
return '◾'; return '◾';
case LIST_STYLE_TYPE.DECIMAL_LEADING_ZERO: case LIST_STYLE_TYPE.DECIMAL_LEADING_ZERO:
const string = createCounterStyleFromRange(value, 48, 57, true); const string = createCounterStyleFromRange(value, 48, 57, true, defaultSuffix);
return string.length < 4 ? `0${string}` : string; return string.length < 4 ? `0${string}` : string;
case LIST_STYLE_TYPE.CJK_DECIMAL: case LIST_STYLE_TYPE.CJK_DECIMAL:
return createCounterStyleFromSymbols(value, '〇一二三四五六七八九', '、'); return createCounterStyleFromSymbols(value, '〇一二三四五六七八九', cjkSuffix);
case LIST_STYLE_TYPE.LOWER_ROMAN: case LIST_STYLE_TYPE.LOWER_ROMAN:
return createAdditiveCounter( return createAdditiveCounter(
value, value,
1, 1,
3999, 3999,
ROMAN_UPPER, ROMAN_UPPER,
LIST_STYLE_TYPE.DECIMAL LIST_STYLE_TYPE.DECIMAL,
defaultSuffix
).toLowerCase(); ).toLowerCase();
case LIST_STYLE_TYPE.UPPER_ROMAN: case LIST_STYLE_TYPE.UPPER_ROMAN:
return createAdditiveCounter(value, 1, 3999, ROMAN_UPPER, LIST_STYLE_TYPE.DECIMAL); return createAdditiveCounter(
value,
1,
3999,
ROMAN_UPPER,
LIST_STYLE_TYPE.DECIMAL,
defaultSuffix
);
case LIST_STYLE_TYPE.LOWER_GREEK: case LIST_STYLE_TYPE.LOWER_GREEK:
return createCounterStyleFromRange(value, 945, 969, false); return createCounterStyleFromRange(value, 945, 969, false, defaultSuffix);
case LIST_STYLE_TYPE.LOWER_ALPHA: case LIST_STYLE_TYPE.LOWER_ALPHA:
return createCounterStyleFromRange(value, 97, 122, false); return createCounterStyleFromRange(value, 97, 122, false, defaultSuffix);
case LIST_STYLE_TYPE.UPPER_ALPHA: case LIST_STYLE_TYPE.UPPER_ALPHA:
return createCounterStyleFromRange(value, 65, 90, false); return createCounterStyleFromRange(value, 65, 90, false, defaultSuffix);
case LIST_STYLE_TYPE.ARABIC_INDIC: case LIST_STYLE_TYPE.ARABIC_INDIC:
return createCounterStyleFromRange(value, 1632, 1641, true); return createCounterStyleFromRange(value, 1632, 1641, true, defaultSuffix);
case LIST_STYLE_TYPE.ARMENIAN: case LIST_STYLE_TYPE.ARMENIAN:
case LIST_STYLE_TYPE.UPPER_ARMENIAN: case LIST_STYLE_TYPE.UPPER_ARMENIAN:
return createAdditiveCounter(value, 1, 9999, ARMENIAN, LIST_STYLE_TYPE.DECIMAL); return createAdditiveCounter(
value,
1,
9999,
ARMENIAN,
LIST_STYLE_TYPE.DECIMAL,
defaultSuffix
);
case LIST_STYLE_TYPE.LOWER_ARMENIAN: case LIST_STYLE_TYPE.LOWER_ARMENIAN:
return createAdditiveCounter( return createAdditiveCounter(
value, value,
1, 1,
9999, 9999,
ARMENIAN, ARMENIAN,
LIST_STYLE_TYPE.DECIMAL LIST_STYLE_TYPE.DECIMAL,
defaultSuffix
).toLowerCase(); ).toLowerCase();
case LIST_STYLE_TYPE.BENGALI: case LIST_STYLE_TYPE.BENGALI:
return createCounterStyleFromRange(value, 2534, 2543, true); return createCounterStyleFromRange(value, 2534, 2543, true, defaultSuffix);
case LIST_STYLE_TYPE.CAMBODIAN: case LIST_STYLE_TYPE.CAMBODIAN:
case LIST_STYLE_TYPE.KHMER: case LIST_STYLE_TYPE.KHMER:
return createCounterStyleFromRange(value, 6112, 6121, true); return createCounterStyleFromRange(value, 6112, 6121, true, defaultSuffix);
case LIST_STYLE_TYPE.CJK_EARTHLY_BRANCH: case LIST_STYLE_TYPE.CJK_EARTHLY_BRANCH:
return createCounterStyleFromSymbols(value, '子丑寅卯辰巳午未申酉戌亥', '、'); return createCounterStyleFromSymbols(value, '子丑寅卯辰巳午未申酉戌亥', cjkSuffix);
case LIST_STYLE_TYPE.CJK_HEAVENLY_STEM: case LIST_STYLE_TYPE.CJK_HEAVENLY_STEM:
return createCounterStyleFromSymbols(value, '甲乙丙丁戊己庚辛壬癸', '、'); return createCounterStyleFromSymbols(value, '甲乙丙丁戊己庚辛壬癸', cjkSuffix);
case LIST_STYLE_TYPE.CJK_IDEOGRAPHIC: case LIST_STYLE_TYPE.CJK_IDEOGRAPHIC:
case LIST_STYLE_TYPE.TRAD_CHINESE_INFORMAL: case LIST_STYLE_TYPE.TRAD_CHINESE_INFORMAL:
return createCJKCounter( return createCJKCounter(
@ -548,7 +571,7 @@ const createCounterText = (value: number, type: ListStyleType): string => {
'零一二三四五六七八九', '零一二三四五六七八九',
CHINESE_INFORMAL_MULTIPLIERS, CHINESE_INFORMAL_MULTIPLIERS,
'負', '負',
'、', cjkSuffix,
CJK_TEN_COEFFICIENTS | CJK_TEN_HIGH_COEFFICIENTS | CJK_HUNDRED_COEFFICIENTS CJK_TEN_COEFFICIENTS | CJK_TEN_HIGH_COEFFICIENTS | CJK_HUNDRED_COEFFICIENTS
); );
case LIST_STYLE_TYPE.TRAD_CHINESE_FORMAL: case LIST_STYLE_TYPE.TRAD_CHINESE_FORMAL:
@ -557,7 +580,7 @@ const createCounterText = (value: number, type: ListStyleType): string => {
'零壹貳參肆伍陸柒捌玖', '零壹貳參肆伍陸柒捌玖',
CHINESE_FORMAL_MULTIPLIERS, CHINESE_FORMAL_MULTIPLIERS,
'負', '負',
'、', cjkSuffix,
CJK_ZEROS | CJK_ZEROS |
CJK_TEN_COEFFICIENTS | CJK_TEN_COEFFICIENTS |
CJK_TEN_HIGH_COEFFICIENTS | CJK_TEN_HIGH_COEFFICIENTS |
@ -569,7 +592,7 @@ const createCounterText = (value: number, type: ListStyleType): string => {
'零一二三四五六七八九', '零一二三四五六七八九',
CHINESE_INFORMAL_MULTIPLIERS, CHINESE_INFORMAL_MULTIPLIERS,
'负', '负',
'、', cjkSuffix,
CJK_TEN_COEFFICIENTS | CJK_TEN_HIGH_COEFFICIENTS | CJK_HUNDRED_COEFFICIENTS CJK_TEN_COEFFICIENTS | CJK_TEN_HIGH_COEFFICIENTS | CJK_HUNDRED_COEFFICIENTS
); );
case LIST_STYLE_TYPE.SIMP_CHINESE_FORMAL: case LIST_STYLE_TYPE.SIMP_CHINESE_FORMAL:
@ -578,21 +601,21 @@ const createCounterText = (value: number, type: ListStyleType): string => {
'零壹贰叁肆伍陆柒捌玖', '零壹贰叁肆伍陆柒捌玖',
CHINESE_FORMAL_MULTIPLIERS, CHINESE_FORMAL_MULTIPLIERS,
'负', '负',
'、', cjkSuffix,
CJK_ZEROS | CJK_ZEROS |
CJK_TEN_COEFFICIENTS | CJK_TEN_COEFFICIENTS |
CJK_TEN_HIGH_COEFFICIENTS | CJK_TEN_HIGH_COEFFICIENTS |
CJK_HUNDRED_COEFFICIENTS CJK_HUNDRED_COEFFICIENTS
); );
case LIST_STYLE_TYPE.JAPANESE_INFORMAL: case LIST_STYLE_TYPE.JAPANESE_INFORMAL:
return createCJKCounter(value, '〇一二三四五六七八九', '十百千万', JAPANESE_NEGATIVE, '、', 0); return createCJKCounter(value, '〇一二三四五六七八九', '十百千万', JAPANESE_NEGATIVE, cjkSuffix, 0);
case LIST_STYLE_TYPE.JAPANESE_FORMAL: case LIST_STYLE_TYPE.JAPANESE_FORMAL:
return createCJKCounter( return createCJKCounter(
value, value,
'零壱弐参四伍六七八九', '零壱弐参四伍六七八九',
'拾百千万', '拾百千万',
JAPANESE_NEGATIVE, JAPANESE_NEGATIVE,
'、', cjkSuffix,
CJK_ZEROS | CJK_TEN_COEFFICIENTS | CJK_TEN_HIGH_COEFFICIENTS CJK_ZEROS | CJK_TEN_COEFFICIENTS | CJK_TEN_HIGH_COEFFICIENTS
); );
case LIST_STYLE_TYPE.KOREAN_HANGUL_FORMAL: case LIST_STYLE_TYPE.KOREAN_HANGUL_FORMAL:
@ -601,30 +624,44 @@ const createCounterText = (value: number, type: ListStyleType): string => {
'영일이삼사오육칠팔구', '영일이삼사오육칠팔구',
'십백천만', '십백천만',
KOREAN_NEGATIVE, KOREAN_NEGATIVE,
', ', koreanSuffix,
CJK_ZEROS | CJK_TEN_COEFFICIENTS | CJK_TEN_HIGH_COEFFICIENTS CJK_ZEROS | CJK_TEN_COEFFICIENTS | CJK_TEN_HIGH_COEFFICIENTS
); );
case LIST_STYLE_TYPE.KOREAN_HANJA_INFORMAL: case LIST_STYLE_TYPE.KOREAN_HANJA_INFORMAL:
return createCJKCounter(value, '零一二三四五六七八九', '十百千萬', KOREAN_NEGATIVE, ', ', 0); return createCJKCounter(value, '零一二三四五六七八九', '十百千萬', KOREAN_NEGATIVE, koreanSuffix, 0);
case LIST_STYLE_TYPE.KOREAN_HANJA_FORMAL: case LIST_STYLE_TYPE.KOREAN_HANJA_FORMAL:
return createCJKCounter( return createCJKCounter(
value, value,
'零壹貳參四五六七八九', '零壹貳參四五六七八九',
'拾百千', '拾百千',
KOREAN_NEGATIVE, KOREAN_NEGATIVE,
', ', koreanSuffix,
CJK_ZEROS | CJK_TEN_COEFFICIENTS | CJK_TEN_HIGH_COEFFICIENTS CJK_ZEROS | CJK_TEN_COEFFICIENTS | CJK_TEN_HIGH_COEFFICIENTS
); );
case LIST_STYLE_TYPE.DEVANAGARI: case LIST_STYLE_TYPE.DEVANAGARI:
return createCounterStyleFromRange(value, 0x966, 0x96f, true); return createCounterStyleFromRange(value, 0x966, 0x96f, true, defaultSuffix);
case LIST_STYLE_TYPE.GEORGIAN: case LIST_STYLE_TYPE.GEORGIAN:
return createAdditiveCounter(value, 1, 19999, GEORGIAN, LIST_STYLE_TYPE.DECIMAL); return createAdditiveCounter(
value,
1,
19999,
GEORGIAN,
LIST_STYLE_TYPE.DECIMAL,
defaultSuffix
);
case LIST_STYLE_TYPE.GUJARATI: case LIST_STYLE_TYPE.GUJARATI:
return createCounterStyleFromRange(value, 0xae6, 0xaef, true); return createCounterStyleFromRange(value, 0xae6, 0xaef, true, defaultSuffix);
case LIST_STYLE_TYPE.GURMUKHI: case LIST_STYLE_TYPE.GURMUKHI:
return createCounterStyleFromRange(value, 0xa66, 0xa6f, true); return createCounterStyleFromRange(value, 0xa66, 0xa6f, true, defaultSuffix);
case LIST_STYLE_TYPE.HEBREW: case LIST_STYLE_TYPE.HEBREW:
return createAdditiveCounter(value, 1, 10999, HEBREW, LIST_STYLE_TYPE.DECIMAL); return createAdditiveCounter(
value,
1,
10999,
HEBREW,
LIST_STYLE_TYPE.DECIMAL,
defaultSuffix
);
case LIST_STYLE_TYPE.HIRAGANA: case LIST_STYLE_TYPE.HIRAGANA:
return createCounterStyleFromSymbols( return createCounterStyleFromSymbols(
value, value,
@ -636,39 +673,39 @@ const createCounterText = (value: number, type: ListStyleType): string => {
'いろはにほへとちりぬるをわかよたれそつねならむうゐのおくやまけふこえてあさきゆめみしゑひもせす' 'いろはにほへとちりぬるをわかよたれそつねならむうゐのおくやまけふこえてあさきゆめみしゑひもせす'
); );
case LIST_STYLE_TYPE.KANNADA: case LIST_STYLE_TYPE.KANNADA:
return createCounterStyleFromRange(value, 0xce6, 0xcef, true); return createCounterStyleFromRange(value, 0xce6, 0xcef, true, defaultSuffix);
case LIST_STYLE_TYPE.KATAKANA: case LIST_STYLE_TYPE.KATAKANA:
return createCounterStyleFromSymbols( return createCounterStyleFromSymbols(
value, value,
'アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヰヱヲン', 'アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヰヱヲン',
'、' cjkSuffix
); );
case LIST_STYLE_TYPE.KATAKANA_IROHA: case LIST_STYLE_TYPE.KATAKANA_IROHA:
return createCounterStyleFromSymbols( return createCounterStyleFromSymbols(
value, value,
'イロハニホヘトチリヌルヲワカヨタレソツネナラムウヰノオクヤマケフコエテアサキユメミシヱヒモセス', 'イロハニホヘトチリヌルヲワカヨタレソツネナラムウヰノオクヤマケフコエテアサキユメミシヱヒモセス',
'、' cjkSuffix
); );
case LIST_STYLE_TYPE.LAO: case LIST_STYLE_TYPE.LAO:
return createCounterStyleFromRange(value, 0xed0, 0xed9, true); return createCounterStyleFromRange(value, 0xed0, 0xed9, true, defaultSuffix);
case LIST_STYLE_TYPE.MONGOLIAN: case LIST_STYLE_TYPE.MONGOLIAN:
return createCounterStyleFromRange(value, 0x1810, 0x1819, true); return createCounterStyleFromRange(value, 0x1810, 0x1819, true, defaultSuffix);
case LIST_STYLE_TYPE.MYANMAR: case LIST_STYLE_TYPE.MYANMAR:
return createCounterStyleFromRange(value, 0x1040, 0x1049, true); return createCounterStyleFromRange(value, 0x1040, 0x1049, true, defaultSuffix);
case LIST_STYLE_TYPE.ORIYA: case LIST_STYLE_TYPE.ORIYA:
return createCounterStyleFromRange(value, 0xb66, 0xb6f, true); return createCounterStyleFromRange(value, 0xb66, 0xb6f, true, defaultSuffix);
case LIST_STYLE_TYPE.PERSIAN: case LIST_STYLE_TYPE.PERSIAN:
return createCounterStyleFromRange(value, 0x6f0, 0x6f9, true); return createCounterStyleFromRange(value, 0x6f0, 0x6f9, true, defaultSuffix);
case LIST_STYLE_TYPE.TAMIL: case LIST_STYLE_TYPE.TAMIL:
return createCounterStyleFromRange(value, 0xbe6, 0xbef, true); return createCounterStyleFromRange(value, 0xbe6, 0xbef, true, defaultSuffix);
case LIST_STYLE_TYPE.TELUGU: case LIST_STYLE_TYPE.TELUGU:
return createCounterStyleFromRange(value, 0xc66, 0xc6f, true); return createCounterStyleFromRange(value, 0xc66, 0xc6f, true, defaultSuffix);
case LIST_STYLE_TYPE.THAI: case LIST_STYLE_TYPE.THAI:
return createCounterStyleFromRange(value, 0xe50, 0xe59, true); return createCounterStyleFromRange(value, 0xe50, 0xe59, true, defaultSuffix);
case LIST_STYLE_TYPE.TIBETAN: case LIST_STYLE_TYPE.TIBETAN:
return createCounterStyleFromRange(value, 0xf20, 0xf29, true); return createCounterStyleFromRange(value, 0xf20, 0xf29, true, defaultSuffix);
case LIST_STYLE_TYPE.DECIMAL: case LIST_STYLE_TYPE.DECIMAL:
default: default:
return createCounterStyleFromRange(value, 48, 57, true); return createCounterStyleFromRange(value, 48, 57, true, defaultSuffix);
} }
}; };

View File

@ -1,7 +1,8 @@
/* @flow */ /* @flow */
'use strict'; 'use strict';
import ListStyleTypeFormatter from 'liststyletype-formatter'; import {createCounterText} from './ListItem';
import {parseListStyleType} from './parsing/listStyle';
export const PSEUDO_CONTENT_ITEM_TYPE = { export const PSEUDO_CONTENT_ITEM_TYPE = {
TEXT: 0, TEXT: 0,
@ -84,6 +85,9 @@ export const resolvePseudoContent = (
} }
const tokens = parseContent(style.content); const tokens = parseContent(style.content);
console.log(style.content);
console.log(tokens);
const len = tokens.length; const len = tokens.length;
const contentItems: Array<PseudoContentItem> = []; const contentItems: Array<PseudoContentItem> = [];
let s = ''; let s = '';
@ -332,7 +336,7 @@ const formatCounterValue = (counter, glue: ?string, format: ?string): string =>
if (i > 0) { if (i > 0) {
result += glue || ''; result += glue || '';
} }
result += ListStyleTypeFormatter.format(counter[i], format || 'decimal', false); result += createCounterText(counter[i], parseListStyleType(format || 'decimal'), false);
} }
return result; return result;

View File

@ -75,7 +75,7 @@ export const LIST_STYLE_TYPE = {
export type ListStylePosition = $Values<typeof LIST_STYLE_POSITION>; export type ListStylePosition = $Values<typeof LIST_STYLE_POSITION>;
export type ListStyleType = $Values<typeof LIST_STYLE_TYPE>; export type ListStyleType = $Values<typeof LIST_STYLE_TYPE>;
const parseListStyleType = (type: string): ListStyleType => { export const parseListStyleType = (type: string): ListStyleType => {
switch (type) { switch (type) {
case 'disc': case 'disc':
return LIST_STYLE_TYPE.DISC; return LIST_STYLE_TYPE.DISC;