Comparing version
/*! | ||
* pangu.js | ||
* -------- | ||
* @version: 3.1.0 | ||
* @version: 3.1.1 | ||
* @homepage: https://github.com/vinta/pangu.js | ||
@@ -93,2 +93,6 @@ * @license: MIT | ||
_this.blockTags = /^(div|h1|h2|h3|h4|h5|h6|p)$/i; | ||
// TODO | ||
// this.ignoreClasses | ||
// this.ignoreAttributes | ||
return _this; | ||
@@ -103,3 +107,3 @@ } | ||
while (parentNode && parentNode.nodeName && parentNode.nodeName.search(this.topTags) === -1) { | ||
if (parentNode.getAttribute('contenteditable') === 'true' || parentNode.getAttribute('g_editable') === 'true' || parentNode.nodeName.search(this.ignoreTags) >= 0) { | ||
if (parentNode.nodeName.search(this.ignoreTags) >= 0 || parentNode.isContentEditable || parentNode.getAttribute('g_editable') === 'true') { | ||
return true; | ||
@@ -262,2 +266,37 @@ } | ||
}, { | ||
key: 'spacingNode', | ||
value: function spacingNode(contextNode) { | ||
var xPathQuery = './/*/text()[normalize-space(.)]'; | ||
var hasSpacing = this.spacingNodeByXPath(xPathQuery, contextNode); | ||
return hasSpacing; | ||
} | ||
}, { | ||
key: 'spacingElementById', | ||
value: function spacingElementById(idName) { | ||
var xPathQuery = 'id("' + idName + '")//text()'; | ||
var hasSpacing = this.spacingNodeByXPath(xPathQuery); | ||
return hasSpacing; | ||
} | ||
}, { | ||
key: 'spacingElementByClassName', | ||
value: function spacingElementByClassName(className) { | ||
var xPathQuery = '//*[contains(concat(" ", normalize-space(@class), " "), "' + className + '")]//text()'; | ||
var hasSpacing = this.spacingNodeByXPath(xPathQuery); | ||
return hasSpacing; | ||
} | ||
}, { | ||
key: 'spacingElementByTagName', | ||
value: function spacingElementByTagName(tagName) { | ||
var xPathQuery = '//' + tagName + '//text()'; | ||
var hasSpacing = this.spacingNodeByXPath(xPathQuery); | ||
return hasSpacing; | ||
} | ||
}, { | ||
key: 'spacingPageTitle', | ||
@@ -314,3 +353,3 @@ value: function spacingPageTitle() { | ||
// TODO: 改用 promise | ||
// TODO: 支援 callback 和 promise | ||
@@ -325,29 +364,2 @@ }, { | ||
} | ||
}, { | ||
key: 'spacingElementById', | ||
value: function spacingElementById(idName) { | ||
var xPathQuery = 'id("' + idName + '")//text()'; | ||
var hasSpacing = this.spacingNodeByXPath(xPathQuery); | ||
return hasSpacing; | ||
} | ||
}, { | ||
key: 'spacingElementByClassName', | ||
value: function spacingElementByClassName(className) { | ||
var xPathQuery = '//*[contains(concat(" ", normalize-space(@class), " "), "' + className + '")]//text()'; | ||
var hasSpacing = this.spacingNodeByXPath(xPathQuery); | ||
return hasSpacing; | ||
} | ||
}, { | ||
key: 'spacingElementByTagName', | ||
value: function spacingElementByTagName(tagName) { | ||
var xPathQuery = '//' + tagName + '//text()'; | ||
var hasSpacing = this.spacingNodeByXPath(xPathQuery); | ||
return hasSpacing; | ||
} | ||
}]); | ||
@@ -360,3 +372,4 @@ | ||
module.exports = pangu; | ||
exports = module.exports = pangu; | ||
exports.Pangu = BrowserPangu; | ||
@@ -403,3 +416,4 @@ /***/ }, | ||
newText = newText.replace(/(["])([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])/g, '$1 $2'); | ||
newText = newText.replace(/(["'\(\[\{<\u201c]+)(\s*)(.+?)(\s*)(["'\)\]\}>\u201d]+)/g, '$1$3$5'); | ||
newText = newText.replace(/(["']+)(\s*)(.+?)(\s*)(["']+)/g, '$1$3$5'); | ||
newText = newText.replace(/([\(\[\{<\u201c]+)(\s*)(.+?)(\s*)([\)\]\}>\u201d]+)/g, '$1$3$5'); | ||
newText = newText.replace(/([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])( )(')([A-Za-z])/g, '$1$3$4'); | ||
@@ -406,0 +420,0 @@ |
/*! | ||
* pangu.js | ||
* -------- | ||
* @version: 3.1.0 | ||
* @version: 3.1.1 | ||
* @homepage: https://github.com/vinta/pangu.js | ||
@@ -9,3 +9,3 @@ * @license: MIT | ||
*/ | ||
!function(e,u){"object"==typeof exports&&"object"==typeof module?module.exports=u():"function"==typeof define&&define.amd?define("pangu",[],u):"object"==typeof exports?exports.pangu=u():e.pangu=u()}(this,function(){return function(e){function u(t){if(f[t])return f[t].exports;var a=f[t]={exports:{},id:t,loaded:!1};return e[t].call(a.exports,a,a.exports,u),a.loaded=!0,a.exports}var f={};return u.m=e,u.c=f,u.p="",u(0)}([function(e,u,f){"use strict";function t(e,u){if(!(e instanceof u))throw new TypeError("Cannot call a class as a function")}function a(e,u){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!u||"object"!=typeof u&&"function"!=typeof u?e:u}function n(e,u){if("function"!=typeof u&&null!==u)throw new TypeError("Super expression must either be null or a function, not "+typeof u);e.prototype=Object.create(u&&u.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),u&&(Object.setPrototypeOf?Object.setPrototypeOf(e,u):e.__proto__=u)}var r=function(){function e(e,u){for(var f=0;f<u.length;f++){var t=u[f];t.enumerable=t.enumerable||!1,t.configurable=!0,"value"in t&&(t.writable=!0),Object.defineProperty(e,t.key,t)}}return function(u,f,t){return f&&e(u.prototype,f),t&&e(u,t),u}}(),i=f(1).Pangu,o=8,s=function(e){function u(){t(this,u);var e=a(this,Object.getPrototypeOf(u).call(this));return e.topTags=/^(html|head|body|#document)$/i,e.ignoreTags=/^(code|pre|textarea)$/i,e.spaceSensitiveTags=/^(a|del|pre|s|strike|u)$/i,e.spaceLikeTags=/^(br|hr|i|img|pangu)$/i,e.blockTags=/^(div|h1|h2|h3|h4|h5|h6|p)$/i,e}return n(u,e),r(u,[{key:"canIgnoreNode",value:function(e){for(var u=e.parentNode;u&&u.nodeName&&-1===u.nodeName.search(this.topTags);){if("true"===u.getAttribute("contenteditable")||"true"===u.getAttribute("g_editable")||u.nodeName.search(this.ignoreTags)>=0)return!0;u=u.parentNode}return!1}},{key:"isFirstTextChild",value:function(e,u){for(var f=e.childNodes,t=0;t<f.length;t++){var a=f[t];if(a.nodeType!==o&&a.textContent)return a===u}}},{key:"isLastTextChild",value:function(e,u){for(var f=e.childNodes,t=f.length-1;t>-1;t--){var a=f[t];if(a.nodeType!==o&&a.textContent)return a===u}}},{key:"spacingNodeByXPath",value:function(e){for(var u=arguments.length<=1||void 0===arguments[1]?document:arguments[1],f=!1,t=document.evaluate(e,u,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null),a=void 0,n=void 0,r=t.snapshotLength-1;r>-1;--r)if(a=t.snapshotItem(r),this.canIgnoreNode(a))n=a;else{var i=this.spacing(a.data);if(a.data!==i&&(f=!0,a.data=i),n){if(a.nextSibling&&a.nextSibling.nodeName.search(this.spaceLikeTags)>=0){n=a;continue}var o=a.data.toString().substr(-1)+n.data.toString().substr(0,1),s=this.spacing(o);if(s!==o){f=!0;for(var c=n;c.parentNode&&-1===c.nodeName.search(this.spaceSensitiveTags)&&this.isFirstTextChild(c.parentNode,c);)c=c.parentNode;for(var d=a;d.parentNode&&-1===d.nodeName.search(this.spaceSensitiveTags)&&this.isLastTextChild(d.parentNode,d);)d=d.parentNode;if(d.nextSibling&&d.nextSibling.nodeName.search(this.spaceLikeTags)>=0){n=a;continue}if(-1===d.nodeName.search(this.blockTags))if(-1===c.nodeName.search(this.spaceSensitiveTags))-1===c.nodeName.search(this.ignoreTags)&&-1===c.nodeName.search(this.blockTags)&&(n.previousSibling?-1===n.previousSibling.nodeName.search(this.spaceLikeTags)&&(n.data=" "+n.data):this.canIgnoreNode(n)||(n.data=" "+n.data));else if(-1===d.nodeName.search(this.spaceSensitiveTags))a.data=a.data+" ";else{var l=document.createElement("pangu");l.innerHTML=" ",c.previousSibling?-1===c.previousSibling.nodeName.search(this.spaceLikeTags)&&c.parentNode.insertBefore(l,c):c.parentNode.insertBefore(l,c),l.previousElementSibling||l.parentNode&&l.parentNode.removeChild(l)}}}n=a}return f}},{key:"spacingPageTitle",value:function(){var e="/html/head/title/text()",u=this.spacingNodeByXPath(e);return u}},{key:"spacingPageBody",value:function(){for(var e="/html/body//*/text()[normalize-space(.)]",u=["script","style","textarea"],f=0;f<u.length;f++){var t=u[f];e+='[translate(name(..),"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")!="'+t+'"]'}var a=this.spacingNodeByXPath(e);return a}},{key:"spacingPage",value:function(){var e=this.spacingPageTitle(),u=this.spacingPageBody();return e||u}},{key:"spacingElementById",value:function(e){var u='id("'+e+'")//text()',f=this.spacingNodeByXPath(u);return f}},{key:"spacingElementByClassName",value:function(e){var u='//*[contains(concat(" ", normalize-space(@class), " "), "'+e+'")]//text()',f=this.spacingNodeByXPath(u);return f}},{key:"spacingElementByTagName",value:function(e){var u="//"+e+"//text()",f=this.spacingNodeByXPath(u);return f}}]),u}(i),c=new s;e.exports=c},function(e,u){"use strict";function f(e,u){if(!(e instanceof u))throw new TypeError("Cannot call a class as a function")}var t=function(){function e(e,u){for(var f=0;f<u.length;f++){var t=u[f];t.enumerable=t.enumerable||!1,t.configurable=!0,"value"in t&&(t.writable=!0),Object.defineProperty(e,t.key,t)}}return function(u,f,t){return f&&e(u.prototype,f),t&&e(u,t),u}}(),a=function(){function e(){f(this,e)}return t(e,[{key:"spacing",value:function(e){var u=e;u=u.replace(/([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])(["])/g,"$1 $2"),u=u.replace(/(["])([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])/g,"$1 $2"),u=u.replace(/(["'\(\[\{<\u201c]+)(\s*)(.+?)(\s*)(["'\)\]\}>\u201d]+)/g,"$1$3$5"),u=u.replace(/([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])( )(')([A-Za-z])/g,"$1$3$4"),u=u.replace(/([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])(#(\S+))/g,"$1 $2"),u=u.replace(/((\S+)#)([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])/g,"$1 $3"),u=u.replace(/([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])([\+\-\*\/=&\\|<>])([A-Za-z0-9])/g,"$1 $2 $3"),u=u.replace(/([A-Za-z0-9])([\+\-\*\/=&\\|<>])([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])/g,"$1 $2 $3");var f=u,t=u.replace(/([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])([\(\[\{<\u201c]+(.*?)[\)\]\}>\u201d]+)([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])/g,"$1 $2 $4");return u=t,f===t&&(u=u.replace(/([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])([\(\[\{<\u201c>])/g,"$1 $2"),u=u.replace(/([\)\]\}>\u201d<])([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])/g,"$1 $2")),u=u.replace(/([\(\[\{<\u201c]+)(\s*)(.+?)(\s*)([\)\]\}>\u201d]+)/g,"$1$3$5"),u=u.replace(/([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])([~!;:,\.\?\u2026])([A-Za-z0-9])/g,"$1$2 $3"),u=u.replace(/([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])([A-Za-z0-9`\$%\^&\*\-=\+\\\|\/@\u00a1-\u00ff\u2022\u2027\u2150-\u218f])/g,"$1 $2"),u=u.replace(/([A-Za-z0-9`~\$%\^&\*\-=\+\\\|\/!;:,\.\?\u00a1-\u00ff\u2022\u2026\u2027\u2150-\u218f])([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])/g,"$1 $2")}},{key:"spacingText",value:function(e){var u=arguments.length<=1||void 0===arguments[1]?function(){}:arguments[1];try{var f=this.spacing(e);u(null,f)}catch(t){u(t)}}}]),e}();e.exports.Pangu=a}])}); | ||
!function(e,u){"object"==typeof exports&&"object"==typeof module?module.exports=u():"function"==typeof define&&define.amd?define("pangu",[],u):"object"==typeof exports?exports.pangu=u():e.pangu=u()}(this,function(){return function(e){function u(t){if(f[t])return f[t].exports;var a=f[t]={exports:{},id:t,loaded:!1};return e[t].call(a.exports,a,a.exports,u),a.loaded=!0,a.exports}var f={};return u.m=e,u.c=f,u.p="",u(0)}([function(e,u,f){"use strict";function t(e,u){if(!(e instanceof u))throw new TypeError("Cannot call a class as a function")}function a(e,u){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!u||"object"!=typeof u&&"function"!=typeof u?e:u}function n(e,u){if("function"!=typeof u&&null!==u)throw new TypeError("Super expression must either be null or a function, not "+typeof u);e.prototype=Object.create(u&&u.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),u&&(Object.setPrototypeOf?Object.setPrototypeOf(e,u):e.__proto__=u)}var r=function(){function e(e,u){for(var f=0;f<u.length;f++){var t=u[f];t.enumerable=t.enumerable||!1,t.configurable=!0,"value"in t&&(t.writable=!0),Object.defineProperty(e,t.key,t)}}return function(u,f,t){return f&&e(u.prototype,f),t&&e(u,t),u}}(),i=f(1).Pangu,o=8,s=function(e){function u(){t(this,u);var e=a(this,Object.getPrototypeOf(u).call(this));return e.topTags=/^(html|head|body|#document)$/i,e.ignoreTags=/^(code|pre|textarea)$/i,e.spaceSensitiveTags=/^(a|del|pre|s|strike|u)$/i,e.spaceLikeTags=/^(br|hr|i|img|pangu)$/i,e.blockTags=/^(div|h1|h2|h3|h4|h5|h6|p)$/i,e}return n(u,e),r(u,[{key:"canIgnoreNode",value:function(e){for(var u=e.parentNode;u&&u.nodeName&&-1===u.nodeName.search(this.topTags);){if(u.nodeName.search(this.ignoreTags)>=0||u.isContentEditable||"true"===u.getAttribute("g_editable"))return!0;u=u.parentNode}return!1}},{key:"isFirstTextChild",value:function(e,u){for(var f=e.childNodes,t=0;t<f.length;t++){var a=f[t];if(a.nodeType!==o&&a.textContent)return a===u}}},{key:"isLastTextChild",value:function(e,u){for(var f=e.childNodes,t=f.length-1;t>-1;t--){var a=f[t];if(a.nodeType!==o&&a.textContent)return a===u}}},{key:"spacingNodeByXPath",value:function(e){for(var u=arguments.length<=1||void 0===arguments[1]?document:arguments[1],f=!1,t=document.evaluate(e,u,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null),a=void 0,n=void 0,r=t.snapshotLength-1;r>-1;--r)if(a=t.snapshotItem(r),this.canIgnoreNode(a))n=a;else{var i=this.spacing(a.data);if(a.data!==i&&(f=!0,a.data=i),n){if(a.nextSibling&&a.nextSibling.nodeName.search(this.spaceLikeTags)>=0){n=a;continue}var o=a.data.toString().substr(-1)+n.data.toString().substr(0,1),s=this.spacing(o);if(s!==o){f=!0;for(var c=n;c.parentNode&&-1===c.nodeName.search(this.spaceSensitiveTags)&&this.isFirstTextChild(c.parentNode,c);)c=c.parentNode;for(var d=a;d.parentNode&&-1===d.nodeName.search(this.spaceSensitiveTags)&&this.isLastTextChild(d.parentNode,d);)d=d.parentNode;if(d.nextSibling&&d.nextSibling.nodeName.search(this.spaceLikeTags)>=0){n=a;continue}if(-1===d.nodeName.search(this.blockTags))if(-1===c.nodeName.search(this.spaceSensitiveTags))-1===c.nodeName.search(this.ignoreTags)&&-1===c.nodeName.search(this.blockTags)&&(n.previousSibling?-1===n.previousSibling.nodeName.search(this.spaceLikeTags)&&(n.data=" "+n.data):this.canIgnoreNode(n)||(n.data=" "+n.data));else if(-1===d.nodeName.search(this.spaceSensitiveTags))a.data=a.data+" ";else{var l=document.createElement("pangu");l.innerHTML=" ",c.previousSibling?-1===c.previousSibling.nodeName.search(this.spaceLikeTags)&&c.parentNode.insertBefore(l,c):c.parentNode.insertBefore(l,c),l.previousElementSibling||l.parentNode&&l.parentNode.removeChild(l)}}}n=a}return f}},{key:"spacingNode",value:function(e){var u=".//*/text()[normalize-space(.)]",f=this.spacingNodeByXPath(u,e);return f}},{key:"spacingElementById",value:function(e){var u='id("'+e+'")//text()',f=this.spacingNodeByXPath(u);return f}},{key:"spacingElementByClassName",value:function(e){var u='//*[contains(concat(" ", normalize-space(@class), " "), "'+e+'")]//text()',f=this.spacingNodeByXPath(u);return f}},{key:"spacingElementByTagName",value:function(e){var u="//"+e+"//text()",f=this.spacingNodeByXPath(u);return f}},{key:"spacingPageTitle",value:function(){var e="/html/head/title/text()",u=this.spacingNodeByXPath(e);return u}},{key:"spacingPageBody",value:function(){for(var e="/html/body//*/text()[normalize-space(.)]",u=["script","style","textarea"],f=0;f<u.length;f++){var t=u[f];e+='[translate(name(..),"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")!="'+t+'"]'}var a=this.spacingNodeByXPath(e);return a}},{key:"spacingPage",value:function(){var e=this.spacingPageTitle(),u=this.spacingPageBody();return e||u}}]),u}(i),c=new s;u=e.exports=c,u.Pangu=s},function(e,u){"use strict";function f(e,u){if(!(e instanceof u))throw new TypeError("Cannot call a class as a function")}var t=function(){function e(e,u){for(var f=0;f<u.length;f++){var t=u[f];t.enumerable=t.enumerable||!1,t.configurable=!0,"value"in t&&(t.writable=!0),Object.defineProperty(e,t.key,t)}}return function(u,f,t){return f&&e(u.prototype,f),t&&e(u,t),u}}(),a=function(){function e(){f(this,e)}return t(e,[{key:"spacing",value:function(e){var u=e;u=u.replace(/([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])(["])/g,"$1 $2"),u=u.replace(/(["])([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])/g,"$1 $2"),u=u.replace(/(["']+)(\s*)(.+?)(\s*)(["']+)/g,"$1$3$5"),u=u.replace(/([\(\[\{<\u201c]+)(\s*)(.+?)(\s*)([\)\]\}>\u201d]+)/g,"$1$3$5"),u=u.replace(/([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])( )(')([A-Za-z])/g,"$1$3$4"),u=u.replace(/([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])(#(\S+))/g,"$1 $2"),u=u.replace(/((\S+)#)([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])/g,"$1 $3"),u=u.replace(/([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])([\+\-\*\/=&\\|<>])([A-Za-z0-9])/g,"$1 $2 $3"),u=u.replace(/([A-Za-z0-9])([\+\-\*\/=&\\|<>])([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])/g,"$1 $2 $3");var f=u,t=u.replace(/([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])([\(\[\{<\u201c]+(.*?)[\)\]\}>\u201d]+)([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])/g,"$1 $2 $4");return u=t,f===t&&(u=u.replace(/([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])([\(\[\{<\u201c>])/g,"$1 $2"),u=u.replace(/([\)\]\}>\u201d<])([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])/g,"$1 $2")),u=u.replace(/([\(\[\{<\u201c]+)(\s*)(.+?)(\s*)([\)\]\}>\u201d]+)/g,"$1$3$5"),u=u.replace(/([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])([~!;:,\.\?\u2026])([A-Za-z0-9])/g,"$1$2 $3"),u=u.replace(/([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])([A-Za-z0-9`\$%\^&\*\-=\+\\\|\/@\u00a1-\u00ff\u2022\u2027\u2150-\u218f])/g,"$1 $2"),u=u.replace(/([A-Za-z0-9`~\$%\^&\*\-=\+\\\|\/!;:,\.\?\u00a1-\u00ff\u2022\u2026\u2027\u2150-\u218f])([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])/g,"$1 $2")}},{key:"spacingText",value:function(e){var u=arguments.length<=1||void 0===arguments[1]?function(){}:arguments[1];try{var f=this.spacing(e);u(null,f)}catch(t){u(t)}}}]),e}();e.exports.Pangu=a}])}); | ||
//# sourceMappingURL=pangu.min.js.map |
@@ -37,3 +37,4 @@ 'use strict'; | ||
newText = newText.replace(/(["])([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])/g, '$1 $2'); | ||
newText = newText.replace(/(["'\(\[\{<\u201c]+)(\s*)(.+?)(\s*)(["'\)\]\}>\u201d]+)/g, '$1$3$5'); | ||
newText = newText.replace(/(["']+)(\s*)(.+?)(\s*)(["']+)/g, '$1$3$5'); | ||
newText = newText.replace(/([\(\[\{<\u201c]+)(\s*)(.+?)(\s*)([\)\]\}>\u201d]+)/g, '$1$3$5'); | ||
newText = newText.replace(/([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])( )(')([A-Za-z])/g, '$1$3$4'); | ||
@@ -40,0 +41,0 @@ |
# History | ||
v3.1.1 / 2016-06-26 | ||
------------------- | ||
- 剛吃完烤肉來改進一下 Paranoid Text Spacing 演算法 | ||
v3.1.0 / 2016-06-25 | ||
@@ -9,2 +15,3 @@ ------------------- | ||
v3.0.0 / 2016-01-10 | ||
@@ -16,8 +23,2 @@ ------------------- | ||
v2.5.7 / 2015-07-16 | ||
------------------- | ||
- 幫 [Taipei, China](http://newtalk.tw/news/view/2015-07-16/62294) 加上空格 | ||
v2.5.6 / 2015-05-13 | ||
@@ -24,0 +25,0 @@ ------------------- |
@@ -18,3 +18,3 @@ { | ||
], | ||
"version": "3.1.0", | ||
"version": "3.1.1", | ||
"author": { | ||
@@ -37,2 +37,4 @@ "name": "Vinta Chen", | ||
"scripts": { | ||
"pack:chrome": "cp -rf ./dist/browser/pangu.min.js ./browser_extensions/chrome/vendors/pangu.min.js && cp -rf ./browser_extensions/chrome/ ./browser_extensions/paranoid-auto-spacing/", | ||
"pack": "npm run pack:chrome", | ||
"clear:browser": "rm -rf ./dist/browser/", | ||
@@ -43,3 +45,3 @@ "clear:node": "rm -rf ./dist/node/ ./dist/shared/", | ||
"build:node": "npm run clear:node && babel src/ -d dist/ --ignore browser/*", | ||
"build": "npm run build:browser && npm run build:node", | ||
"build": "npm run build:browser && npm run build:node && npm run pack", | ||
"test:browser": "npm run build:browser && karma start", | ||
@@ -46,0 +48,0 @@ "test:node": "npm run build:node && mocha --compilers js:babel-register test/shared/ test/node/", |
@@ -18,3 +18,3 @@ # 為什麼你們就是不能加個空格呢? | ||
* [Google Chrome](https://chrome.google.com/webstore/detail/paphcfdffjnbcgkokihcdjliihicmbpd) (2015-05-13 updated) | ||
* [Google Chrome](https://chrome.google.com/webstore/detail/paphcfdffjnbcgkokihcdjliihicmbpd) (2016-06-26 updated) | ||
* [Mozilla Firefox](https://github.com/vinta/pangu.js/raw/master/browser_extensions/firefox/paranoid-auto-spacing.user.js) (2015-05-13 updated) | ||
@@ -36,3 +36,3 @@ | ||
``` bash | ||
```bash | ||
$ npm install pangu --save | ||
@@ -43,3 +43,3 @@ ``` | ||
``` html | ||
```html | ||
<head> | ||
@@ -62,5 +62,5 @@ // Files are located on /node_modules/pangu/dist/browser/ | ||
``` html | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/pangu/3.0.0/pangu.js"></script> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/pangu/3.0.0/pangu.min.js"></script> | ||
```html | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/pangu/3.1.1/pangu.js"></script> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/pangu/3.1.1/pangu.min.js"></script> | ||
``` | ||
@@ -70,3 +70,3 @@ | ||
``` js | ||
```js | ||
var pangu = require('pangu'); // ES5 | ||
@@ -78,16 +78,11 @@ import pangu from 'pangu'; // ES6 | ||
//./text.txt | ||
// 聖誕老人Hohoho三小 | ||
pangu.spacingFile('./text.txt', function(err, result) { | ||
// result = 聖誕老人 Hohoho 三小 | ||
pangu.spacingFile('/path/to/text.txt', function(err, data) { | ||
console.log(data); | ||
}); | ||
pangu.spacingFilePromise('./text.txt').then(function(result) { | ||
// result = 聖誕老人 Hohoho 三小 | ||
pangu.spacingFile('/path/to/text.txt').then(function(data) { | ||
console.log(data); | ||
}); | ||
const result = pangu.spacingFileSync('./text.txt'); | ||
// result = 聖誕老人 Hohoho 三小 | ||
const data = pangu.spacingFileSync('/path/to/text.txt'); | ||
``` | ||
@@ -99,5 +94,4 @@ | ||
``` bash | ||
$ git clone git@github.com:vinta/pangu.js.git | ||
$ cd pangu.js | ||
```bash | ||
$ git clone git@github.com:vinta/pangu.js.git && cd pangu.js | ||
$ npm install | ||
@@ -104,0 +98,0 @@ $ npm run test |
@@ -16,2 +16,6 @@ const Pangu = require('../shared/core').Pangu; | ||
this.blockTags = /^(div|h1|h2|h3|h4|h5|h6|p)$/i; | ||
// TODO | ||
// this.ignoreClasses | ||
// this.ignoreAttributes | ||
} | ||
@@ -23,3 +27,3 @@ | ||
while (parentNode && parentNode.nodeName && parentNode.nodeName.search(this.topTags) === -1) { | ||
if ((parentNode.getAttribute('contenteditable') === 'true') || (parentNode.getAttribute('g_editable') === 'true') || (parentNode.nodeName.search(this.ignoreTags) >= 0)) { | ||
if ((parentNode.nodeName.search(this.ignoreTags) >= 0) || (parentNode.isContentEditable) || (parentNode.getAttribute('g_editable') === 'true')) { | ||
return true; | ||
@@ -146,3 +150,3 @@ } | ||
} else { | ||
let panguSpace = document.createElement('pangu'); | ||
const panguSpace = document.createElement('pangu'); | ||
panguSpace.innerHTML = ' '; | ||
@@ -178,2 +182,33 @@ | ||
spacingNode(contextNode) { | ||
const xPathQuery = './/*/text()[normalize-space(.)]'; | ||
const hasSpacing = this.spacingNodeByXPath(xPathQuery, contextNode); | ||
return hasSpacing; | ||
} | ||
spacingElementById(idName) { | ||
const xPathQuery = `id("${idName}")//text()`; | ||
const hasSpacing = this.spacingNodeByXPath(xPathQuery); | ||
return hasSpacing; | ||
} | ||
spacingElementByClassName(className) { | ||
const xPathQuery = `//*[contains(concat(" ", normalize-space(@class), " "), "${className}")]//text()`; | ||
const hasSpacing = this.spacingNodeByXPath(xPathQuery); | ||
return hasSpacing; | ||
} | ||
spacingElementByTagName(tagName) { | ||
const xPathQuery = `//${tagName}//text()`; | ||
const hasSpacing = this.spacingNodeByXPath(xPathQuery); | ||
return hasSpacing; | ||
} | ||
spacingPageTitle() { | ||
@@ -226,3 +261,3 @@ const titleQuery = '/html/head/title/text()'; | ||
// TODO: 改用 promise | ||
// TODO: 支援 callback 和 promise | ||
spacingPage() { | ||
@@ -235,26 +270,2 @@ const hasSpacingPageTitle = this.spacingPageTitle(); | ||
spacingElementById(idName) { | ||
const xPathQuery = `id("${idName}")//text()`; | ||
const hasSpacing = this.spacingNodeByXPath(xPathQuery); | ||
return hasSpacing; | ||
} | ||
spacingElementByClassName(className) { | ||
const xPathQuery = `//*[contains(concat(" ", normalize-space(@class), " "), "${className}")]//text()`; | ||
const hasSpacing = this.spacingNodeByXPath(xPathQuery); | ||
return hasSpacing; | ||
} | ||
spacingElementByTagName(tagName) { | ||
const xPathQuery = `//${tagName}//text()`; | ||
const hasSpacing = this.spacingNodeByXPath(xPathQuery); | ||
return hasSpacing; | ||
} | ||
} | ||
@@ -264,2 +275,3 @@ | ||
module.exports = pangu; | ||
exports = module.exports = pangu; | ||
exports.Pangu = BrowserPangu; |
@@ -30,3 +30,4 @@ class Pangu { | ||
newText = newText.replace(/(["])([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])/g, '$1 $2'); | ||
newText = newText.replace(/(["'\(\[\{<\u201c]+)(\s*)(.+?)(\s*)(["'\)\]\}>\u201d]+)/g, '$1$3$5'); | ||
newText = newText.replace(/(["']+)(\s*)(.+?)(\s*)(["']+)/g, '$1$3$5'); | ||
newText = newText.replace(/([\(\[\{<\u201c]+)(\s*)(.+?)(\s*)([\)\]\}>\u201d]+)/g, '$1$3$5'); | ||
newText = newText.replace(/([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])( )(')([A-Za-z])/g, '$1$3$4'); | ||
@@ -33,0 +34,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
227268
1.29%31
3.33%1412
1.73%116
-4.92%