pseudo-localization 2.3.2 → 2.3.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/lib/index.js +1 -1
- package/lib/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -38,12 +38,12 @@ import pseudoLocalization from 'pseudo-localization';
|
|
|
38
38
|
|
|
39
39
|
pseudoLocalization.start();
|
|
40
40
|
|
|
41
|
-
pseudoLocalization.isEnabled // true
|
|
41
|
+
pseudoLocalization.isEnabled() // true
|
|
42
42
|
|
|
43
43
|
// Later, if needed
|
|
44
44
|
pseudoLocalization.stop();
|
|
45
45
|
|
|
46
|
-
pseudoLocalization.isEnabled // false
|
|
46
|
+
pseudoLocalization.isEnabled() // false
|
|
47
47
|
|
|
48
48
|
```
|
|
49
49
|
|
package/lib/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:true});Object.defineProperty(exports,"localize",{enumerable:true,get:function get(){return _localize.default}});exports.default=void 0;var _localize=_interopRequireDefault(require("./localize.js"));function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}var pseudoLocalization=function(){var opts={blacklistedNodeNames:["STYLE"]};var
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});Object.defineProperty(exports,"localize",{enumerable:true,get:function get(){return _localize.default}});exports.default=void 0;var _localize=_interopRequireDefault(require("./localize.js"));function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}var pseudoLocalization=function(){var opts={blacklistedNodeNames:["STYLE"]};var enabled=false;var observer=null;var observerConfig={characterData:true,childList:true,subtree:true};var textNodesUnder=function textNodesUnder(element){var walker=document.createTreeWalker(element,NodeFilter.SHOW_TEXT,function(node){var isAllWhitespace=!/[^\s]/.test(node.nodeValue);if(isAllWhitespace)return NodeFilter.FILTER_REJECT;var isBlacklistedNode=opts.blacklistedNodeNames.includes(node.parentElement.nodeName);if(isBlacklistedNode)return NodeFilter.FILTER_REJECT;return NodeFilter.FILTER_ACCEPT});var currNode;var textNodes=[];while(currNode=walker.nextNode()){textNodes.push(currNode)}return textNodes};var isNonEmptyString=function isNonEmptyString(str){return str&&typeof str==="string"};var pseudoLocalize=function pseudoLocalize(element){var textNodesUnderElement=textNodesUnder(element);var _iteratorNormalCompletion=true;var _didIteratorError=false;var _iteratorError=undefined;try{for(var _iterator=textNodesUnderElement[Symbol.iterator](),_step;!(_iteratorNormalCompletion=(_step=_iterator.next()).done);_iteratorNormalCompletion=true){var textNode=_step.value;var nodeValue=textNode.nodeValue;if(isNonEmptyString(nodeValue)){textNode.nodeValue=(0,_localize.default)(nodeValue,opts)}}}catch(err){_didIteratorError=true;_iteratorError=err}finally{try{if(!_iteratorNormalCompletion&&_iterator.return!=null){_iterator.return()}}finally{if(_didIteratorError){throw _iteratorError}}}};var domMutationCallback=function domMutationCallback(mutationsList){var _iteratorNormalCompletion2=true;var _didIteratorError2=false;var _iteratorError2=undefined;try{for(var _iterator2=mutationsList[Symbol.iterator](),_step2;!(_iteratorNormalCompletion2=(_step2=_iterator2.next()).done);_iteratorNormalCompletion2=true){var mutation=_step2.value;if(mutation.type==="childList"&&mutation.addedNodes.length>0){observer.disconnect();mutation.addedNodes.forEach(pseudoLocalize);observer.observe(document.body,observerConfig)}else if(mutation.type==="characterData"){var nodeValue=mutation.target.nodeValue;var isBlacklistedNode=opts.blacklistedNodeNames.includes(mutation.target.parentElement.nodeName);if(isNonEmptyString(nodeValue)&&!isBlacklistedNode){observer.disconnect();mutation.target.nodeValue=(0,_localize.default)(nodeValue,opts);observer.observe(document.body,observerConfig)}}}}catch(err){_didIteratorError2=true;_iteratorError2=err}finally{try{if(!_iteratorNormalCompletion2&&_iterator2.return!=null){_iterator2.return()}}finally{if(_didIteratorError2){throw _iteratorError2}}}};var isEnabled=function isEnabled(){return enabled};var start=function start(){var _ref=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{},_ref$strategy=_ref.strategy,strategy=_ref$strategy===void 0?"accented":_ref$strategy,_ref$blacklistedNodeN=_ref.blacklistedNodeNames,blacklistedNodeNames=_ref$blacklistedNodeN===void 0?opts.blacklistedNodeNames:_ref$blacklistedNodeN;if(isEnabled()){console.error("pseudo-localization is already enabled");return}opts.blacklistedNodeNames=blacklistedNodeNames;opts.strategy=strategy;pseudoLocalize(document.body);observer=new MutationObserver(domMutationCallback);observer.observe(document.body,observerConfig);enabled=true};var stop=function stop(){if(!isEnabled()){console.error("pseudo-localization is already disabled");return}observer&&observer.disconnect();enabled=false};return{start:start,stop:stop,isEnabled:isEnabled,localize:_localize.default}}();var _default=pseudoLocalization;exports.default=_default;
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.js"],"names":["pseudoLocalization","opts","blacklistedNodeNames","
|
|
1
|
+
{"version":3,"sources":["../src/index.js"],"names":["pseudoLocalization","opts","blacklistedNodeNames","enabled","observer","observerConfig","characterData","childList","subtree","textNodesUnder","element","walker","document","createTreeWalker","NodeFilter","SHOW_TEXT","node","isAllWhitespace","test","nodeValue","FILTER_REJECT","isBlacklistedNode","includes","parentElement","nodeName","FILTER_ACCEPT","currNode","textNodes","nextNode","push","isNonEmptyString","str","pseudoLocalize","textNodesUnderElement","textNode","domMutationCallback","mutationsList","mutation","type","addedNodes","length","disconnect","forEach","observe","body","target","isEnabled","start","strategy","console","error","MutationObserver","stop","localize","pseudoLocalizeString"],"mappings":"sMAAA,+D,kFASA,GAAMA,CAAAA,kBAAkB,CAAI,UAAM,CAChC,GAAMC,CAAAA,IAAI,CAAG,CACXC,oBAAoB,CAAE,CAAC,OAAD,CADX,CAAb,CAKA,GAAIC,CAAAA,OAAO,CAAG,KAAd,CAIA,GAAIC,CAAAA,QAAQ,CAAG,IAAf,CACA,GAAMC,CAAAA,cAAc,CAAG,CACrBC,aAAa,CAAE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M4C,CAAAA,KAAK,CAAG,QAARA,CAAAA,KAAQ,EAGH,oEAAP,EAAO,oBAFTC,QAES,CAFTA,QAES,wBAFE,UAEF,0CADT9C,oBACS,CADTA,oBACS,gCADcD,IAAI,CAACC,oBACnB,uBACT,GAAI4C,SAAS,EAAb,CAAiB,CACfG,OAAO,CAACC,KAAR,CAAc,wCAAd,EACA,MACD,CAEDjD,IAAI,CAACC,oBAAL,CAA4BA,oBAA5B,CACAD,IAAI,CAAC+C,QAAL,CAAgBA,QAAhB,CAEAhB,cAAc,CAACpB,QAAQ,CAACgC,IAAV,CAAd,CAGAxC,QAAQ,CAAG,GAAI+C,CAAAA,gBAAJ,CAAqBhB,mBAArB,CAAX,CACA/B,QAAQ,CAACuC,OAAT,CAAiB/B,QAAQ,CAACgC,IAA1B,CAAgCvC,cAAhC,EACAF,OAAO,CAAG,IACX,CAlBD,CAoBA,GAAMiD,CAAAA,IAAI,CAAG,QAAPA,CAAAA,IAAO,EAAM,CACjB,GAAI,CAACN,SAAS,EAAd,CAAkB,CAChBG,OAAO,CAACC,KAAR,CAAc,yCAAd,EACA,MACD,CAED9C,QAAQ,EAAIA,QAAQ,CAACqC,UAAT,EAAZ,CACAtC,OAAO,CAAG,KACX,CARD,CAUA,MAAO,CACL4C,KAAK,CAALA,KADK,CAELK,IAAI,CAAJA,IAFK,CAGLN,SAAS,CAATA,SAHK,CAILO,QAAQ,CAAEC,iBAJL,CAMR,CAxH0B,EAA3B,C,aA0HetD,kB","sourcesContent":["import pseudoLocalizeString from './localize.js';\n\n/**\n * export the underlying pseudo localization function so this import style\n * import { localize } from 'pseudo-localization';\n * can be used.\n */\nexport { default as localize } from './localize.js';\n\nconst pseudoLocalization = (() => {\n const opts = {\n blacklistedNodeNames: ['STYLE'],\n };\n\n // Indicates whether the pseudo-localization is currently enabled.\n let enabled = false;\n\n // Observer for dom updates. Initialization is defered to make parts\n // of the API safe to use in non-browser environments like nodejs\n let observer = null;\n const observerConfig = {\n characterData: true,\n childList: true,\n subtree: true,\n };\n\n const textNodesUnder = element => {\n const walker = document.createTreeWalker(\n element,\n NodeFilter.SHOW_TEXT,\n node => {\n const isAllWhitespace = !/[^\\s]/.test(node.nodeValue);\n if (isAllWhitespace) return NodeFilter.FILTER_REJECT;\n\n const isBlacklistedNode = opts.blacklistedNodeNames.includes(\n node.parentElement.nodeName\n );\n if (isBlacklistedNode) return NodeFilter.FILTER_REJECT;\n\n return NodeFilter.FILTER_ACCEPT;\n }\n );\n\n let currNode;\n const textNodes = [];\n while ((currNode = walker.nextNode())) textNodes.push(currNode);\n return textNodes;\n };\n\n const isNonEmptyString = str => str && typeof str === 'string';\n\n const pseudoLocalize = element => {\n const textNodesUnderElement = textNodesUnder(element);\n for (let textNode of textNodesUnderElement) {\n const nodeValue = textNode.nodeValue;\n if (isNonEmptyString(nodeValue)) {\n textNode.nodeValue = pseudoLocalizeString(nodeValue, opts);\n }\n }\n };\n\n const domMutationCallback = mutationsList => {\n for (let mutation of mutationsList) {\n if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {\n // Turn the observer off while performing dom manipulation to prevent\n // infinite dom mutation callback loops\n observer.disconnect();\n // For every node added, recurse down it's subtree and convert\n // all children as well\n mutation.addedNodes.forEach(pseudoLocalize);\n observer.observe(document.body, observerConfig);\n } else if (mutation.type === 'characterData') {\n const nodeValue = mutation.target.nodeValue;\n const isBlacklistedNode = opts.blacklistedNodeNames.includes(\n mutation.target.parentElement.nodeName\n );\n if (isNonEmptyString(nodeValue) && !isBlacklistedNode) {\n // Turn the observer off while performing dom manipulation to prevent\n // infinite dom mutation callback loops\n observer.disconnect();\n // The target will always be a text node so it can be converted\n // directly\n mutation.target.nodeValue = pseudoLocalizeString(nodeValue, opts);\n observer.observe(document.body, observerConfig);\n }\n }\n }\n };\n\n const isEnabled = () => {\n return enabled;\n }\n\n const start = ({\n strategy = 'accented',\n blacklistedNodeNames = opts.blacklistedNodeNames,\n } = {}) => {\n if (isEnabled()) {\n console.error('pseudo-localization is already enabled');\n return;\n }\n\n opts.blacklistedNodeNames = blacklistedNodeNames;\n opts.strategy = strategy;\n // Pseudo localize the DOM\n pseudoLocalize(document.body);\n // Start observing the DOM for changes and run\n // pseudo localization on any added text nodes\n observer = new MutationObserver(domMutationCallback);\n observer.observe(document.body, observerConfig);\n enabled = true;\n };\n\n const stop = () => {\n if (!isEnabled()) {\n console.error('pseudo-localization is already disabled');\n return;\n }\n\n observer && observer.disconnect();\n enabled = false;\n };\n\n return {\n start,\n stop,\n isEnabled,\n localize: pseudoLocalizeString,\n };\n})();\n\nexport default pseudoLocalization;\n"],"file":"index.js"}
|