Skip to content

Commit

Permalink
Improve how jiya coöperates with jinzified Biaodian
Browse files Browse the repository at this point in the history
  • Loading branch information
ethantw committed Feb 25, 2016
1 parent 89a1104 commit f7e536d
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 105 deletions.
30 changes: 15 additions & 15 deletions src/js/inline/hanging.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,24 @@ $.extend( Han, {
var beenWrapped = matches( $elmt, 'h-char[unicode], h-char[unicode] *' )

void function( $elmt ) {
if (matches( $elmt, 'h-jinze, h-jinze *' )) {
var $jinze = $elmt
var $cs
if (!matches( $elmt, 'h-jinze, h-jinze *' )) return

while (!matches( $jinze, 'h-jinze' )) {
$jinze = $jinze.parentNode
}
var $jinze = $elmt
var $cs

while (!matches( $jinze, 'h-jinze' )) {
$jinze = $jinze.parentNode
}

$cs = $jinze.nextSibling
$cs = $jinze.nextSibling

if ( $cs && matches( $cs, 'h-cs.jinze-outer' )) {
$cs.classList.add( 'hangable-outer' )
} else {
$jinze.insertAdjacentHTML(
'afterend',
'<h-cs hidden class="jinze-outer hangable-outer"> </h-cs>'
)
}
if ( $cs && matches( $cs, 'h-cs.jinze-outer' )) {
$cs.classList.add( 'hangable-outer' )
} else {
$jinze.insertAdjacentHTML(
'afterend',
'<h-cs hidden class="jinze-outer hangable-outer"> </h-cs>'
)
}
}( $elmt )

Expand Down
180 changes: 98 additions & 82 deletions src/js/inline/jiya.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,11 @@ define([
var JIYA_CLASS = 'bd-jiya'
var JIYA_AVOID = 'h-char.bd-jiya'
var CONSECUTIVE_CLASS = 'bd-consecutive'
var CS_HTML = '<h-cs hidden class="jinze-outer jiya-outer"> </h-cs>'

var matches = Han.find.matches

var get$csoHTML = function( prev, clazz ) {
return '<h-cs hidden prev="' + prev + '" class="jinze-outer ' + clazz + '"> </h-cs>'
}

function get$bdType( $char ) {
var hasClass = function( className ) {
return $char.classList.contains( className )
}

function get$bdType( $bd ) {
return (
hasClass( 'bd-open' )
? 'bd-open'
Expand All @@ -31,41 +24,122 @@ function get$bdType( $char ) {
? 'bd-liga'
: hasClass( 'bd-end' )
? (
/(?:3001|3002|ff0c)/i.test($char.getAttribute( 'unicode' ))
/(?:3001|3002|ff0c)/i.test($bd.getAttribute( 'unicode' ))
// `cop` stands for ‘comma or period’.
? 'bd-end bd-cop'
: 'bd-end'
)
: ''
)

function hasClass( className ) {
return $bd.classList.contains( className )
}
}

function charifyBiaodian( portion ) {
var biaodian = portion.text
var $elmt = portion.node.parentNode
var $new = Han.createBdChar( biaodian )
var beenWrapped = matches( $elmt, 'h-char.biaodian, h-char.biaodian *' )

$new.innerHTML = '<h-inner>' + biaodian + '</h-inner>'
$new.classList.add( JIYA_CLASS )

if (matches( $elmt, 'h-jinze *' )) {
insertAdjacentCS( $elmt )
}

return !beenWrapped
? $new
: (function() {
var $bd = $elmt

while (!matches( $bd, 'h-char.biaodian' )) {
$bd = $bd.parentNode
}

$bd.classList.add( JIYA_CLASS )

return matches( $elmt, 'h-inner, h-inner *' )
? biaodian
: $new.firstChild
})()
}

var prevBiaodianType
var prevBdType

function locateConsecutiveBd( portion ) {
var $elmt = portion.node.parentNode
var clazz
var prev = prevBdType
var $elmt, $bd, $jinze, classList

$elmt = $bd = portion.node.parentNode

while (!matches( $elmt, 'h-char.biaodian' )) {
$elmt = $elmt.parentNode
while (!matches( $bd, 'h-char.biaodian' )) {
$bd = $bd.parentNode
}

clazz = $elmt.classList
classList = $bd.classList

if ( prevBiaodianType ) {
$elmt.setAttribute( 'prev', prevBiaodianType )
if ( prev ) {
$bd.setAttribute( 'prev', prevBdType )
}

if ( portion.isEnd ) {
prevBiaodianType = undefined
clazz.add( CONSECUTIVE_CLASS, 'end-portion' )
prevBdType = undefined
classList.add( CONSECUTIVE_CLASS, 'end-portion' )
} else {
prevBiaodianType = get$bdType( $elmt )
clazz.add( CONSECUTIVE_CLASS )
prevBdType = get$bdType( $bd )
classList.add( CONSECUTIVE_CLASS )
}

if (matches( $bd, 'h-jinze *' )) {
do {
$jinze = ( $jinze || $bd ).parentNode
} while (!matches( $jinze, 'h-jinze' ))

locateCS( $jinze, {
prev: prev,
'class': $bd.className
})
}
return portion.text
}

function insertAdjacentCS( $node ) {
var $jinze, $cs

do {
$jinze = ( $jinze || $node ).parentNode
} while (!matches( $jinze, 'h-jinze' ))

if (matches( $node, 'h-jinze > .bd-open:first-child' )) {
$jinze.insertAdjacentHTML( 'beforebegin', CS_HTML )
}
if (matches( $node, 'h-jinze > .bd-end:last-child' )) {
$jinze.insertAdjacentHTML( 'afterend', CS_HTML )
}
}

function locateCS( $jinze, attr ) {
var $cs

if (matches( $jinze, '.tou, .touwei' )) {
$cs = $jinze.previousSibling

if (matches( $cs, 'h-cs' )) {
$cs.setAttribute( 'prev', attr.prev )
}
}
if (matches( $jinze, '.wei, .touwei' )) {
$cs = $jinze.nextSibling

if (matches( $cs, 'h-cs' )) {
$cs.className += attr[ 'class' ]
}
}
}

Han.renderJiya = function( context ) {
var context = context || document
var finder = Han.find( context )
Expand All @@ -76,73 +150,15 @@ Han.renderJiya = function( context ) {
.avoid( JIYA_AVOID )
.charify({
avoid: false,

biaodian: function( portion ) {
var $elmt = portion.node.parentNode
var beenWrapped = matches( $elmt, 'h-char.biaodian, h-char.biaodian *' )

var biaodian = portion.text
var $new = Han.createBdChar( biaodian )

$new.innerHTML = '<h-inner>' + biaodian + '</h-inner>'
$new.classList.add( JIYA_CLASS )

return !beenWrapped
? $new
: (function() {
var $char = $elmt

while (!matches( $char, 'h-char.biaodian' )) {
$char = $char.parentNode
}
$char.classList.add( JIYA_CLASS )

return matches( $elmt, 'h-inner, h-inner *' )
? biaodian
: $new.firstChild
})()
}
biaodian: charifyBiaodian
})
// End avoiding `JIYA_AVOID`:
.endAvoid()

.avoid( 'textarea, code, kbd, samp, pre, h-cs' )
.replace( TYPESET.group.biaodian[0], locateConsecutiveBd )
.replace( TYPESET.group.biaodian[1], locateConsecutiveBd )

$.qsa( 'h-jinze', context )
.forEach(function( $jinze ) {
var clazz = 'jiya-outer '
var $char, $cs, prev

if (matches( $jinze, '.tou, .touwei' )) {
$char = $.qs( '.biaodian:first-child', $jinze )
$cs = $jinze.previousSibling
prev = $char.getAttribute( 'prev' ) || ''

if ( $cs && matches( $cs, 'h-cs.jinze-outer' )) {
$cs.setAttribute( 'prev', prev )
$cs.setAttribute( 'class', 'jinze-outer jiya-outer' )
} else {
$jinze.insertAdjacentHTML(
'beforebegin', get$csoHTML( prev, clazz )
)
}
}
if (matches( $jinze, '.wei, .touwei' )) {
$char = $.qs( '.biaodian:last-child', $jinze )
$cs = $jinze.nextSibling
clazz += $char.getAttribute( 'class' )

if ( $cs && matches( $cs, 'h-cs.jinze-outer' )) {
$cs.setAttribute( 'class', clazz + ' ' + $cs.getAttribute( 'class' ))
} else {
$jinze.insertAdjacentHTML(
'afterend', get$csoHTML( '', clazz )
)
}
}
})

return finder
}

Expand Down
Loading

0 comments on commit f7e536d

Please sign in to comment.