fix: support browsers dont support regexp positive lookbehind
This commit is contained in:
parent
47d85d752f
commit
5a7182fcca
5 changed files with 669 additions and 677 deletions
1273
_site/heti-addon.js
1273
_site/heti-addon.js
File diff suppressed because it is too large
Load diff
|
@ -3,7 +3,7 @@
|
|||
* Add right spacing between CJK & ANS characters
|
||||
*/
|
||||
|
||||
import Finder from 'findandreplacedomtext'
|
||||
import Finder from 'heti-findandreplacedomtext'
|
||||
|
||||
// 正则表达式来自 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'
|
||||
|
@ -11,52 +11,42 @@ const A = 'A-Za-z\u0370-\u03ff'
|
|||
const N = '0-9'
|
||||
const S = '`~!@#\\$%\\^&\\*\\(\\)-_=\\+\\[\\]{}\\\\\\|;:\'",<.>\\/\\?'
|
||||
const ANS = `${A}${N}${S}`
|
||||
const HETI_NON_CONTIGUOUS_ELEMENTS = {
|
||||
// Block Elements
|
||||
address: 1, article: 1, aside: 1, blockquote: 1, dd: 1, div: 1,
|
||||
dl: 1, fieldset: 1, figcaption: 1, figure: 1, footer: 1, form: 1, h1: 1, h2: 1, h3: 1,
|
||||
h4: 1, h5: 1, h6: 1, header: 1, hgroup: 1, hr: 1, main: 1, nav: 1, noscript: 1, ol: 1,
|
||||
output: 1, p: 1, pre: 1, section: 1, ul: 1,
|
||||
// Other misc. elements that are not part of continuous inline prose:
|
||||
br: 1, li: 1, summary: 1, dt: 1, details: 1, rp: 1, rt: 1, rtc: 1,
|
||||
// Media / Source elements:
|
||||
script: 1, style: 1, img: 1, video: 1, audio: 1, canvas: 1, svg: 1, map: 1, object: 1,
|
||||
// Input elements
|
||||
input: 1, textarea: 1, select: 1, option: 1, optgroup: 1, button: 1,
|
||||
// Table related elements:
|
||||
table: 1, tbody: 1, thead: 1, th: 1, tr: 1, td: 1, caption: 1, col: 1, tfoot: 1, colgroup: 1,
|
||||
const HETI_NON_CONTIGUOUS_ELEMENTS = Object.assign({}, Finder.NON_CONTIGUOUS_PROSE_ELEMENTS, {
|
||||
// Inline elements
|
||||
ins: 1, del: 1, s: 1,
|
||||
}
|
||||
const HETI_SKIPPED_ELEMENTS = {
|
||||
br: 1, hr: 1,
|
||||
// Media / Source elements:
|
||||
script: 1, style: 1, img: 1, video: 1, audio: 1, canvas: 1, svg: 1, map: 1, object: 1,
|
||||
// Input elements:
|
||||
input: 1, textarea: 1, select: 1, option: 1, optgroup: 1, button: 1,
|
||||
// Pre elements:
|
||||
})
|
||||
const HETI_SKIPPED_ELEMENTS = Object.assign({}, Finder.NON_PROSE_ELEMENTS, {
|
||||
pre: 1, code: 1, sup: 1, sub: 1,
|
||||
// Heti elements
|
||||
'heti-spacing': 1,
|
||||
}
|
||||
})
|
||||
const HETI_SKIPPED_CLASS = 'heti-skip'
|
||||
const hasOwn = {}.hasOwnProperty
|
||||
const REG_FULL = `(?<=[${CJK}])( *[${ANS}]+(?: +[${ANS}]+)* *)(?=[${CJK}])`
|
||||
const REG_FULL_FIX = `(?:[${CJK}])( *[${ANS}]+(?: +[${ANS}]+)* *)(?=[${CJK}])`
|
||||
const REG_START = `([${ANS}]+(?: +[${ANS}]+)* *)(?=[${CJK}])`
|
||||
const REG_END = `(?<=[${CJK}])( *[${ANS}]+(?: +[${ANS}]+)*)`
|
||||
const REG_END_FIX = `(?:[${CJK}])( *[${ANS}]+(?: +[${ANS}]+)*)`
|
||||
|
||||
class Heti {
|
||||
constructor (rootSelector) {
|
||||
let supportLookBehind = true
|
||||
|
||||
try {
|
||||
new RegExp(`(?<=\d)\d`, 'g').test('')
|
||||
} catch (err) {
|
||||
console.warn(err.name, '浏览器尚未实现 RegExp positive lookbehind')
|
||||
return
|
||||
console.info(err.name, '该浏览器尚未实现 RegExp positive lookbehind')
|
||||
supportLookBehind = false
|
||||
}
|
||||
|
||||
this.rootSelector = rootSelector || '.heti'
|
||||
this.REG_FULL = new RegExp(`(?<=[${CJK}])( *[${ANS}]+(?: +[${ANS}]+)* *)(?=[${CJK}])`, 'g')
|
||||
this.REG_START = new RegExp(`([${ANS}]+(?: +[${ANS}]+)* *)(?=[${CJK}])`, 'g')
|
||||
this.REG_END = new RegExp(`(?<=[${CJK}])( *[${ANS}]+(?: +[${ANS}]+)*)`, 'g')
|
||||
this.REG_FULL = new RegExp(supportLookBehind ? REG_FULL : REG_FULL_FIX, 'g')
|
||||
this.REG_START = new RegExp(REG_START, 'g')
|
||||
this.REG_END = new RegExp(supportLookBehind ? REG_END : REG_END_FIX, 'g')
|
||||
this.offsetWidth = supportLookBehind ? 0 : 1
|
||||
this.funcForceContext = function forceContext (el) {
|
||||
return hasOwn.call(HETI_NON_CONTIGUOUS_ELEMENTS, el.nodeName.toLowerCase())
|
||||
// return true
|
||||
}
|
||||
this.funcFilterElements = function filterElements (el) {
|
||||
return (
|
||||
|
@ -84,19 +74,21 @@ class Heti {
|
|||
return $$r
|
||||
}
|
||||
|
||||
Finder($$elm, Object.assign(commonConfig, {
|
||||
Finder($$elm, Object.assign({}, commonConfig, {
|
||||
find: this.REG_FULL,
|
||||
replace: portion => getWrapper('heti-spacing-start heti-spacing-end', portion.text),
|
||||
offset: this.offsetWidth,
|
||||
}))
|
||||
|
||||
Finder($$elm, Object.assign(commonConfig, {
|
||||
Finder($$elm, Object.assign({}, commonConfig, {
|
||||
find: this.REG_START,
|
||||
replace: portion => getWrapper('heti-spacing-start', portion.text),
|
||||
}))
|
||||
|
||||
Finder($$elm, Object.assign(commonConfig, {
|
||||
Finder($$elm, Object.assign({}, commonConfig, {
|
||||
find: this.REG_END,
|
||||
replace: portion => getWrapper('heti-spacing-end', portion.text),
|
||||
offset: this.offsetWidth,
|
||||
}))
|
||||
}
|
||||
|
||||
|
|
12
package-lock.json
generated
12
package-lock.json
generated
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "heti",
|
||||
"version": "0.4.1",
|
||||
"version": "0.5.0",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
|
@ -1124,11 +1124,6 @@
|
|||
"path-exists": "^4.0.0"
|
||||
}
|
||||
},
|
||||
"findandreplacedomtext": {
|
||||
"version": "0.4.6",
|
||||
"resolved": "https://registry.npmjs.org/findandreplacedomtext/-/findandreplacedomtext-0.4.6.tgz",
|
||||
"integrity": "sha512-CVRIKbEwfWoKTSnLrmyX26WjMY7o0aUUTm0RXN47fF/eHINJa8C+YHZxGagC7gMWVaAEBFwH7uVXuhwZcylWOQ=="
|
||||
},
|
||||
"flat-cache": {
|
||||
"version": "2.0.1",
|
||||
"resolved": "https://registry.npmjs.org/flat-cache/-/flat-cache-2.0.1.tgz",
|
||||
|
@ -1424,6 +1419,11 @@
|
|||
"integrity": "sha1-4Ob+aijPUROIVeCG0Wkedx3iqLk=",
|
||||
"dev": true
|
||||
},
|
||||
"heti-findandreplacedomtext": {
|
||||
"version": "0.5.0",
|
||||
"resolved": "https://registry.npmjs.org/heti-findandreplacedomtext/-/heti-findandreplacedomtext-0.5.0.tgz",
|
||||
"integrity": "sha512-GFZjqU8LAdu1uR72GqrReI+lzNLMlcWtvdz1TKNJiofyo1mfTecFYSZEoEbcLcRMl+KwEldnNQoS4BwO8wtg0A=="
|
||||
},
|
||||
"hosted-git-info": {
|
||||
"version": "2.8.5",
|
||||
"resolved": "https://registry.npmjs.org/hosted-git-info/-/hosted-git-info-2.8.5.tgz",
|
||||
|
|
|
@ -34,7 +34,7 @@
|
|||
},
|
||||
"homepage": "https://github.com/sivan/heti#readme",
|
||||
"dependencies": {
|
||||
"findandreplacedomtext": "^0.4.6"
|
||||
"heti-findandreplacedomtext": "^0.5.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@rollup/plugin-commonjs": "^11.0.2",
|
||||
|
|
|
@ -16,6 +16,9 @@ export default {
|
|||
name: 'Heti',
|
||||
plugins: [
|
||||
terser({
|
||||
compress: {
|
||||
pure_funcs: ["console.info"] // 移除调试信息
|
||||
},
|
||||
output: {
|
||||
comments: false
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue