Best JavaScript code snippet using wpt
FormatUtils.js
Source:FormatUtils.js
1/**2 * FormatUtils.js3 *4 * Released under LGPL License.5 * Copyright (c) 1999-2017 Ephox Corp. All rights reserved6 *7 * License: http://www.tinymce.com/license8 * Contributing: http://www.tinymce.com/contributing9 */10define(11 'tinymce.core.fmt.FormatUtils',12 [13 'tinymce.core.dom.TreeWalker'14 ],15 function (TreeWalker) {16 var isInlineBlock = function (node) {17 return node && /^(IMG)$/.test(node.nodeName);18 };19 var moveStart = function (dom, selection, rng) {20 var container = rng.startContainer,21 offset = rng.startOffset,22 walker, node, nodes;23 if (rng.startContainer === rng.endContainer) {24 if (isInlineBlock(rng.startContainer.childNodes[rng.startOffset])) {25 return;26 }27 }28 // Convert text node into index if possible29 if (container.nodeType === 3 && offset >= container.nodeValue.length) {30 // Get the parent container location and walk from there31 offset = dom.nodeIndex(container);32 container = container.parentNode;33 }34 // Move startContainer/startOffset in to a suitable node35 if (container.nodeType === 1) {36 nodes = container.childNodes;37 if (offset < nodes.length) {38 container = nodes[offset];39 walker = new TreeWalker(container, dom.getParent(container, dom.isBlock));40 } else {41 container = nodes[nodes.length - 1];42 walker = new TreeWalker(container, dom.getParent(container, dom.isBlock));43 walker.next(true);44 }45 for (node = walker.current(); node; node = walker.next()) {46 if (node.nodeType === 3 && !isWhiteSpaceNode(node)) {47 rng.setStart(node, 0);48 selection.setRng(rng);49 return;50 }51 }52 }53 };54 /**55 * Returns the next/previous non whitespace node.56 *57 * @private58 * @param {Node} node Node to start at.59 * @param {boolean} next (Optional) Include next or previous node defaults to previous.60 * @param {boolean} inc (Optional) Include the current node in checking. Defaults to false.61 * @return {Node} Next or previous node or undefined if it wasn't found.62 */63 var getNonWhiteSpaceSibling = function (node, next, inc) {64 if (node) {65 next = next ? 'nextSibling' : 'previousSibling';66 for (node = inc ? node : node[next]; node; node = node[next]) {67 if (node.nodeType === 1 || !isWhiteSpaceNode(node)) {68 return node;69 }70 }71 }72 };73 var isTextBlock = function (editor, name) {74 if (name.nodeType) {75 name = name.nodeName;76 }77 return !!editor.schema.getTextBlockElements()[name.toLowerCase()];78 };79 var isValid = function (ed, parent, child) {80 return ed.schema.isValidChild(parent, child);81 };82 var isWhiteSpaceNode = function (node) {83 return node && node.nodeType === 3 && /^([\t \r\n]+|)$/.test(node.nodeValue);84 };85 /**86 * Replaces variables in the value. The variable format is %var.87 *88 * @private89 * @param {String} value Value to replace variables in.90 * @param {Object} vars Name/value array with variables to replace.91 * @return {String} New value with replaced variables.92 */93 var replaceVars = function (value, vars) {94 if (typeof value !== "string") {95 value = value(vars);96 } else if (vars) {97 value = value.replace(/%(\w+)/g, function (str, name) {98 return vars[name] || str;99 });100 }101 return value;102 };103 /**104 * Compares two string/nodes regardless of their case.105 *106 * @private107 * @param {String/Node} str1 Node or string to compare.108 * @param {String/Node} str2 Node or string to compare.109 * @return {boolean} True/false if they match.110 */111 var isEq = function (str1, str2) {112 str1 = str1 || '';113 str2 = str2 || '';114 str1 = '' + (str1.nodeName || str1);115 str2 = '' + (str2.nodeName || str2);116 return str1.toLowerCase() === str2.toLowerCase();117 };118 var normalizeStyleValue = function (dom, value, name) {119 // Force the format to hex120 if (name === 'color' || name === 'backgroundColor') {121 value = dom.toHex(value);122 }123 // Opera will return bold as 700124 if (name === 'fontWeight' && value === 700) {125 value = 'bold';126 }127 // Normalize fontFamily so "'Font name', Font" becomes: "Font name,Font"128 if (name === 'fontFamily') {129 value = value.replace(/[\'\"]/g, '').replace(/,\s+/g, ',');130 }131 return '' + value;132 };133 var getStyle = function (dom, node, name) {134 return normalizeStyleValue(dom, dom.getStyle(node, name), name);135 };136 var getTextDecoration = function (dom, node) {137 var decoration;138 dom.getParent(node, function (n) {139 decoration = dom.getStyle(n, 'text-decoration');140 return decoration && decoration !== 'none';141 });142 return decoration;143 };144 var getParents = function (dom, node, selector) {145 return dom.getParents(node, selector, dom.getRoot());146 };147 return {148 isInlineBlock: isInlineBlock,149 moveStart: moveStart,150 getNonWhiteSpaceSibling: getNonWhiteSpaceSibling,151 isTextBlock: isTextBlock,152 isValid: isValid,153 isWhiteSpaceNode: isWhiteSpaceNode,154 replaceVars: replaceVars,155 isEq: isEq,156 normalizeStyleValue: normalizeStyleValue,157 getStyle: getStyle,158 getTextDecoration: getTextDecoration,159 getParents: getParents160 };161 }...
Using AI Code Generation
1CKEDITOR.plugins.add( 'test', {2 init: function( editor ) {3 editor.on( 'key', function( evt ) {4 range = editor.getSelection().getRanges()[ 0 ],5 startPath = new CKEDITOR.dom.elementPath( range.startContainer ),6 textNode = startPath.contains( CKEDITOR.dom.walker.nodeType( CKEDITOR.NODE_TEXT ) );7 if ( textNode && CKEDITOR.plugins.wptextpattern.isWhitespaceNode( textNode ) ) {8 alert( 'whitespace' );9 }10 });11 }12});
Using AI Code Generation
1var wptextpattern = require( 'wptextpattern' );2var isWhitespaceNode = wptextpattern.isWhitespaceNode;3var wptextpattern = require( 'wptextpattern' );4var isWhitespaceNode = wptextpattern.isWhitespaceNode;5var node = { "nodeName": "#text", "nodeValue": " " };6var result = isWhitespaceNode( node );7console.log( result );8isWhitespaceNode( node )9var wptextpattern = require( 'wptextpattern' );10var isWhitespaceNode = wptextpattern.isWhitespaceNode;11var node = { "nodeName": "#text", "nodeValue": " " };12var result = isWhitespaceNode( node );13console.log( result );
Using AI Code Generation
1CKEDITOR.plugins.add('test', {2 init: function(editor) {3 editor.on('key', function(event) {4 var key = event.data.keyCode;5 var range = editor.getSelection().getRanges()[ 0 ];6 var node = range.startContainer;7 if (key === 32) {8 if (node.type === CKEDITOR.NODE_TEXT && editor.plugins.wptextpattern.isWhitespaceNode(node)) {9 console.log('whitespace');10 }11 }12 });13 }14});
Using AI Code Generation
1CKEDITOR.plugins.add('wptextpattern', {2 init: function (editor) {3 editor.on('paste', function (evt) {4 var data = evt.data.dataValue;5 var data = data.replace(/ /gi, ' ');6 var data = data.replace(/&/gi, '&');7 var data = data.replace(/</gi, '<');8 var data = data.replace(/>/gi, '>');9 var data = data.replace(/"/gi, '"');10 var data = data.replace(/'/gi, "'");11 evt.data.dataValue = data;12 var data = evt.data.dataValue;13 var data = data.replace(/ /gi, ' ');14 var data = data.replace(/&/gi, '&');15 var data = data.replace(/</gi, '<');16 var data = data.replace(/>/gi, '>');17 var data = data.replace(/"/gi, '"');18 var data = data.replace(/'/gi, "'");19 evt.data.dataValue = data;20 var data = evt.data.dataValue;21 var data = data.replace(/ /gi, ' ');22 var data = data.replace(/&/gi, '&');23 var data = data.replace(/</gi, '<');24 var data = data.replace(/>/gi, '>');25 var data = data.replace(/"/gi, '"');26 var data = data.replace(/'/gi, "'");27 evt.data.dataValue = data;28 var data = evt.data.dataValue;29 var data = data.replace(/ /gi, ' ');30 var data = data.replace(/&/gi, '&');31 var data = data.replace(/</gi, '<');32 var data = data.replace(/>/gi, '>');33 var data = data.replace(/"/gi, '"');34 var data = data.replace(/'/gi, "'");35 evt.data.dataValue = data;36 var data = evt.data.dataValue;37 var data = data.replace(/ /gi, ' ');38 var data = data.replace(/&/gi, '&');39 var data = data.replace(/</gi, '<');40 var data = data.replace(/>/gi, '>');
Using AI Code Generation
1var wptextpattern = CKEDITOR.plugins.get('wptextpattern');2var element = editor.document.getBody();3var isWhitespace = wptextpattern.isWhitespaceNode(element);4var wptextpattern = CKEDITOR.plugins.get('wptextpattern');5var element = editor.document.getBody();6var range = wptextpattern.getWhitespaceRange(element);7var wptextpattern = CKEDITOR.plugins.get('wptextpattern');8var element = editor.document.getBody();9var isPattern = wptextpattern.isTextPattern(element);10var wptextpattern = CKEDITOR.plugins.get('wptextpattern');11var element = editor.document.getBody();12var range = wptextpattern.getTextPatternRange(element);13var wptextpattern = CKEDITOR.plugins.get('wptextpattern');14var element = editor.document.getBody();15var pattern = wptextpattern.getTextPattern(element);16var wptextpattern = CKEDITOR.plugins.get('wptextpattern');17var element = editor.document.getBody();18var pattern = wptextpattern.replaceTextPattern(element);19var wptextpattern = CKEDITOR.plugins.get('wptextpattern');20var element = editor.document.getBody();21var pattern = wptextpattern.insertTextPattern(element);22var wptextpattern = CKEDITOR.plugins.get('wptextpattern');23var element = editor.document.getBody();
Using AI Code Generation
1CKEDITOR.plugins.add( 'wptextpattern', {2 init: function( editor ) {3 editor.addCommand( 'wptextpattern', {4 exec: function( editor ) {5 var sel = editor.getSelection();6 var range = sel.getRanges()[0];7 var startContainer = range.startContainer;8 var endContainer = range.endContainer;9 var textNode = editor.document.getBody().getFirst();10 var isWhitespaceNode = editor.plugins.wptextpattern.isWhitespaceNode( textNode );11 console.log( isWhitespaceNode );12 }13 });14 editor.ui.addButton( 'wptextpattern', {15 });16 }17});18CKEDITOR.plugins.add( 'wptextpattern', {19 init: function( editor ) {20 editor.addCommand( 'wptextpattern', {21 exec: function( editor ) {22 var sel = editor.getSelection();23 var range = sel.getRanges()[0];24 var startContainer = range.startContainer;25 var endContainer = range.endContainer;26 var textNode = editor.document.getBody().getFirst();27 var isWhitespaceNode = editor.plugins.wptextpattern.isWhitespaceNode( textNode );28 console.log( isWhitespaceNode );29 }30 });31 editor.ui.addButton( 'wptextpattern', {32 });33 }34});35CKEDITOR.plugins.add( 'wptextpattern', {36 init: function( editor ) {37 editor.addCommand( 'wptextpattern', {38 exec: function( editor ) {39 var sel = editor.getSelection();40 var range = sel.getRanges()[0];41 var startContainer = range.startContainer;42 var endContainer = range.endContainer;43 var textNode = editor.document.getBody().getFirst();
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!