By my test script I want to display the background color (hex value) of some of my web element to a .txt file but I want to display like this the background color of login button hex value. The hex value will be good to be highlighted. Is there any way I can do it.
Assuming there is a button that has a CSS as color and has a hex value #000000 and has some text, You can do something like this.
cy.get('button').then(($ele) => {
cy.writeFile(
'cypress/fixtures/colors.txt',
'The background color of the element ' +
$ele.text() +
' is ' +
$ele.css('color'),
{flag: 'a+'}
)
})
If you have multiple elements with the same selector, you can just replace then() with each()
cy.get('button').each(($ele) => {
cy.writeFile(
'cypress/fixtures/colors.txt',
'The background color of the element ' +
$ele.text() +
' is ' +
$ele.css('color'),
{flag: 'a+'}
)
})
Your colors.txt file should look like this:
The background color of the element Button-1 is #000000
The background color of the element Button-2 is #FFFFFF
...
...
...
The {flag: 'a+'} will make sure that all the new texts are added to the end of the file rather than overwriting it.
No, .txt files generally do not have bold formatting, but you could use an HTML file.
Wrap the text with <b>my text</b>, and add <br> to give new line.
const html = `<b>the background color of ${selector}</b> <span style="background-color: yellow">${hex}</span><br>`
cy.writeFile('cypress/backgroundColors.html', html, {flag: 'a+'})
HTML output
Converting rgb to hex
jQuery returns rgb not hex, but sometimes its rgb(0, 0, 0) and sometimes rgba(0, 0, 0, 0) (depending on what sets it).
Here's my full test that covers the conversion,
Cypress.Commands.add('hexBG', (selector) => {
cy.get(selector)
.invoke('css', 'background-color')
.then(rgb => {
const hex = (x) => ("0" + parseInt(x).toString(16)).slice(-2);
const rgb2hex = (rgb) => {
const match = rgb.match(/^rgb(a*)\((\d+),\s*(\d+),\s*(\d+)/);
return "#" + hex(match[2]) + hex(match[3]) + hex(match[4]);
}
return rgb2hex(rgb)
})
})
cy.visit('http://example.com')
const selector = 'h1'
cy.hexBG('h1')
.then(hex => {
const html = `<b>the background color of ${selector}</b> <span style="background-color: yellow">${hex}</span><br>`
cy.writeFile('cypress/backgroundColors.html', html, {flag: 'a+'})
})
cy.get('h1').invoke('css', 'background-color', 'green') // make it more interesting
cy.hexBG('h1')
.then(hex => {
const html = `<b>the background color of ${selector}</b> <span style="background-color: yellow">${hex}</span><br>`
cy.writeFile('cypress/backgroundColors.html', html, {flag: 'a+'})
})
HTML output
Related
I'm trying to do a tooltip similar to this:
https://codepen.io/team/amcharts/pen/dyyaxLr
But when a series is disabled via the legend (i.e. "cars"), I also want to remove the value in the tooltip.
I guess there should be a way to format the series.tooltipText with an adapter like this:
series.adapter.add("tooltipText", function (text, target) {
// generate text dynamically
// ...
return text;
});
But I can't figure out how to get only the data for the visible series and format the string accordingly.
Is something like this possible?
I found the following solution:
series.adapter.add("tooltipText", function (ev) {
var text = "[bold]{dateX}[/]\n";
x.series.each(function (item) {
if (!item.isHidden)
text +=
"[" +
item.stroke.hex +
"]●[/] " +
item.name +
": {" +
item.dataFields.valueY +
"}\n";
});
return text;
});
I am trying to add a text (as watermark) to images. I am using Image/Intervention package. The text shows but I want it to be at top right hand corner of the image and I also want the size increased. The text is currently at top-left corner and the size is extremely small.
This is my code
if($request->hasFile('file')) {
foreach ($request->file('file') as $photo) {
$file = $photo;
$img = Image::make($file);
$img->text('12345 ', 120, 100, function($font) {
$font->size(45);
$font->color('#e1e1e1');
$font->align('center');
$font->valign('top');
});
$img->save(public_path('images/hardik3.jpg'));
}
}
How do I solve this?
From the documentation:
Font sizing is only available if a font file is set and will be ignored otherwise. Default: 12
So you have to specify a custom font just like in the below example:
$img->text('foo', 0, 0, function($font) {
$font->file('foo/bar.ttf');
$font->size(24);
$font->color('#fdf6e3');
$font->align('center');
$font->valign('top');
$font->angle(45);
});
Update
The text alignment is relative to the size of the textbox, but the positioning is given by the x and y coordinates (2nd and 3rd parameters of the text method). To put the text on the right top corner you can do:
$img->text('foo', $img->width(), 100, function($font) {
$font->file('foo/bar.ttf');
$font->size(24);
$font->color('#e1e1e1');
$font->align('right');
$font->valign('top');
});
The text function accepts the X and Y coordinates of the position to insert the text. The text is being printed into the shown position because you used the coordinates 120 and 100.
Try the following:
if($request->hasFile('file')) {
foreach ($request->file('file') as $photo) {
$file = $photo;
$img = Image::make($file);
$img->text('12345 ', $img->width() - 120, 100, function($font) {
$font->size(45);
$font->color('#e1e1e1');
$font->align('center');
$font->valign('top');
});
$img->save(public_path('images/hardik3.jpg'));
}
}
Source: http://image.intervention.io/api/text
I am using Angular 4 with TypeScript for a web application. I am allowing users to upload a thumbnail profile photo from their device as either a png, jpeg, or jpg, and I want to convert that photo to a jpg on the frontend. I am looking for some way to do this, as the file type is readonly.
The reason that I am doing this is so that when users load the profile page, they don't have to download large images so the page is quick. I think that converting to a jpg might be the best bet because when tested with a sample image, a png of an image was 35.4KB while a converted jpg of the same image was 6.7KB. If there is a better solution, I would love to hear it!
Thank you in advance!
I just wrote some code like your requirements, here is what I did:
load a local image by FileReader and add a listener to its onload event
in the onload listener, create an "Image" object by new Image(), and set "src" attribute by the loaded image "src" (Data URL format)
create a "Canvas" element, and draw the image on this canvas
use Canvas.toDataURL() to fetch the converted image data (in base64)
post the image data to server
After you draw an image to canvas, call Canvas.toDataURL() will get the canvas content in Data URL string, please note that it's the canvas data not original image data, for example, if image size is 100 x 100 and canvas size is 50 x 50, you'll get an image in 50 x 50 pixel with this function, so if you want a full size image, you need to resize the canvas to the certain size.
this function has two parameters:
canvas.toDataURL(type, encoderOptions);
type - A DOMString indicating the image format. The default format type is image/png. in your code, set to "image/jpeg" in your case
encoderOptions - A Number between 0 and 1 indicating image quality if the requested type is image/jpeg or image/webp.
Here is my "preview and upload" function write in TypesScript for reference:
preview(input: HTMLInputElement) {
if (input.files.length) {
let reader = new FileReader();
reader.onload = (e) => {
if (!this.img) {
this.img = new Image();
}
this.img.src = (e.target as any).result;
this.img.onload = () => {
// omit code of setting 'dx', 'dy', 'width', 'height'
let ctx = <CanvasRenderingContext2D>this.canvas.nativeElement.getContext('2d');
ctx.drawImage(this.img, dx, dy, width, height);
let dataUrl = (<HTMLCanvasElement>this.canvas.nativeElement).toDataURL('image/jpeg', 0.7);
this.uploadService.upload(dataUrl).then((resp: any) => {
if (resp.key) {
this.asset.image = resp.key;
}
});
};
}
reader.readAsDataURL(input.files[0]);
}
}
I omitted some variables: dx, dx, width, height in the above code, I use these variables to adjust image position (for clipping purpose).
This is JavaScript example:
function _elm(id) {
return document.getElementById(id);
}
_elm('fileInput').onchange= function(event) {
if (event.target.files.length) {
var fileReader = new FileReader();
fileReader.onload = function(event) {
var img = new Image();
img.src = event.target.result;
_elm('sizeRaw').innerText = '+ data-url size ' + event.target.result.length;
img.onload = function() {
var canvas = _elm('canvas');
var context = canvas.getContext('2d');
context.drawImage(img, 0, 0, 200, 200);
var dataUrl = canvas.toDataURL('image/jpeg', 0.5);
_elm('output').innerText = dataUrl;
_elm('sizeNew').innerText = '+ data-url size ' + dataUrl.length;
}
};
fileReader.readAsDataURL(event.target.files[0]);
}
};
#canvas {
border: 1px solid blue;
}
#output {
word-break: break-all;
}
<h3>Input file <span id="sizeRaw"></span>: </h3>
<input id="fileInput" type="file" accept="image/*">
<h3>Canvas</h3>
<div>
<canvas id="canvas" width="200" height="200"></canvas>
</div>
<h3>Output <span id="sizeNew"></span>: </h3>
<div id="output">
</div>
I am using ckEditor to enable the user to configure HTML snippets that get used by my program as text templates. I have found a problem if styles are not explicitly set by the user in the ckEditor then when my program uses the HTML snippets. I want to allow the user to configure default styles that will be applied to div, span and paragraph elements if the user does not set an explicit style. I have found that I can do this using the code shown below.
function setEditorHtmlFilter(editor) {
var fontSize = "12pt";
var fontFamily = "arial,helvetica,sans-serif";
var lineHeight = "1.15";
editor.dataProcessor.htmlFilter.addRules({
elements: {
p: function(e) {
if (!e.attributes.style) {
e.attributes.style = "font-size:" + fontSize + ";font-family:" + fontFamily + ";line-height:" + lineHeight + ";";
}
},
span: function(e) {
if (!e.attributes.style) {
e.attributes.style = "font-size:" + fontSize + ";font-family:" + fontFamily + ";line-height:" + lineHeight + ";";
}
},
div: function (e) {
if (!e.attributes.style) {
e.attributes.style = "font-size:" + fontSize + ";font-family:" + fontFamily + ";line-height:" + lineHeight + ";";
}
}
}
}, {
applyToAll: true
});
}
My problem is that ckEditor ignores the style added to the containing div in the editor. For example the following div styles are ignored by the editor:
<div style="font-size:12pt;font-family:arial,helvetica,sans-serif;line-height:1.15;">abc</div>
<div style="font-size:12pt;font-family:arial,helvetica,sans-serif;line-height:1.15;">def</div>
<div style="font-size:12pt;font-family:arial,helvetica,sans-serif;line-height:1.15;">ghi</div>
In my ckEditor config.js I have the following setting:
config.enterMode = CKEDITOR.ENTER_DIV;
How can I get the editor to stop ignoring the styles set on the divs?
ckeditor creates its own style in <div> container so your style will be ignored but you can use class and external css with !important to give it a custom style. with !important you external css property will apply and inline css property created by ckeditor will be ignored
HTML
<div class="custom_div">abc</div>
<div class="custom_div">def</div>
<div class="custom_div">ghi</div>
Supposing you need to set black background color
CSS
.custom_div{
background:#000 !important;
/* all other css you want to apply*/
}
I have a CKeditor instance (version 4.1.2) with font, size, text and background color buttons in its toolbar, all completely default.
It's created by replacing a <textarea> whose contents are loaded from a database.
When the loaded html contains elements such as:
<h3><font color="red">Big Red Heading</font></h3>
CKeditor is simply stripping away the tags, to leave:
<h3>Big Red Heading</h3>
Whereas, my expectations (according to the docs) were that it should convert this to:
<h3><span style="color:red">Big Red Heading</span></h3>
(It strips tags with size and face attributes also, just the same way).
I haven't changed allowedContent or colorButton_foreStyle, or any other config setting that ought to have any effect on this issue. I've tried removing all custom config (leaving an absolutely default instance of the editor), and it still happens.
Can anyone explain why this might be happening, and how to fix it?
Thanks.
EDIT: The default value of colorButton_foreStyle is set like this in the CKeditor source:
CKEDITOR.config.colorButton_foreStyle = {
element: 'span',
styles: { 'color': '#(color)' },
overrides: [ { element: 'font', attributes: { 'color': null } } ]
};
...which is why I expected it would automatically convert font tags..?
CKEditor hasn't got all possible transformations defined by default. There is a set of them and it will be enlarged in the future, but this specific one wasn't defined yet.
From Advanced Content Filter guide - content transformations:
Currently, we have defined content transformations for only a handful of editor features, but their number will increase in future releases.
So, there are two solutions:
If you want to keep your font tags, then extend the Advanced Content Filter settings by defining config.extraAllowedContent and change the font plugins settings like in HTML output sample.
If you want to automatically transform your font tags to their newer equivalents, then you can add a new transformations. Read more in filter#addTransformations doc.
I got same problem in CKeditor 4. i searched but i didnt get solution. but i need it so i created my own method in js. its working great.
i created ownFunctoin:
function ConvertFontTagToSpanTag(str) {
var startIndex = str.indexOf('<font');
while (startIndex >= 0) {
var endIndex = str.substring(startIndex).indexOf('>');
var subString1 = str.substring(startIndex, (startIndex + endIndex) + 1);
var subString2 = subString1;
var mapObj = {
size: "font-size:",
face: "font-family:",
color: "color:"
};
subString2 = subString2.replace(/size|face|color/gi, function (matched) {
return mapObj[matched];
});
subString2 = subString2.replace(/['"]/g, ';');
subString2 = subString2.replace(/=;/g, '');
subString2 = subString2.replace('font', 'span');
if (subString2.length > 6) {
subString2 = [subString2.slice(0, 6), 'style=\"', subString2.slice(6)].join('');
subString2 = [subString2.slice(0, subString2.length - 1), '\"', subString2.slice(subString2.length - 1)].join('');
}
//Converting Font-size
var sizeIndex = subString2.indexOf('font-size:');
if (sizeIndex >= 0) {
var sizeEndIndex = subString2.substring(sizeIndex).indexOf(';');
var size = subString2.substring(sizeIndex + 10, (sizeIndex + sizeEndIndex));
//Removing Font size
subString2 = subString2.slice(0, (sizeIndex + sizeEndIndex) - 1) + subString2.slice((sizeIndex + sizeEndIndex));
//Adding Font Size
subString2 = [subString2.slice(0, (sizeIndex + sizeEndIndex)-1), ConvertSpanFontSize(size), subString2.slice((sizeIndex + sizeEndIndex)-1)].join('');
}
//end
str = str.replace(subString1, subString2);
startIndex = str.indexOf('<font');
}
str = str.replace(/font>/g, 'span>');
return str;
}
function ConvertSpanFontSize(size) {
switch (size) {
case '1': return '0.63em';
case '2': return '0.82em';
case '3': return '1.0em';
case '4': return '1.13em';
case '5': return '1.5em';
case '6': return '2em';
case '7': return '3em';
default: return '4em';
}
Cheers...! Thank you