mirror of
https://github.com/niklasvh/html2canvas.git
synced 2023-08-10 21:13:10 +03:00
44 lines
1.1 KiB
JavaScript
44 lines
1.1 KiB
JavaScript
|
/* @flow */
|
||
|
'use strict';
|
||
|
|
||
|
import type NodeContainer from './NodeContainer';
|
||
|
import type {TextTransform} from './parsing/textTransform';
|
||
|
import type {TextBounds} from './TextBounds';
|
||
|
import {TEXT_TRANSFORM} from './parsing/textTransform';
|
||
|
import {parseTextBounds} from './TextBounds';
|
||
|
|
||
|
export default class TextContainer {
|
||
|
text: string;
|
||
|
parent: NodeContainer;
|
||
|
bounds: Array<TextBounds>;
|
||
|
|
||
|
constructor(node: Text, parent: NodeContainer) {
|
||
|
this.text = transform(node.data, parent.style.textTransform);
|
||
|
this.parent = parent;
|
||
|
this.bounds = parseTextBounds(this, node);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
const CAPITALIZE = /(^|\s|:|-|\(|\))([a-z])/g;
|
||
|
|
||
|
const transform = (text: string, transform: TextTransform) => {
|
||
|
switch (transform) {
|
||
|
case TEXT_TRANSFORM.LOWERCASE:
|
||
|
return text.toLowerCase();
|
||
|
case TEXT_TRANSFORM.CAPITALIZE:
|
||
|
return text.replace(CAPITALIZE, capitalize);
|
||
|
case TEXT_TRANSFORM.UPPERCASE:
|
||
|
return text.toUpperCase();
|
||
|
default:
|
||
|
return text;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
function capitalize(m, p1, p2) {
|
||
|
if (m.length > 0) {
|
||
|
return p1 + p2.toUpperCase();
|
||
|
}
|
||
|
|
||
|
return m;
|
||
|
}
|