2020-02-21 01:49:22 +08:00
|
|
|
/**
|
|
|
|
* Heti add-on v 0.1.0
|
|
|
|
* Add right spacing between CJK & ANS characters
|
|
|
|
*/
|
|
|
|
|
2020-03-15 20:58:23 +08:00
|
|
|
import Finder from 'heti-findandreplacedomtext'
|
2020-02-21 01:49:22 +08:00
|
|
|
|
2020-03-15 23:19:37 +08:00
|
|
|
const hasOwn = {}.hasOwnProperty
|
2020-03-15 20:58:23 +08:00
|
|
|
const HETI_NON_CONTIGUOUS_ELEMENTS = Object.assign({}, Finder.NON_CONTIGUOUS_PROSE_ELEMENTS, {
|
2020-05-04 23:29:03 +08:00
|
|
|
ins: 1, del: 1, s: 1, a: 1,
|
2020-03-15 20:58:23 +08:00
|
|
|
})
|
|
|
|
const HETI_SKIPPED_ELEMENTS = Object.assign({}, Finder.NON_PROSE_ELEMENTS, {
|
2020-03-15 23:19:37 +08:00
|
|
|
pre: 1, code: 1, sup: 1, sub: 1, 'heti-spacing': 1, 'heti-close': 1,
|
2020-03-15 20:58:23 +08:00
|
|
|
})
|
2020-02-21 01:49:22 +08:00
|
|
|
const HETI_SKIPPED_CLASS = 'heti-skip'
|
2020-03-15 23:19:37 +08:00
|
|
|
|
|
|
|
// 部分正则表达式修改自 pangu.js https://github.com/vinta/pangu.js
|
|
|
|
const CJK = '\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30fa\u30fc-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff'
|
|
|
|
const A = 'A-Za-z\u0080-\u00ff\u0370-\u03ff'
|
|
|
|
const N = '0-9'
|
|
|
|
const S = '`~!@#\\$%\\^&\\*\\(\\)-_=\\+\\[\\]{}\\\\\\|;:\'",<.>\\/\\?'
|
|
|
|
const ANS = `${A}${N}${S}`
|
|
|
|
const REG_CJK_FULL = `(?<=[${CJK}])( *[${ANS}]+(?: +[${ANS}]+)* *)(?=[${CJK}])`
|
|
|
|
const REG_CJK_START = `([${ANS}]+(?: +[${ANS}]+)* *)(?=[${CJK}])`
|
|
|
|
const REG_CJK_END = `(?<=[${CJK}])( *[${ANS}]+(?: +[${ANS}]+)*)`
|
|
|
|
const REG_CJK_FULL_WITHOUT_LOOKBEHIND = `(?:[${CJK}])( *[${ANS}]+(?: +[${ANS}]+)* *)(?=[${CJK}])`
|
|
|
|
const REG_CJK_END_WITHOUT_LOOKBEHIND = `(?:[${CJK}])( *[${ANS}]+(?: +[${ANS}]+)*)`
|
|
|
|
const REG_BD_STOP = `。.,、:;!‼?⁇`
|
|
|
|
const REG_BD_SEP = `·・‧`
|
|
|
|
const REG_BD_OPEN = `「『(《〈【〖〔[{`
|
|
|
|
const REG_BD_CLOSE = `」』)》〉】〗〕]}`
|
|
|
|
const REG_BD_START = `${REG_BD_OPEN}${REG_BD_CLOSE}`
|
|
|
|
const REG_BD_END = `${REG_BD_STOP}${REG_BD_OPEN}${REG_BD_CLOSE}`
|
2021-12-09 02:38:51 +08:00
|
|
|
const REG_BD_HALF_OPEN = `“‘`
|
|
|
|
const REG_BD_HALF_CLOSE = `”’`
|
|
|
|
const REG_BD_HALF_START = `${REG_BD_HALF_OPEN}${REG_BD_HALF_CLOSE}`
|
2020-02-21 01:49:22 +08:00
|
|
|
|
|
|
|
class Heti {
|
2022-12-31 22:20:24 +08:00
|
|
|
constructor (rootDocument, rootSelector) {
|
2020-03-15 20:58:23 +08:00
|
|
|
let supportLookBehind = true
|
|
|
|
|
2020-03-04 23:08:13 +08:00
|
|
|
try {
|
|
|
|
new RegExp(`(?<=\d)\d`, 'g').test('')
|
|
|
|
} catch (err) {
|
2020-03-15 20:58:23 +08:00
|
|
|
console.info(err.name, '该浏览器尚未实现 RegExp positive lookbehind')
|
|
|
|
supportLookBehind = false
|
2020-03-04 23:08:13 +08:00
|
|
|
}
|
|
|
|
|
2022-12-31 22:20:24 +08:00
|
|
|
this.rootDocument = rootDocument
|
2020-02-21 01:49:22 +08:00
|
|
|
this.rootSelector = rootSelector || '.heti'
|
2020-03-15 23:19:37 +08:00
|
|
|
this.REG_FULL = new RegExp(supportLookBehind ? REG_CJK_FULL : REG_CJK_FULL_WITHOUT_LOOKBEHIND, 'g')
|
|
|
|
this.REG_START = new RegExp(REG_CJK_START, 'g')
|
|
|
|
this.REG_END = new RegExp(supportLookBehind ? REG_CJK_END : REG_CJK_END_WITHOUT_LOOKBEHIND, 'g')
|
2020-03-15 20:58:23 +08:00
|
|
|
this.offsetWidth = supportLookBehind ? 0 : 1
|
2020-02-21 01:49:22 +08:00
|
|
|
this.funcForceContext = function forceContext (el) {
|
|
|
|
return hasOwn.call(HETI_NON_CONTIGUOUS_ELEMENTS, el.nodeName.toLowerCase())
|
|
|
|
}
|
|
|
|
this.funcFilterElements = function filterElements (el) {
|
|
|
|
return (
|
|
|
|
!(el.classList && el.classList.contains(HETI_SKIPPED_CLASS)) &&
|
|
|
|
!hasOwn.call(HETI_SKIPPED_ELEMENTS, el.nodeName.toLowerCase())
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
spacingElements (elmList) {
|
|
|
|
for (let $$root of elmList) {
|
|
|
|
this.spacingElement($$root)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
spacingElement ($$elm) {
|
|
|
|
const commonConfig = {
|
|
|
|
forceContext: this.funcForceContext,
|
|
|
|
filterElements: this.funcFilterElements,
|
|
|
|
}
|
2022-12-31 22:20:24 +08:00
|
|
|
const getWrapper = (elementName, classList, text) => {
|
|
|
|
const $$r = this.rootDocument.createElement(elementName)
|
2020-02-21 01:49:22 +08:00
|
|
|
$$r.className = classList
|
|
|
|
$$r.textContent = text.trim()
|
|
|
|
return $$r
|
|
|
|
}
|
|
|
|
|
2020-03-15 20:58:23 +08:00
|
|
|
Finder($$elm, Object.assign({}, commonConfig, {
|
2020-02-21 01:49:22 +08:00
|
|
|
find: this.REG_FULL,
|
2020-03-15 23:19:37 +08:00
|
|
|
replace: portion => getWrapper('heti-spacing', 'heti-spacing-start heti-spacing-end', portion.text),
|
2020-03-15 20:58:23 +08:00
|
|
|
offset: this.offsetWidth,
|
2020-02-21 01:49:22 +08:00
|
|
|
}))
|
|
|
|
|
2020-03-15 20:58:23 +08:00
|
|
|
Finder($$elm, Object.assign({}, commonConfig, {
|
2020-02-21 01:49:22 +08:00
|
|
|
find: this.REG_START,
|
2020-03-15 23:19:37 +08:00
|
|
|
replace: portion => getWrapper('heti-spacing', 'heti-spacing-start', portion.text),
|
2020-02-21 01:49:22 +08:00
|
|
|
}))
|
|
|
|
|
2020-03-15 20:58:23 +08:00
|
|
|
Finder($$elm, Object.assign({}, commonConfig, {
|
2020-02-21 01:49:22 +08:00
|
|
|
find: this.REG_END,
|
2020-03-15 23:19:37 +08:00
|
|
|
replace: portion => getWrapper('heti-spacing', 'heti-spacing-end', portion.text),
|
|
|
|
offset: this.offsetWidth,
|
|
|
|
}))
|
|
|
|
|
|
|
|
Finder($$elm, Object.assign({}, commonConfig, {
|
|
|
|
find: new RegExp(`([${REG_BD_STOP}])(?=[${REG_BD_START}])|([${REG_BD_OPEN}])(?=[${REG_BD_OPEN}])|([${REG_BD_CLOSE}])(?=[${REG_BD_END}])`,'g'),
|
|
|
|
replace: portion => getWrapper('heti-adjacent', 'heti-adjacent-half', portion.text),
|
|
|
|
offset: this.offsetWidth,
|
|
|
|
}))
|
|
|
|
|
|
|
|
Finder($$elm, Object.assign({}, commonConfig, {
|
|
|
|
find: new RegExp(`([${REG_BD_SEP}])(?=[${REG_BD_OPEN}])|([${REG_BD_CLOSE}])(?=[${REG_BD_SEP}])`,'g'),
|
|
|
|
replace: portion => getWrapper('heti-adjacent', 'heti-adjacent-quarter', portion.text),
|
2020-03-15 20:58:23 +08:00
|
|
|
offset: this.offsetWidth,
|
2020-02-21 01:49:22 +08:00
|
|
|
}))
|
2021-12-09 02:38:51 +08:00
|
|
|
|
|
|
|
// 使用弯引号的情况下,在停顿符号接弯引号(如「。“」)或弯引号接全角开引号(如“《」)时,间距缩进调整到四分之一
|
|
|
|
Finder($$elm, Object.assign({}, commonConfig, {
|
|
|
|
find: new RegExp(`([${REG_BD_STOP}])(?=[${REG_BD_HALF_START}])|([${REG_BD_HALF_OPEN}])(?=[${REG_BD_OPEN}])`,'g'),
|
|
|
|
replace: portion => getWrapper('heti-adjacent', 'heti-adjacent-quarter', portion.text),
|
|
|
|
offset: this.offsetWidth,
|
|
|
|
}))
|
2020-02-21 01:49:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
autoSpacing () {
|
2022-06-15 09:37:29 +08:00
|
|
|
const callback = () => {
|
2022-12-31 22:20:24 +08:00
|
|
|
const $$rootList = this.rootDocument.querySelectorAll(this.rootSelector)
|
2020-02-21 01:49:22 +08:00
|
|
|
|
|
|
|
for (let $$root of $$rootList) {
|
|
|
|
this.spacingElement($$root)
|
|
|
|
}
|
2022-06-15 09:37:29 +08:00
|
|
|
}
|
2022-12-31 22:20:24 +08:00
|
|
|
if (this.rootDocument == document) {
|
|
|
|
if (this.rootDocument.readyState === 'complete') setTimeout(callback)
|
|
|
|
else this.rootDocument.addEventListener('DOMContentLoaded', callback)
|
|
|
|
} else {
|
|
|
|
callback()
|
|
|
|
}
|
2020-02-21 01:49:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Heti
|