Can you operate on XML / HTML documents in node.js? - dom

Is it possible to load an XML / HTML document with node.js and perform operations such as getElementsByTagName ?

node.js doesn't give you a DOM "out of the box", but there is a great module that provides a proper HTML DOM: https://github.com/tmpvar/jsdom.
Edit: there are also several modules that help with interacting with XML. Here's a list of them on the wiki (this page is deprecated): https://github.com/joyent/node/wiki/modules#wiki-parsers-xml

Cheerio: Tiny, fast, and elegant implementation of core jQuery designed specifically for the server.
var cheerio = require('cheerio'),
$ = cheerio.load('<h2 class="title">Hello world</h2>');
$('h2.title').text('Hello there!');
$('h2').addClass('welcome');
$.html();
//=> <h2 class="title welcome">Hello there!</h2>

Also check out https://github.com/robtweed/ewdDOM
This is a persistent DOM implementation using the Globals database

Related

How to work with the typo3 viewhelper documentation?

I found this example in the TYPO3 documentation:
<f:be.uri route="web_ts" parameters="{id: 92}"/>
Unfortunately the doc doesn't explain, what "route" is for.
"parameters" is for key-value-pairs (given to TYPO3 to work with), but unfortunately it isn't mentioned, how to add more than one and how they would become seperated.
Furthermore the doc is mentioning an argument "referenceType", which also isn't explained.
May I ask for some help/explanation for it?
Edit: The title of the question doesn't really reflect the nature of my question. stackoverflow wasn't allowing me to use a more precise one.
The documentation https://docs.typo3.org/m/typo3/reference-coreapi/master/en-us/ApiOverview/BackendRouting/Index.html explains backend routes quite well.
Furthermore the backend module "Configuration" > Backend Routes lists all available routes.
The core itself is most of the time a good example how ViewHelpers can be used, so a random example which covers quite a lot is the following one
<a href="{f:be.uri(route:'site_redirects', parameters: '{action: \'overview\', demand: demand.parameters, page: 1}')}" title="{f:translate(extensionName: 'fluid', key:'widget.pagination.first')}">
<core:icon identifier="actions-view-paging-first" />
</a>
Checking the implementation, referenceType can either be url or absolute to change the type of urls generated. I don't see currently the need to set this, so I guess you won't need it as well.

Typoscript filelink - Wrap URL within link

First, here is the Typoscript :
20 = TEXT
20 {
value {
field = field_title
wrap = |.txt
}
filelink {
stdWrap.wrap = <li>|</li>
path = fileadmin/txt-files/
}
}
The result I get is :
<li>
<a href="/fileadmin/txt-files/Title.txt">
<img src="typo3/sysext/frontend/Resources/Public/Icons/FileIcons/txt.png">
</a>
</li>
And what I need is :
<li>
<a href="/fileadmin/force_download_script.php?filepath=/fileadmin/txt-files/Title.txt">
<img src="typo3/sysext/frontend/Resources/Public/Icons/FileIcons/txt.png">
</a>
</li>
I need to make the link downloadable, rather than opening the file in the browser. For that I have a force_download_script.php, but when I do that :
wrap = fileadmin/force_download_script.php?filepath=|txt
instead of the current wrap, filelink doesn't find the file anymore.
I have tried using ATagBeforeWrap.wrap but it doesn't look like it's made for that purpose. I also tried typolinkConfiguration.wrap without any success.
Any idea of how to achieve that ? Using a COA maybe ?
Thank you !
I would not do this with a script, but with server configuration. If you use Apache and have .htaccess enabled, you can add the configuration to a .htaccess file in the directory where the files are located. See https://css-tricks.com/snippets/htaccess/force-files-to-download-not-open-in-browser/
Alternatively you can also use the HTML5 download attribute. This is not supported by Internet Explorer however (it is supported by Edge though).
The issue can get quite a bit complicated, but step by step:
your code above might be wrong if it's not just a copy & paste fault:
wrap = fileadmin/force_download_script.php?filepath=|.txt
The dot before txt was missing.
Nevertheless it is still interesting if the php-script is triggered.
It's possible that the script is not triggered due to some settings in typo3conf/LocalConfiguration.php respectively some settings in the install-tool.
Depending on the TYPO3-Version it's also possible that the script is not triggered at all because all scripts are being required now in an extension. That means you might need to create an extension for that script.
Also simple wrapping of the result with the script-path might not be enough, but you have to call it explicitly by TypoScript perhaps by including the script as user-function or lib.
The admin-panel might be useful to debug some things about your script but if not you've to include some debug-output first in your own code, if that's not enough in the core (temporary).
So you've to find out if your script is triggered and if not, the reason for it.
Are you sure .filelink is what you are looking for?
.filelink is for a set of files. For all files in the folder given by .path a link will be generated. see manual
From your description you want a text wrapped with a link to one single file. That would be more a problem for .typolink where you specify the link in .parameter.
if you really want a link list of multiple files, each wrapped with your script you need to modify .typolinkConfiguration.parameter which will be used internaly by .filelink.
Anyway it might be possible to do a wrap which then would be:
.typolinkConfiguration.parameter.wrap = /fileadmin/force_download_script.php?|
Maybe it is easier to build your list with .stdWrap.filelist, where you can use the filenames in any way to wrap your own href parameter for an A-tag.
To use the TYPO3 core solution with file links you can use this guide:
Create a file storage where you want your "secured" files in TYPO3 backend
Do not set the checkbox "Is public?" in the storage record
The links will be rendered with eID and file parameters
You can look into the FileDumpController handling these links: https://github.com/TYPO3/TYPO3.CMS/blob/2348992f8e3045610636666af096911436fa1c89/typo3/sysext/core/Classes/Controller/FileDumpController.php
You can use the included hook to extend this controller with your logic.
Unfortunately I can't find any official documentation for this feature, but will post it when I find something or wrote it myself. ;)
Maybe this can help you, too: https://extensions.typo3.org/extension/fal_securedownload/
Here is the official part, but it's not much: https://docs.typo3.org/typo3cms/CoreApiReference/ApiOverview/Fal/Administration/Storages.html?highlight=filedumpcontroller

How can we get value from DOM Properties in JMeter?

I'm trying to record a scenario of SAP CRM.
But I have a problem due to that everytime I login SAP CRM generates a new hashed token and will be used in URL like below:
See Image 1 Here
I tried to check where is the information stored, and in firebug and I found it in DOM tab:
See Image 2 Here
Is there any way to get the value from this DOM Properties using Jmeter?
Usually the choices are in:
CSS/JQuery Extractor
XPath Extractor
Regular Expression Extractor
Choose the one, you're most familiar with. Usually it is Regular Expression Extractor, however parsing HTML with regular expressions is not a good idea, moreover you will be very sensitive to DOM changes (part of the element goes to next line, attributes change positions, etc.).
So I would recommend choosing between CSS and XPath, but choose them wisely. I.e. if the number of styles on the page is not too big - go for CSS, if there are a lot of styles but the DOM itself is not very complicated - choose XPath.

Embedding csv in HTML for use with D3.js

In D3.js, one usually loads data from an external csv file. This is very efficient for large data, and avoids changing the code when the data changes.
However, there are two situations (only using small csv data) where I want to embed csv directly in an HTML page:
A page that can be loaded locally (i.e. from file:///), without requiring to run a local HTTP server.
A small jsfiddle example explaining a D3.js question for use on stackoverflow.
This is the solution I have come up with, using the example from the D3.js API.
Embedding the csv data:
<pre id="csvdata">
Year,Make,Model,Length
1997,Ford,E350,2.34
2000,Mercury,Cougar,2.38
</pre>
Hidding the raw data on the page:
#csvdata {
display: none;
}
Parsing it:
var raw = d3.select("#csvdata").text();
var parsed = d3.csv.parse(raw);
Optionally, show the result:
d3.select("#parsed").text(JSON.stringify(parsed));
// Assuming <div id="parsed"></div> somewhere on the page
If think this is flawed, or if you have a more elegant solution, I will gladly accept your answer!
EDIT: see it in action in this fiddle

PHP5.3 Oriented Object Modeling for HTML generation, a possibility or performance bottleneck?

The main motivation in this is that I was very tired or using php tags to write php views that were very ugly. Consider this approach:
<?php
<ul>
<?php echo foreach($products as $product): ?>
<li><?php echo $product->getName()?><li>
</ul>
This is the basic approach of trivial MVC with views, but I was tired of that, so I use phpQuery instead:
<?php
$ul = pq('<ul />');
foreach($products as $product) {
$ul->append(pq('<li />')
->html($product->getName())
);
}
echo $ul;
This is much cleaner. Unfortunately, phpQuery does not seem active on development. The thing is, phpQuery is programmed in a procedural way and is really hard to understand and maintain and contains some bugs that make it rather useless in most DOM manipulation cases. I was thinking of writing an OO approach to DOM writing based on jQuery using Zend Framework's component Zend_Dom_Query, with the same approach of phpQuery: same interface as jQuery.
After some search on the net on PHP HTML Generators, I cannot seem to find much that has been done on that part, and my question is, is using OO with PHP to generate HTML a performance bottleneck, since all static HTML would be server side generated?
I know there is the HAML approach, but I like the idea of DOM manipulation, so that it is possible to inject html at any time anywhere during the script execution. DOM manipulation on the server side leads to view script inheritence like this:
//a parent index.phtml
$container = pq('<div />')
->addClass('.container')
->html($this->_("Hello World!"));
$this->setRenderReturn($container);
//a child index.phtml
$container = $this->renderParent(__FILE__); //retrieves the parent just like the conventional parent:: in php scripts
$container->addClass('anotherClass');
$this->setRenderReturn($container);
Also, in case I missed a PHP library that does just that, I would gladly appreciate if someone could point me to it.
In direct answer to your question...
is using OO with PHP to generate HTML a performance bottleneck, since all static HTML would be server side generated?
Yes.
You should have a look at Twig and particularly the extensions added in Symfony 2. It supports template inheritance as well as named sections for overwriting / inserting markup.