Comparing version 3.1.1 to 3.2.0
/*! | ||
* pangu.js | ||
* -------- | ||
* @version: 3.1.1 | ||
* @version: 3.2.0 | ||
* @homepage: https://github.com/vinta/pangu.js | ||
@@ -149,8 +149,3 @@ * @license: MIT | ||
key: 'spacingNodeByXPath', | ||
value: function spacingNodeByXPath(xPathQuery) { | ||
var contextNode = arguments.length <= 1 || arguments[1] === undefined ? document : arguments[1]; | ||
// 是否加了空格 | ||
var hasSpacing = false; | ||
value: function spacingNodeByXPath(xPathQuery, contextNode) { | ||
// 因為 xPathQuery 會是用 text() 結尾,所以這些 nodes 會是 text 而不是 DOM element | ||
@@ -162,4 +157,4 @@ // snapshotLength 要配合 XPathResult.ORDERED_NODE_SNAPSHOT_TYPE 使用 | ||
var currentTextNode = undefined; | ||
var nextTextNode = undefined; | ||
var currentTextNode = void 0; | ||
var nextTextNode = void 0; | ||
@@ -177,3 +172,2 @@ // 從最下面、最裡面的節點開始,所以是倒序的 | ||
if (currentTextNode.data !== newText) { | ||
hasSpacing = true; | ||
currentTextNode.data = newText; | ||
@@ -196,4 +190,2 @@ } | ||
if (testNewText !== testText) { | ||
hasSpacing = true; | ||
// 往上找 nextTextNode 的 parent node | ||
@@ -264,4 +256,2 @@ // 直到遇到 spaceSensitiveTags | ||
} | ||
return hasSpacing; | ||
} | ||
@@ -272,5 +262,3 @@ }, { | ||
var xPathQuery = './/*/text()[normalize-space(.)]'; | ||
var hasSpacing = this.spacingNodeByXPath(xPathQuery, contextNode); | ||
return hasSpacing; | ||
this.spacingNodeByXPath(xPathQuery, contextNode); | ||
} | ||
@@ -281,6 +269,3 @@ }, { | ||
var xPathQuery = 'id("' + idName + '")//text()'; | ||
var hasSpacing = this.spacingNodeByXPath(xPathQuery); | ||
return hasSpacing; | ||
this.spacingNodeByXPath(xPathQuery, document); | ||
} | ||
@@ -291,6 +276,3 @@ }, { | ||
var xPathQuery = '//*[contains(concat(" ", normalize-space(@class), " "), "' + className + '")]//text()'; | ||
var hasSpacing = this.spacingNodeByXPath(xPathQuery); | ||
return hasSpacing; | ||
this.spacingNodeByXPath(xPathQuery, document); | ||
} | ||
@@ -301,6 +283,3 @@ }, { | ||
var xPathQuery = '//' + tagName + '//text()'; | ||
var hasSpacing = this.spacingNodeByXPath(xPathQuery); | ||
return hasSpacing; | ||
this.spacingNodeByXPath(xPathQuery, document); | ||
} | ||
@@ -310,6 +289,4 @@ }, { | ||
value: function spacingPageTitle() { | ||
var titleQuery = '/html/head/title/text()'; | ||
var hasSpacing = this.spacingNodeByXPath(titleQuery); | ||
return hasSpacing; | ||
var xPathQuery = '/html/head/title/text()'; | ||
this.spacingNodeByXPath(xPathQuery, document); | ||
} | ||
@@ -346,3 +323,3 @@ }, { | ||
// 注意,以下的 query 只會取出各節點的 text 內容! | ||
var bodyQuery = '/html/body//*/text()[normalize-space(.)]'; | ||
var xPathQuery = '/html/body//*/text()[normalize-space(.)]'; | ||
var _arr = ['script', 'style', 'textarea']; | ||
@@ -354,7 +331,5 @@ for (var _i = 0; _i < _arr.length; _i++) { | ||
// ex: [translate(name(..), "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz") != "script"] | ||
bodyQuery += '[translate(name(..),"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")!="' + tag + '"]'; | ||
xPathQuery += '[translate(name(..),"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")!="' + tag + '"]'; | ||
} | ||
var hasSpacing = this.spacingNodeByXPath(bodyQuery); | ||
return hasSpacing; | ||
this.spacingNodeByXPath(xPathQuery, document); | ||
} | ||
@@ -367,6 +342,4 @@ | ||
value: function spacingPage() { | ||
var hasSpacingPageTitle = this.spacingPageTitle(); | ||
var hasSpacingPageBody = this.spacingPageBody(); | ||
return hasSpacingPageTitle || hasSpacingPageBody; | ||
this.spacingPageTitle(); | ||
this.spacingPageBody(); | ||
} | ||
@@ -427,6 +400,8 @@ }]); | ||
// hash_anscjk_hash | ||
// cjk_hash | ||
// hash_cjk | ||
newText = newText.replace(/([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])(#(\S+))/g, '$1 $2'); | ||
newText = newText.replace(/((\S+)#)([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])/g, '$1 $3'); | ||
newText = newText.replace(/([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])(#)([A-Za-z0-9\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff]+)(#)([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])/g, '$1 $2$3$4 $5'); | ||
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(/(([^ ])#)([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])/g, '$1 $3'); | ||
@@ -433,0 +408,0 @@ // cjk_operator_ans |
/*! | ||
* pangu.js | ||
* -------- | ||
* @version: 3.1.1 | ||
* @version: 3.2.0 | ||
* @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(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}])}); | ||
!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(a){if(f[a])return f[a].exports;var t=f[a]={exports:{},id:a,loaded:!1};return e[a].call(t.exports,t,t.exports,u),t.loaded=!0,t.exports}var f={};return u.m=e,u.c=f,u.p="",u(0)}([function(e,u,f){"use strict";function a(e,u){if(!(e instanceof u))throw new TypeError("Cannot call a class as a function")}function t(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 i=function(){function e(e,u){for(var f=0;f<u.length;f++){var a=u[f];a.enumerable=a.enumerable||!1,a.configurable=!0,"value"in a&&(a.writable=!0),Object.defineProperty(e,a.key,a)}}return function(u,f,a){return f&&e(u.prototype,f),a&&e(u,a),u}}(),r=f(1).Pangu,o=8,s=function(e){function u(){a(this,u);var e=t(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),i(u,[{key:"canIgnoreNode",value:function(e){for(var u=e.parentNode;u&&u.nodeName&&u.nodeName.search(this.topTags)===-1;){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,a=0;a<f.length;a++){var t=f[a];if(t.nodeType!==o&&t.textContent)return t===u}}},{key:"isLastTextChild",value:function(e,u){for(var f=e.childNodes,a=f.length-1;a>-1;a--){var t=f[a];if(t.nodeType!==o&&t.textContent)return t===u}}},{key:"spacingNodeByXPath",value:function(e,u){for(var f=document.evaluate(e,u,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null),a=void 0,t=void 0,n=f.snapshotLength-1;n>-1;--n)if(a=f.snapshotItem(n),this.canIgnoreNode(a))t=a;else{var i=this.spacing(a.data);if(a.data!==i&&(a.data=i),t){if(a.nextSibling&&a.nextSibling.nodeName.search(this.spaceLikeTags)>=0){t=a;continue}var r=a.data.toString().substr(-1)+t.data.toString().substr(0,1),o=this.spacing(r);if(o!==r){for(var s=t;s.parentNode&&s.nodeName.search(this.spaceSensitiveTags)===-1&&this.isFirstTextChild(s.parentNode,s);)s=s.parentNode;for(var c=a;c.parentNode&&c.nodeName.search(this.spaceSensitiveTags)===-1&&this.isLastTextChild(c.parentNode,c);)c=c.parentNode;if(c.nextSibling&&c.nextSibling.nodeName.search(this.spaceLikeTags)>=0){t=a;continue}if(c.nodeName.search(this.blockTags)===-1)if(s.nodeName.search(this.spaceSensitiveTags)===-1)s.nodeName.search(this.ignoreTags)===-1&&s.nodeName.search(this.blockTags)===-1&&(t.previousSibling?t.previousSibling.nodeName.search(this.spaceLikeTags)===-1&&(t.data=" "+t.data):this.canIgnoreNode(t)||(t.data=" "+t.data));else if(c.nodeName.search(this.spaceSensitiveTags)===-1)a.data=a.data+" ";else{var d=document.createElement("pangu");d.innerHTML=" ",s.previousSibling?s.previousSibling.nodeName.search(this.spaceLikeTags)===-1&&s.parentNode.insertBefore(d,s):s.parentNode.insertBefore(d,s),d.previousElementSibling||d.parentNode&&d.parentNode.removeChild(d)}}}t=a}}},{key:"spacingNode",value:function(e){var u=".//*/text()[normalize-space(.)]";this.spacingNodeByXPath(u,e)}},{key:"spacingElementById",value:function(e){var u='id("'+e+'")//text()';this.spacingNodeByXPath(u,document)}},{key:"spacingElementByClassName",value:function(e){var u='//*[contains(concat(" ", normalize-space(@class), " "), "'+e+'")]//text()';this.spacingNodeByXPath(u,document)}},{key:"spacingElementByTagName",value:function(e){var u="//"+e+"//text()";this.spacingNodeByXPath(u,document)}},{key:"spacingPageTitle",value:function(){var e="/html/head/title/text()";this.spacingNodeByXPath(e,document)}},{key:"spacingPageBody",value:function(){for(var e="/html/body//*/text()[normalize-space(.)]",u=["script","style","textarea"],f=0;f<u.length;f++){var a=u[f];e+='[translate(name(..),"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")!="'+a+'"]'}this.spacingNodeByXPath(e,document)}},{key:"spacingPage",value:function(){this.spacingPageTitle(),this.spacingPageBody()}}]),u}(r),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 a=function(){function e(e,u){for(var f=0;f<u.length;f++){var a=u[f];a.enumerable=a.enumerable||!1,a.configurable=!0,"value"in a&&(a.writable=!0),Object.defineProperty(e,a.key,a)}}return function(u,f,a){return f&&e(u.prototype,f),a&&e(u,a),u}}(),t=function(){function e(){f(this,e)}return a(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])(#)([A-Za-z0-9\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff]+)(#)([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])/g,"$1 $2$3$4 $5"),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 $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,a=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=a,f===a&&(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(a){u(a)}}}]),e}();e.exports.Pangu=t}])}); | ||
//# sourceMappingURL=pangu.min.js.map |
@@ -41,6 +41,8 @@ 'use strict'; | ||
// hash_anscjk_hash | ||
// cjk_hash | ||
// hash_cjk | ||
newText = newText.replace(/([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])(#(\S+))/g, '$1 $2'); | ||
newText = newText.replace(/((\S+)#)([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])/g, '$1 $3'); | ||
newText = newText.replace(/([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])(#)([A-Za-z0-9\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff]+)(#)([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])/g, '$1 $2$3$4 $5'); | ||
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(/(([^ ])#)([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])/g, '$1 $3'); | ||
@@ -47,0 +49,0 @@ // cjk_operator_ans |
# History | ||
v3.2.0 / 2016-06-26 | ||
------------------- | ||
- 修正效能問題 | ||
v3.1.1 / 2016-06-26 | ||
@@ -12,4 +18,4 @@ ------------------- | ||
- NodePangu 新增 `spacingFile()`,支援 callback 與 promise | ||
- NodePangu 新增 `spacingFileSync()` | ||
- `NodePangu` 新增 `spacingFile()`,支援 callback 與 promise | ||
- `NodePangu` 新增 `spacingFileSync()` | ||
@@ -16,0 +22,0 @@ |
@@ -18,3 +18,3 @@ { | ||
], | ||
"version": "3.1.1", | ||
"version": "3.2.0", | ||
"author": { | ||
@@ -42,5 +42,5 @@ "name": "Vinta Chen", | ||
"clear": "npm run clear:browser && npm run clear:node", | ||
"build:browser": "npm run clear:browser && webpack", | ||
"build:browser": "npm run clear:browser && webpack && npm run pack", | ||
"build:node": "npm run clear:node && babel src/ -d dist/ --ignore browser/*", | ||
"build": "npm run build:browser && npm run build:node && npm run pack", | ||
"build": "npm run build:browser && npm run build:node", | ||
"test:browser": "npm run build:browser && karma start", | ||
@@ -53,27 +53,29 @@ "test:node": "npm run build:node && mocha --compilers js:babel-register test/shared/ test/node/", | ||
"devDependencies": { | ||
"babel-cli": "6.3.17", | ||
"babel-core": "6.3.26", | ||
"babel-loader": "6.2.0", | ||
"babel-plugin-add-module-exports": "0.1.2", | ||
"babel-polyfill": "6.3.14", | ||
"babel-preset-es2015": "6.3.13", | ||
"babel-cli": "6.10.1", | ||
"babel-core": "6.10.4", | ||
"babel-loader": "6.2.4", | ||
"babel-plugin-add-module-exports": "0.2.1", | ||
"babel-polyfill": "6.9.1", | ||
"babel-preset-es2015": "6.9.0", | ||
"babel-register": "6.9.0", | ||
"chai": "3.4.1", | ||
"eslint": "1.10.3", | ||
"eslint-config-airbnb": "2.1.1", | ||
"istanbul": "0.4.1", | ||
"karma": "0.13.19", | ||
"chai": "3.5.0", | ||
"eslint": "2.13.1", | ||
"eslint-config-airbnb": "9.0.1", | ||
"eslint-config-airbnb-base": "3.0.1", | ||
"eslint-plugin-import": "1.9.2", | ||
"istanbul": "0.4.4", | ||
"karma": "1.0.0", | ||
"karma-babel-preprocessor": "6.0.1", | ||
"karma-coverage": "0.5.3", | ||
"karma-html2js-preprocessor": "0.1.0", | ||
"karma-mocha": "0.2.1", | ||
"karma-mocha-reporter": "1.1.5", | ||
"karma-phantomjs-launcher": "0.2.2", | ||
"karma-sourcemap-loader": "0.3.6", | ||
"karma-coverage": "1.0.0", | ||
"karma-html2js-preprocessor": "1.0.0", | ||
"karma-mocha": "1.0.1", | ||
"karma-mocha-reporter": "2.0.4", | ||
"karma-phantomjs-launcher": "1.0.1", | ||
"karma-sourcemap-loader": "0.3.7", | ||
"karma-webpack": "1.7.0", | ||
"mocha": "2.3.4", | ||
"phantomjs": "1.9.19", | ||
"mocha": "2.5.3", | ||
"phantomjs-prebuilt": "2.1.7", | ||
"underscore": "1.8.3", | ||
"webpack": "1.12.9" | ||
"webpack": "1.13.1" | ||
} | ||
} |
@@ -60,4 +60,4 @@ # 為什麼你們就是不能加個空格呢? | ||
```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> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/pangu/3.2.0/pangu.js"></script> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/pangu/3.2.0/pangu.min.js"></script> | ||
``` | ||
@@ -67,2 +67,4 @@ | ||
Learn more on [npm](https://www.npmjs.com/package/pangu). | ||
```js | ||
@@ -76,2 +78,3 @@ var pangu = require('pangu'); // ES5 | ||
pangu.spacingFile('/path/to/text.txt', function(err, data) { | ||
// callback | ||
console.log(data); | ||
@@ -81,2 +84,3 @@ }); | ||
pangu.spacingFile('/path/to/text.txt').then(function(data) { | ||
// promise | ||
console.log(data); | ||
@@ -83,0 +87,0 @@ }); |
@@ -66,6 +66,3 @@ const Pangu = require('../shared/core').Pangu; | ||
spacingNodeByXPath(xPathQuery, contextNode = document) { | ||
// 是否加了空格 | ||
let hasSpacing = false; | ||
spacingNodeByXPath(xPathQuery, contextNode) { | ||
// 因為 xPathQuery 會是用 text() 結尾,所以這些 nodes 會是 text 而不是 DOM element | ||
@@ -91,3 +88,2 @@ // snapshotLength 要配合 XPathResult.ORDERED_NODE_SNAPSHOT_TYPE 使用 | ||
if (currentTextNode.data !== newText) { | ||
hasSpacing = true; | ||
currentTextNode.data = newText; | ||
@@ -110,4 +106,2 @@ } | ||
if (testNewText !== testText) { | ||
hasSpacing = true; | ||
// 往上找 nextTextNode 的 parent node | ||
@@ -178,4 +172,2 @@ // 直到遇到 spaceSensitiveTags | ||
} | ||
return hasSpacing; | ||
} | ||
@@ -185,5 +177,3 @@ | ||
const xPathQuery = './/*/text()[normalize-space(.)]'; | ||
const hasSpacing = this.spacingNodeByXPath(xPathQuery, contextNode); | ||
return hasSpacing; | ||
this.spacingNodeByXPath(xPathQuery, contextNode); | ||
} | ||
@@ -193,6 +183,3 @@ | ||
const xPathQuery = `id("${idName}")//text()`; | ||
const hasSpacing = this.spacingNodeByXPath(xPathQuery); | ||
return hasSpacing; | ||
this.spacingNodeByXPath(xPathQuery, document); | ||
} | ||
@@ -202,6 +189,3 @@ | ||
const xPathQuery = `//*[contains(concat(" ", normalize-space(@class), " "), "${className}")]//text()`; | ||
const hasSpacing = this.spacingNodeByXPath(xPathQuery); | ||
return hasSpacing; | ||
this.spacingNodeByXPath(xPathQuery, document); | ||
} | ||
@@ -211,13 +195,8 @@ | ||
const xPathQuery = `//${tagName}//text()`; | ||
const hasSpacing = this.spacingNodeByXPath(xPathQuery); | ||
return hasSpacing; | ||
this.spacingNodeByXPath(xPathQuery, document); | ||
} | ||
spacingPageTitle() { | ||
const titleQuery = '/html/head/title/text()'; | ||
const hasSpacing = this.spacingNodeByXPath(titleQuery); | ||
return hasSpacing; | ||
const xPathQuery = '/html/head/title/text()'; | ||
this.spacingNodeByXPath(xPathQuery, document); | ||
} | ||
@@ -253,3 +232,3 @@ | ||
// 注意,以下的 query 只會取出各節點的 text 內容! | ||
let bodyQuery = '/html/body//*/text()[normalize-space(.)]'; | ||
let xPathQuery = '/html/body//*/text()[normalize-space(.)]'; | ||
for (const tag of ['script', 'style', 'textarea']) { | ||
@@ -259,7 +238,5 @@ // 理論上這幾個 tag 裡面不會包含其他 tag | ||
// ex: [translate(name(..), "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz") != "script"] | ||
bodyQuery += `[translate(name(..),"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")!="${tag}"]`; | ||
xPathQuery += `[translate(name(..),"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")!="${tag}"]`; | ||
} | ||
const hasSpacing = this.spacingNodeByXPath(bodyQuery); | ||
return hasSpacing; | ||
this.spacingNodeByXPath(xPathQuery, document); | ||
} | ||
@@ -269,6 +246,4 @@ | ||
spacingPage() { | ||
const hasSpacingPageTitle = this.spacingPageTitle(); | ||
const hasSpacingPageBody = this.spacingPageBody(); | ||
return hasSpacingPageTitle || hasSpacingPageBody; | ||
this.spacingPageTitle(); | ||
this.spacingPageBody(); | ||
} | ||
@@ -275,0 +250,0 @@ |
@@ -34,6 +34,8 @@ class Pangu { | ||
// hash_anscjk_hash | ||
// cjk_hash | ||
// hash_cjk | ||
newText = newText.replace(/([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])(#(\S+))/g, '$1 $2'); | ||
newText = newText.replace(/((\S+)#)([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])/g, '$1 $3'); | ||
newText = newText.replace(/([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])(#)([A-Za-z0-9\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff]+)(#)([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])/g, '$1 $2$3$4 $5'); | ||
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(/(([^ ])#)([\u2e80-\u2eff\u2f00-\u2fdf\u3040-\u309f\u30a0-\u30ff\u3100-\u312f\u3200-\u32ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff])/g, '$1 $3'); | ||
@@ -40,0 +42,0 @@ // cjk_operator_ans |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
120
224385
26
1400