Comparing version 0.1.5 to 0.4.0
110
mojik.js
/*! | ||
* Mojik v0.1.5 https://github.com/terkel/mojik | ||
* Mojik v0.4.0 https://github.com/terkel/mojik | ||
* @author Takeru Suzuki http://terkel.jp/ | ||
@@ -17,3 +17,13 @@ * @license MIT http://opensource.org/licenses/MIT | ||
var Mojik = Mojik || {}; | ||
var cutsTheMustard = | ||
"classList" in document.documentElement && | ||
"requestAnimationFrame" in window; | ||
var isOldAndroid = /Android [1-5]\./.test(navigator.userAgent); | ||
var defaults = { | ||
doubleDash: false, | ||
supportOldAndroid: false | ||
}; | ||
throttle("resize", "optimizedResize"); | ||
Mojik.htmlClassPrefix = "mjk-"; | ||
@@ -28,2 +38,5 @@ | ||
leadOpeningBracketBefore_atLineEnd: "leadOpeningBracketBefore-atLineEnd", | ||
dash: "dash", | ||
dash_2em: "dash-2em", | ||
dash_3em: "dash-3em", | ||
western: "western", | ||
@@ -36,3 +49,4 @@ western_number: "western-number", | ||
western_atParagraphEnd: "western-atParagraphEnd", | ||
western_afterLineBreak: "western-afterLineBreak" | ||
western_afterLineBreak: "western-afterLineBreak", | ||
hidden: "hidden" | ||
}; | ||
@@ -69,2 +83,8 @@ | ||
// ダッシュ | ||
dashes: "\\u2014\\u2015", // EM DASH, HRIZONTAL BAR | ||
// リーダー | ||
leaders: "\\u2025\\u2026", // TWO DOT LEADER, HORIZONTAL ELLIPSIS | ||
// 欧文文字 | ||
@@ -83,3 +103,3 @@ western: | ||
// ... | ||
"\\u2000-\\u206F" + // General Punctuation | ||
"\\u2000-\\u203A\\u203C-\\u206F" + // General Punctuation excluding U+203B REFERENCE MARK | ||
"\\u2070-\\u209F" + // Superscripts and Subscripts | ||
@@ -95,17 +115,15 @@ "\\u20A0-\\u20CF" + // Currency Symbols | ||
// 欧文終わり括弧 | ||
westernClosingBrackets: "’”)]}>»›", | ||
westernClosingBrackets: "’”)]}>»›" | ||
}; | ||
// 欧文ダッシュ (FIGURE DASH, EN DASH, EM DASH, HORIZONTAL BAR, SWUNG DASH) | ||
westernDashes: "\\u2012\\u2013\\u2014\\u2015\\u2053", | ||
Mojik.ignoreTag = "pre|code|kbd|samp"; | ||
// 欧文ハイフン (HYPHEN-MINUS, HYPHEN, NON-BREAKING HYPHEN) | ||
westernHyphens: "\\u002D\\u2010\\u2011", | ||
Mojik.compose = function (selector, options) { | ||
// 欧文リード (HORIZONTAL ELLIPSIS, TWO DOT LEADER) | ||
westernEllipses: "\\u2025\\u2026" | ||
}; | ||
options = extend(defaults, options); | ||
Mojik.ignoreTag = "pre|code|kbd|samp"; | ||
if (!cutsTheMustard || (isOldAndroid && !options.supportOldAndroid)) { | ||
return; | ||
} | ||
Mojik.compose = function (selector/*, options*/) { | ||
var elements = document.querySelectorAll(selector); | ||
@@ -116,3 +134,3 @@ var reCommentStr = "<!--[\\s\\S]*?-->"; | ||
var reTagDivider = new RegExp(reCommentStr + "|" + reTagStr + "|[^<>]+", "gi"); | ||
var reIgnoreTag = new RegExp("^<(" + Mojik.ignoreTag + ")[ >]", "i"); | ||
var reIgnoreTag = new RegExp("^<(" + Mojik.ignoreTag + ")[\\s>]", "i"); | ||
var reWestern = new RegExp("[" + Mojik.characters.western + "]+", "g"); | ||
@@ -131,2 +149,5 @@ var reWesternAhead = new RegExp("[" + Mojik.characters.western + "]+$"); | ||
var reOpeningBracket = new RegExp("[" + Mojik.characters.japaneseOpeningBrackets + "]", "g"); | ||
var reStartWithDashOrLeader = new RegExp("^([" + Mojik.characters.dashes + Mojik.characters.leaders + "])"); | ||
var reEndWithDashOrLeader = new RegExp("([" + Mojik.characters.dashes + Mojik.characters.leaders + "])$"); | ||
var reConsecutiveDashes = new RegExp("([" + Mojik.characters.dashes + "])\\1+", "g"); | ||
var puncPairs = [ | ||
@@ -153,2 +174,5 @@ [Mojik.characters.japaneseClosingBrackets, | ||
Mojik.characters.japaneseFullWidthSpace | ||
], | ||
[Mojik.characters.japaneseFullWidthSpace, | ||
Mojik.characters.japaneseOpeningBrackets | ||
] | ||
@@ -158,2 +182,3 @@ ]; | ||
var obj = {}; | ||
var key; | ||
for (key in Mojik.htmlClass) { | ||
@@ -169,3 +194,3 @@ obj[key] = Mojik.htmlClassPrefix + Mojik.htmlClass[key]; | ||
// 要素内の文字列をテキストとタグのスライス(文字列片)に分割 | ||
var slices = el.innerHTML.match(reTagDivider); | ||
var slices = el.innerHTML.match(reTagDivider) || []; | ||
@@ -300,2 +325,17 @@ var textSlices = []; | ||
// ダッシュまたはリーダーで始まる | ||
if (reStartWithDashOrLeader.test(match)) { | ||
hasNoSpaceBefore = true; | ||
} | ||
// ダッシュまたはリーダーで終わる | ||
if (reEndWithDashOrLeader.test(match)) { | ||
hasNoSpaceAfter = true; | ||
} | ||
// 2倍ダッシュ・3倍ダッシュ | ||
if (options.doubleDash) { | ||
match = match.replace(reConsecutiveDashes, wrapDashes); | ||
} | ||
return leadingWhitespace + "<span class=\"" + htmlClass.western + | ||
@@ -386,4 +426,4 @@ (isNumber? " " + htmlClass.western_number: "") + | ||
parseLines(); | ||
throttle("resize", "optimizedResize"); | ||
window.addEventListener("optimizedResize", parseLines); | ||
window.matchMedia("print").addListener(parseLines); | ||
@@ -467,2 +507,26 @@ function parseLines () { | ||
}); | ||
function wrapDashes (dashes) { | ||
var dashesLength = dashes.length; | ||
var dash = dashes.slice(0, 1); | ||
var result = ""; | ||
var odd = dashesLength % 2 > 0; | ||
var len = odd? (dashesLength - 3) / 2: dashesLength / 2; | ||
var l; | ||
if (dashes.length > 1) { | ||
for (l = 0; l < len; l++) { | ||
result += "<span class=\"" + htmlClass.dash + " " + htmlClass.dash_2em + "\">" + dash + | ||
"<span class=\"" + htmlClass.hidden + "\">" + dash + "</span>" + | ||
"</span>"; | ||
} | ||
if (odd) { | ||
result += "<span class=\"" + htmlClass.dash + " " + htmlClass.dash_3em + "\">" + dash + | ||
"<span class=\"" + htmlClass.hidden + "\">" + dash + dash + "</span>" + | ||
"</span>"; | ||
} | ||
} else { | ||
result = "<span class=\"" + htmlClass.dash + "\">" + dashes + "</span>"; | ||
} | ||
return result; | ||
} | ||
} | ||
@@ -485,3 +549,17 @@ | ||
function extend (target, source) { | ||
var key; | ||
var val; | ||
if (source) { | ||
for (key in source) { | ||
val = source[key]; | ||
if (typeof val !== "undefined") { | ||
target[key] = val; | ||
} | ||
} | ||
} | ||
return target; | ||
} | ||
return Mojik; | ||
})); |
{ | ||
"name": "mojik", | ||
"version": "0.1.5", | ||
"version": "0.4.0", | ||
"description": "Take control of Japanese typesetting on the web", | ||
@@ -5,0 +5,0 @@ "main": "mojik.js", |
@@ -40,2 +40,6 @@ # Mojik | ||
文字組を適用したくない箇所は`.mjk-ignore`でマークアップします。 | ||
<p>ありがとう<span class="mjk-ignore">ヾ(๑╹◡╹)ノ</span></p> | ||
各アキ量はCSSで調整します。 | ||
@@ -51,2 +55,11 @@ Set the spacing amount using CSS. | ||
### オプション Options | ||
`Mojik.compose()`の2番目の引数でオプションを指定できます。 | ||
Mojik.compose("main h1", { supportOldAndroid: true }); | ||
- `doubleDash`: ダッシュ(U+2014 EM DASHまたはU+2015 HORIZONTAL BAR)が2つ以上連続したとき、それらがつながっているように見せる(実際には1つのダッシュをCSSで横方向に拡大している)。デフォルトは`false` | ||
- `supportOldAndroid`: Noto Sans CJKの入っていない、バージョン5までのAndroidをサポートするかどうか。デフォルトは`false`(参考:[#25](https://github.com/terkel/mojik/issues/25)) | ||
## ライセンス License | ||
@@ -53,0 +66,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
35200
579
67