Is it possible to access inline styles in a structured way using cheerio? - cheerio

I have an element:
<svg style="width:100%;height:100%;">
I know that I can read the styles using cheerio in some way similar to:
const hasWidth = $el([style*="height"])
But I would need to do a lot of extra work to reliably parse CSS values from the style attribute.
Is there a more structured way to access styles of an element using cheerio?

Related

Can a data attribute be set with css? if so then how?

I have the following html fragment with a custom data attribute data-parm1:
<div id="my_div" data-parm1="string_A" ></div>
Can I set data-parm1 with css ?
CSS can't be used to add, manipulate or create nodes in the DOM (Document Object Model). Your best bet is to use JavaScript document.getElementById("my_div").setAttribute("data-param1", "StringB");
The above code sets data-param1 to StringB.

Adding HTML to Word using OpenXML but unable to style the content (.Net Core)

I managed to add HTML (text only) to a Word-document following this post Add HTML String to OpenXML, using an already existing Word-file.
Unfortunately, I can't find any solution to use style from this Word-template for my newly added text. It is always "Times New Roman" size 12px although the standard style of the used template is "Arial" size 9px.
So fare I tried:
Using the ParagraphProperties as I would do for not HTML texts.
Paragraph para = body.AppendChild(new Paragraph());
Run run = para.AppendChild(new Run());
run.AppendChild(altChunk);
para.ParagraphProperties = new ParagraphProperties(new ParagraphStyleId() { Val = "berschrift2" });
Turnig MatchSource off
AltChunkProperties altChunkProperties = new AltChunkProperties();
altChunkProperties.MatchSource = new MatchSource() { Val = new OnOffValue(false) };
altChunk.AppendChild<AltChunkProperties>(altChunkProperties);
Any suggestions?
EDIT:
I found a workaround, which isn´t really a solution for my question, but works for me. I'm no longer trying to use the style from word, but adding the styles to my html before using altchunk.
Some explanation: if you look at the definition of altChunk in ISO 29500-1 17.17.2.1 and specifically in the A.1 section, the schema shows that altChunk is a EG_BlockLevelElts element and this is a peer with paragraphs (i.e. ). It is technically not correct to add as a child to run elements or even paragraph. It should be added at the body level. The fact that Word doesn't complain when adding as a run or paragraph child is unintentional and shouldn't be relied on.
As a result, what Word is doing is using the default style property for fonts to format this new content. You can try this by changing the document defaults in the styles.xml part. With match source property set to false, there isn't a way to specify the font besides document defaults.
Having said that, I think that Thomas' alternative is a better way to go.
The real solution for your question is to transform HTML into Open XML markup "yourself" rather than relying on the alternative format import parts in conjunction with w:altChunk elements. This creates a dependency on how Microsoft Word handles the import, often with little control on your side.
How do you transform HTML (or XML in general) to Open XML markup? The best way is to write so-called recursive pure functional transformations, which translate HTML elements and attributes to Open XML elements and attributes. If you have really simple HTML documents, that is not a big task. However, doing this for "arbitrary" HTML and CSS is quite a feat.
The good news is that the Open-XML-PowerTools, an Open Source library, contain functionality to transform HTML to Open XML and vice versa. Thus, I'd recommend you have a look at that library.
What worked for me and for my situation (if you don't want to go down the rather complex openxml powertools html converter root) is to add a HTML style attribute to the body section of your HTML fragment as follows:
Encoding.UTF8.GetBytes(
#$"<html><head><title></title></head><body style=""font-family: Calibri"">{ConvertUnconventionalUnicodeCharsToAscii(htmlAsString)}</body></html>");
It might be possible to dynamically derive the font family of the "normal" style embedded into the document you are updating and insert that name into the style attribute if deemed compatible.
That way, if you decide to change the base/ normal font the style of the HTML import will attempt to utilise the same font family.
Sorry if a bit off topic, I also could not get alternativeFormatImportPart.FeedData() to process "’" (code 8217) UTF-16 characters and so had to specifically replace them with "'" (code 39) in order to avoid them from being rendered as the following sequence ’

What does it mean to set data-target attribute of a div to the id of that div?

I'm reading some code and there is a piece of html that reads:
<div id="uniqueId1234" data-target=".uniqueId1234">
...
</div>
and then earlier on in the same html file there is a span element that seems to use this div as a class:
<span class="uniqueId1234">
...
</span>
Can someone explain how this works? I thought that a class was something created in a css file. Sorry if this is a dumb question.
This is likely part of some piece of Javascript code or a library that listens for some type of change or event on your element with the data-target attribute.
When this event is triggered, it can then use the value of that attribute as a selector for performing some other logic as seen in this basic jQuery-based example below:
// When an element containing your data-target attribute is clicked
$('[data-target]').click(function(){
// Find the appropriate target (i.e. ".uniqueId1234")
var target = $(this).data('target');
// Then use it as a selector for some type of operation
$(target).toggle();
});
Classes are very common within CSS to style multiple elements, but they can also commonly be used as a mechanism in Javascript as well, which is likely the case in your scenario here.
What does it mean to set data-target attribute of a div to the id of that div?
Nothing standard. data-* attributes are designed to hold custom data for custom code (typically client side JS) to process.
I thought that a class was something created in a css file.
Classes are an HTML feature used to put elements into arbitrary groups. They are commonly used when writing CSS, but also client side JS and other code.

Rendering HTML text with an HTML tag with a 'style' attribute

I am using a spark RichText component to render a html text in my Flex Web Application.
The html text that is given to me is with HTML elements with 'style' attribute having all the styles.
For Example:-
<p style="text-align: left;"><b>Hello</b> <i>this is a sample</i>
<font style="color: #ff0000;">HTML text</font></p><p style="text-align: right;">
<u>to be rerndered in FLEX</u></p>
Now, the Flex spark RichText does not show all these styles applied to the text.
However, if I have HTML with inline property attributes (without 'style' attribute) e.g. :-
<font color="#ff0000">Hello</font>
With the above, I get the desired style.
Any pointers/ solution to get around with this, and render the styles will be appreciated.
Thanks,
Mangirish
Try using <span style="color:#FF0000;">HTML text</span> and see if that makes any difference? I'm not too sure how well Flex renders older HTML tags. Besides, <font> is deprecated anyway.
If your css is fix, you can use StyleSheet Object
Here is a tutorial : http://learnola.com/2008/12/03/actionscript-3-tutorial-using-html-and-css/
If you need more information, just ask it in comments.
Short answer, TLF won't support that kind of styles that I know of, but you can always try to convert it to see what happens:
var textflow:TextFlow = TextConverter.importToFlow(yourHTMLString, TextConverter.TEXT_FIELD_HTML_FORMAT);
However, I have a sneaking suspicion that it won't convert the style properly and probably just ignore it altogether. The only solution I can think of is
Have a custom way to parse the html and get the style out and set it on the textflow or
Have the 'server' html be fixed to be compatible with the flex TLF (inline styles)
I would personally prefer the second option since it's easier to implement on the client.

Is there a way to attatch a css file to a jEditorPane?

Simple enough question: I have a string containing HTML that is being handed off to a JEditorPane for user consumption.
Can I attach a CSS file (or string containing CSS rules) to allow for more specific styling of the text?
The HTMLEditorKit per default looks for a file default.css - I'm not sure where, though.
Alternatively, this should work:
StyleSheet ss = new StyleSheet();
ss.importStyleSheet(styleSheetURL);
HTMLEditorKit kit = (HTMLEditorKit)jEditorPane.getEditorKit();
kit.setStyleSheet(ss);
However, note that HTMLEditorKit only supports a limited subset of CSS 1.
Can't you just include a style tag along with the HTML content in setText()?
e.g.
jEditorPane.setText( "<html><head><style type=\"text/css\">...</style></head><body>...");