How to use textdecode method in wpt

Best JavaScript code snippet using wpt

content_disposition.js

Source:content_disposition.js Github

copy

Full Screen

...28 }29 function toParamRegExp(attributePattern, flags) {30 return new RegExp('(?:^|;)\\s*' + attributePattern + '\\s*=\\s*' + '(' + '[^";\\s][^;\\s]*' + '|' + '"(?:[^"\\\\]|\\\\"?)+"?' + ')', flags);31 }32 function textdecode(encoding, value) {33 if (encoding) {34 if (!/^[\x00-\xFF]+$/.test(value)) {35 return value;36 }37 try {38 let decoder = new TextDecoder(encoding, {39 fatal: true40 });41 let bytes = Array.from(value, function (ch) {42 return ch.charCodeAt(0) & 0xFF;43 });44 value = decoder.decode(new Uint8Array(bytes));45 needsEncodingFixup = false;46 } catch (e) {47 if (/^utf-?8$/i.test(encoding)) {48 try {49 value = decodeURIComponent(escape(value));50 needsEncodingFixup = false;51 } catch (err) {}52 }53 }54 }55 return value;56 }57 function fixupEncoding(value) {58 if (needsEncodingFixup && /[\x80-\xff]/.test(value)) {59 value = textdecode('utf-8', value);60 if (needsEncodingFixup) {61 value = textdecode('iso-8859-1', value);62 }63 }64 return value;65 }66 function rfc2231getparam(contentDisposition) {67 let matches = [],68 match;69 let iter = toParamRegExp('filename\\*((?!0\\d)\\d+)(\\*?)', 'ig');70 while ((match = iter.exec(contentDisposition)) !== null) {71 let [, n, quot, part] = match;72 n = parseInt(n, 10);73 if (n in matches) {74 if (n === 0) {75 break;76 }77 continue;78 }79 matches[n] = [quot, part];80 }81 let parts = [];82 for (let n = 0; n < matches.length; ++n) {83 if (!(n in matches)) {84 break;85 }86 let [quot, part] = matches[n];87 part = rfc2616unquote(part);88 if (quot) {89 part = unescape(part);90 if (n === 0) {91 part = rfc5987decode(part);92 }93 }94 parts.push(part);95 }96 return parts.join('');97 }98 function rfc2616unquote(value) {99 if (value.startsWith('"')) {100 let parts = value.slice(1).split('\\"');101 for (let i = 0; i < parts.length; ++i) {102 let quotindex = parts[i].indexOf('"');103 if (quotindex !== -1) {104 parts[i] = parts[i].slice(0, quotindex);105 parts.length = i + 1;106 }107 parts[i] = parts[i].replace(/\\(.)/g, '$1');108 }109 value = parts.join('"');110 }111 return value;112 }113 function rfc5987decode(extvalue) {114 let encodingend = extvalue.indexOf('\'');115 if (encodingend === -1) {116 return extvalue;117 }118 let encoding = extvalue.slice(0, encodingend);119 let langvalue = extvalue.slice(encodingend + 1);120 let value = langvalue.replace(/^[^']*'/, '');121 return textdecode(encoding, value);122 }123 function rfc2047decode(value) {124 if (!value.startsWith('=?') || /[\x00-\x19\x80-\xff]/.test(value)) {125 return value;126 }127 return value.replace(/=\?([\w-]*)\?([QqBb])\?((?:[^?]|\?(?!=))*)\?=/g, function (_, charset, encoding, text) {128 if (encoding === 'q' || encoding === 'Q') {129 text = text.replace(/_/g, ' ');130 text = text.replace(/=([0-9a-fA-F]{2})/g, function (_, hex) {131 return String.fromCharCode(parseInt(hex, 16));132 });133 return textdecode(charset, text);134 }135 try {136 text = atob(text);137 } catch (e) {}138 return textdecode(charset, text);139 });140 }141 return '';...

Full Screen

Full Screen

content_disposition.ts

Source:content_disposition.ts Github

copy

Full Screen

1/**2 * Adapted directly from content-disposition.js at 3 * https://github.com/Rob--W/open-in-browser/blob/master/extension/content-disposition.js4 * which is licensed as:5 * 6 * (c) 2017 Rob Wu <rob@robwu.nl> (https://robwu.nl)7 * This Source Code Form is subject to the terms of the Mozilla Public8 * License, v. 2.0. If a copy of the MPL was not distributed with this9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.10 */11import { toParamRegExp, unquote } from "./headers.ts";12let needsEncodingFixup = false;13function fixupEncoding(value: string): string {14 if (needsEncodingFixup && /[\x80-\xff]/.test(value)) {15 value = textDecode("utf-8", value);16 if (needsEncodingFixup) {17 value = textDecode("iso-8859-1", value);18 }19 }20 return value;21}22const FILENAME_STAR_REGEX = toParamRegExp("filename\\*", "i");23const FILENAME_START_ITER_REGEX = toParamRegExp(24 "filename\\*((?!0\\d)\\d+)(\\*?)",25 "ig",26);27const FILENAME_REGEX = toParamRegExp("filename", "i");28function rfc2047decode(value: string): string {29 if (!value.startsWith("=?") || /[\x00-\x19\x80-\xff]/.test(value)) {30 return value;31 }32 return value.replace(33 /=\?([\w-]*)\?([QqBb])\?((?:[^?]|\?(?!=))*)\?=/g,34 (_: string, charset: string, encoding: string, text: string) => {35 if (encoding === "q" || encoding === "Q") {36 text = text.replace(/_/g, " ");37 text = text.replace(38 /=([0-9a-fA-F]{2})/g,39 (_, hex) => String.fromCharCode(parseInt(hex, 16)),40 );41 return textDecode(charset, text);42 }43 try {44 text = atob(text);45 } catch {}46 return textDecode(charset, text);47 },48 );49}50function rfc2231getParam(header: string): string {51 const matches: [string, string][] = [];52 let match: RegExpExecArray | null;53 while ((match = FILENAME_START_ITER_REGEX.exec(header))) {54 const [, ns, quote, part] = match;55 const n = parseInt(ns, 10);56 if (n in matches) {57 if (n === 0) {58 break;59 }60 continue;61 }62 matches[n] = [quote, part];63 }64 const parts: string[] = [];65 for (let n = 0; n < matches.length; ++n) {66 if (!(n in matches)) {67 break;68 }69 let [quote, part] = matches[n];70 part = unquote(part);71 if (quote) {72 part = unescape(part);73 if (n === 0) {74 part = rfc5987decode(part);75 }76 }77 parts.push(part);78 }79 return parts.join("");80}81function rfc5987decode(value: string): string {82 const encodingEnd = value.indexOf(`'`);83 if (encodingEnd === -1) {84 return value;85 }86 const encoding = value.slice(0, encodingEnd);87 const langValue = value.slice(encodingEnd + 1);88 return textDecode(encoding, langValue.replace(/^[^']*'/, ""));89}90function textDecode(encoding: string, value: string): string {91 if (encoding) {92 try {93 const decoder = new TextDecoder(encoding, { fatal: true });94 const bytes = Array.from(value, (c) => c.charCodeAt(0));95 if (bytes.every((code) => code <= 0xFF)) {96 value = decoder.decode(new Uint8Array(bytes));97 needsEncodingFixup = false;98 }99 } catch {}100 }101 return value;102}103export function getFilename(header: string): string {104 needsEncodingFixup = true;105 // filename*=ext-value ("ext-value" from RFC 5987, referenced by RFC 6266).106 let matches = FILENAME_STAR_REGEX.exec(header);107 if (matches) {108 const [, filename] = matches;109 return fixupEncoding(110 rfc2047decode(rfc5987decode(unescape(unquote(filename)))),111 );112 }113 // Continuations (RFC 2231 section 3, referenced by RFC 5987 section 3.1).114 // filename*n*=part115 // filename*n=part116 const filename = rfc2231getParam(header);117 if (filename) {118 return fixupEncoding(rfc2047decode(filename));119 }120 // filename=value (RFC 5987, section 4.1).121 matches = FILENAME_REGEX.exec(header);122 if (matches) {123 const [, filename] = matches;124 return fixupEncoding(rfc2047decode(unquote(filename)));125 }126 return "";...

Full Screen

Full Screen

app.js

Source:app.js Github

copy

Full Screen

1function encodeAndDecodeMessages() {2 let buttonSend = document.getElementsByTagName('button')[0];3 let buttonDecode = document.getElementsByTagName('button')[1];4 let textSend = document.getElementsByTagName('textarea')[0];5 let textDecode = document.getElementsByTagName('textarea')[1];6 buttonSend.addEventListener('click', encode);7 buttonDecode.addEventListener('click', decode);8 function encode(e) {9 if (textSend.value.length > 0) {10 let rawMessage = textSend.value;11 let encodedMessage = rawMessage.split('')12 .map(el => String.fromCharCode(el.charCodeAt() + 1))13 .join('');14 textSend.value = '';15 textDecode.value = encodedMessage;16 }17 }18 function decode(e) {19 if (textDecode.value.length > 0) {20 let decodedMessage = textDecode.value.split('')21 .map(el => String.fromCharCode(el.charCodeAt() - 1))22 .join('')23 textDecode.value = decodedMessage;24 }25 }...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var decoder = new TextDecoder("utf-8");2var encoded = new Uint8Array(3);3encoded[0] = 0xE2;4encoded[1] = 0x82;5encoded[2] = 0xAC;6var decoded = decoder.decode(encoded);7console.log(decoded);8var decoder = new TextDecoder("utf-8");9var encoded = new Uint8Array(3);10encoded[0] = 0xE2;11encoded[1] = 0x82;12encoded[2] = 0xAC;13var decoded = decoder.decode(encoded);14console.log(decoded);15var decoder = new TextDecoder("utf-8");16var encoded = new Uint8Array(3);17encoded[0] = 0xE2;18encoded[1] = 0x82;19encoded[2] = 0xAC;20var decoded = decoder.decode(encoded);21console.log(decoded);22var decoder = new TextDecoder("utf-8");23var encoded = new Uint8Array(3);24encoded[0] = 0xE2;25encoded[1] = 0x82;26encoded[2] = 0xAC;27var decoded = decoder.decode(encoded);28console.log(decoded);29var decoder = new TextDecoder("utf-8");30var encoded = new Uint8Array(3);31encoded[0] = 0xE2;32encoded[1] = 0x82;33encoded[2] = 0xAC;34var decoded = decoder.decode(encoded);35console.log(decoded);36var decoder = new TextDecoder("utf-8");37var encoded = new Uint8Array(3);38encoded[0] = 0xE2;39encoded[1] = 0x82;40encoded[2] = 0xAC;41var decoded = decoder.decode(encoded);42console.log(decoded);43var decoder = new TextDecoder("utf-8");44var encoded = new Uint8Array(3);45encoded[0] = 0xE2;46encoded[1] = 0x82;47encoded[2] = 0xAC;

Full Screen

Using AI Code Generation

copy

Full Screen

1var buffer = new ArrayBuffer(8); 2var view = new Uint8Array(buffer); 3view[0] = 0x65; 4view[1] = 0x6e; 5view[2] = 0x67; 6view[3] = 0x6c; 7view[4] = 0x69; 8view[5] = 0x73; 9view[6] = 0x68; 10view[7] = 0x21; 11var decoder = new TextDecoder(); 12var result = decoder.decode(buffer); 13console.log(result);

Full Screen

Using AI Code Generation

copy

Full Screen

1var decoder = new TextDecoder();2var data = decoder.decode(new Uint8Array([0x74, 0x65, 0x73, 0x74]));3var decoder = new TextDecoder();4var data = decoder.decode(new Uint8Array([0x74, 0x65, 0x73, 0x74]));5var decoder = new TextDecoder();6var data = decoder.decode(new Uint8Array([0x74, 0x65, 0x73, 0x74]));7var decoder = new TextDecoder();8var data = decoder.decode(new Uint8Array([0x74, 0x65, 0x73, 0x74]));9var decoder = new TextDecoder();10var data = decoder.decode(new Uint8Array([0x74, 0x65, 0x73, 0x74]));11var decoder = new TextDecoder();12var data = decoder.decode(new Uint8Array([0x74, 0x65, 0x73, 0x74]));13var decoder = new TextDecoder();14var data = decoder.decode(new Uint8Array([0x74, 0x65, 0x73, 0x74]));15var decoder = new TextDecoder();16var data = decoder.decode(new Uint8Array([0x74, 0x65, 0x73, 0x74]));17var decoder = new TextDecoder();18var data = decoder.decode(new Uint8Array([0x74, 0x65, 0x73, 0x74]));19var decoder = new TextDecoder();20var data = decoder.decode(new Uint8Array([0x74, 0x65, 0x73, 0x74]));

Full Screen

Using AI Code Generation

copy

Full Screen

1var textDecoder = new TextDecoder();2var text = textDecoder.decode(new Uint8Array([65,66,67,68]));3console.log(text);4var textDecoder = new TextDecoder();5var text = textDecoder.decode(new Uint8Array([65,66,67,68]));6console.log(text);7TextDecoder([encoding, options])

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run wpt automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful