-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1348 from niklasvh/line-breaking
Implement unicode line-breaking
- Loading branch information
Showing
12 changed files
with
260 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,27 @@ | ||
/* @flow */ | ||
'use strict'; | ||
|
||
export const fromCodePoint = (...codePoints: Array<number>): string => { | ||
if (String.fromCodePoint) { | ||
return String.fromCodePoint(...codePoints); | ||
} | ||
import NodeContainer from './NodeContainer'; | ||
import {LineBreaker, fromCodePoint, toCodePoints} from 'css-line-break'; | ||
import {OVERFLOW_WRAP} from './parsing/overflowWrap'; | ||
|
||
const length = codePoints.length; | ||
if (!length) { | ||
return ''; | ||
} | ||
export {toCodePoints, fromCodePoint} from 'css-line-break'; | ||
|
||
const codeUnits = []; | ||
export const breakWords = (str: string, parent: NodeContainer): Array<string> => { | ||
const breaker = LineBreaker(str, { | ||
lineBreak: parent.style.lineBreak, | ||
wordBreak: | ||
parent.style.overflowWrap === OVERFLOW_WRAP.BREAK_WORD | ||
? 'break-word' | ||
: parent.style.wordBreak | ||
}); | ||
|
||
let index = -1; | ||
let result = ''; | ||
while (++index < length) { | ||
let codePoint = codePoints[index]; | ||
if (codePoint <= 0xffff) { | ||
codeUnits.push(codePoint); | ||
} else { | ||
codePoint -= 0x10000; | ||
codeUnits.push((codePoint >> 10) + 0xd800, codePoint % 0x400 + 0xdc00); | ||
} | ||
if (index + 1 === length || codeUnits.length > 0x4000) { | ||
result += String.fromCharCode(...codeUnits); | ||
codeUnits.length = 0; | ||
} | ||
const words = []; | ||
let bk; | ||
|
||
while (!(bk = breaker.next()).done) { | ||
words.push(bk.value.slice()); | ||
} | ||
return result; | ||
|
||
return words; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* @flow */ | ||
'use strict'; | ||
|
||
export const LINE_BREAK = { | ||
NORMAL: 'normal', | ||
STRICT: 'strict' | ||
}; | ||
|
||
export type LineBreak = $Values<typeof LINE_BREAK>; | ||
|
||
export const parseLineBreak = (wordBreak: string): LineBreak => { | ||
switch (wordBreak) { | ||
case 'strict': | ||
return LINE_BREAK.STRICT; | ||
case 'normal': | ||
default: | ||
return LINE_BREAK.NORMAL; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* @flow */ | ||
'use strict'; | ||
|
||
export const OVERFLOW_WRAP = { | ||
NORMAL: 0, | ||
BREAK_WORD: 1 | ||
}; | ||
|
||
export type OverflowWrap = $Values<typeof OVERFLOW_WRAP>; | ||
|
||
export const parseOverflowWrap = (overflow: string): OverflowWrap => { | ||
switch (overflow) { | ||
case 'break-word': | ||
return OVERFLOW_WRAP.BREAK_WORD; | ||
case 'normal': | ||
default: | ||
return OVERFLOW_WRAP.NORMAL; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* @flow */ | ||
'use strict'; | ||
|
||
export const WORD_BREAK = { | ||
NORMAL: 'normal', | ||
BREAK_ALL: 'break-all', | ||
KEEP_ALL: 'keep-all' | ||
}; | ||
|
||
export type WordBreak = $Values<typeof WORD_BREAK>; | ||
|
||
export const parseWordBreak = (wordBreak: string): WordBreak => { | ||
switch (wordBreak) { | ||
case 'break-all': | ||
return WORD_BREAK.BREAK_ALL; | ||
case 'keep-all': | ||
return WORD_BREAK.KEEP_ALL; | ||
case 'normal': | ||
default: | ||
return WORD_BREAK.NORMAL; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>word-break</title> | ||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | ||
<script type="text/javascript" src="../../test.js"></script> | ||
<style> | ||
body { | ||
font-family: Arial; | ||
} | ||
.test span { | ||
line-break: normal; | ||
} | ||
|
||
.strict span { | ||
line-break: strict; | ||
} | ||
p.test{ | ||
border: 1px solid gray; | ||
color: blue; | ||
width: 6em; | ||
} | ||
</style> | ||
|
||
</head> | ||
<body> | ||
<!-- iteration marks --> | ||
<p class="test" lang="ja"> | ||
<span>サンプルぁルぁルぁルぁルぁルぁルぁぁぁぁ文ンプル–文々サンプル文</span> | ||
</p> | ||
|
||
<p class="test strict" lang="ja"> | ||
<span>サンプルぁルぁルぁルぁルぁルぁルぁぁぁぁ文文文文文‐–〜゠サンプル文々サンプル文</span> | ||
</p> | ||
|
||
|
||
<hr /> | ||
|
||
</body> | ||
</html> |
Oops, something went wrong.