@@ -415,7 +415,7 @@ function module_code(library_namespace) {
415415 * TODO: 可中途跳出。
416416 *
417417 * @param {String }[type]
418- * 欲搜尋之類型 。 e.g., 'template'. see
418+ * filter 或欲搜尋之類型 。 e.g., 'template'. see
419419 * ((wiki_API.parse.wiki_element_toString)).<br />
420420 * 未指定: 處理所有節點。
421421 * @param {Function }processor
@@ -479,14 +479,38 @@ function module_code(library_namespace) {
479479 }
480480
481481 var token_name ;
482+ function filter_token ( token ) {
483+ if ( type && type !== ( Array . isArray ( token ) ? token . type
484+ // 'plain': 對所有 plain text 或尚未 parse 的 wikitext.,皆執行指定作業。
485+ : 'plain' ) ) {
486+ return false ;
487+ }
488+
489+ if ( ! token_name )
490+ return true ;
491+
492+ switch ( token . type ) {
493+ case 'transclusion' :
494+ return session ? session . is_template ( token_name , token )
495+ : token . name === token_name ;
496+
497+ case 'tag_single' :
498+ case 'tag' :
499+ return token . tag === token_name ;
500+ }
501+ }
502+
482503 if ( type || type === '' ) {
483504 if ( typeof type !== 'string' ) {
484505 library_namespace . warn ( 'for_each_subelement: Invalid type ['
485506 + type + ']' ) ;
486507 return ;
487508 }
488509
489- token_name = type . match ( / ^ ( T e m p l a t e ) : ( .+ ) $ / i) ;
510+ type = type . trim ( ) ;
511+ // 配合 function filter_token(token)
512+ token_name = type . match ( / ^ ( T e m p l a t e ) : ( [ ^ { } | : ] + ) $ / i)
513+ || type . match ( / ^ ( { { ) ( [ ^ { } | : ] + ) } } $ / i) ;
490514 if ( token_name ) {
491515 if ( session ) {
492516 token_name = session . redirect_target_of ( type ) ;
@@ -502,6 +526,21 @@ function module_code(library_namespace) {
502526 + '未設定 session 卻篩選模板:' + token_name + ',會漏掉採用別名的模板!' ) ;
503527 console . trace ( type ) ;
504528 }
529+
530+ } else if ( token_name = type . match ( / ^ < ( \w + ) [ ^ < > ] * > / i) ) {
531+ token_name [ 2 ] = wiki_API . parse ( type , options , [ ] ) ;
532+ if ( ! ( token_name [ 2 ] && ( token_name [ 2 ] . type === 'tag' || token_name [ 2 ] . type === 'tag_single' ) ) ) {
533+ // e.g., .each('<p>', ...)
534+ token_name [ 2 ] = wiki_API . parse ( type + '</' + token_name [ 1 ]
535+ + '>' , options , [ ] ) ;
536+ }
537+ if ( token_name [ 2 ]
538+ && ( token_name [ 2 ] . type === 'tag' || token_name [ 2 ] . type === 'tag_single' ) ) {
539+ type = token_name [ 2 ] . type ;
540+ token_name = token_name [ 2 ] . tag ;
541+ } else {
542+ token_name = null ;
543+ }
505544 }
506545
507546 // normalize type
@@ -592,12 +631,7 @@ function module_code(library_namespace) {
592631 console . trace ( [ type , token ] ) ;
593632 }
594633
595- if ( ( ! type
596- // 'plain': 對所有 plain text 或尚未 parse 的 wikitext.,皆執行特定作業。
597- || type === ( Array . isArray ( token ) ? token . type : 'plain' ) )
598- && ( ! token_name || ( session ? token . type === 'transclusion'
599- && session . is_template ( token_name , token )
600- : token . name === token_name ) ) ) {
634+ if ( filter_token ( token ) ) {
601635 // options.set_index
602636 if ( options . add_index && token && typeof token === 'object' ) {
603637 // 假如需要自動設定 .parent, .index 則必須特別指定。
0 commit comments