Parsing HTML with Movian.¶
Starting with version 4.9.389 Movian features a HTML parsing library called gumbo (see https://github.com/google/gumbo-parser). This allows parsing of a HTML DOM using APIs not very different from a normal browser.
To use it first do:
var html = require('showtime/html');
Then to parse a DOM, simply do:
var dom = html.parse('<html><body><h1>This is a heading</h1></body></html>');
The returned object contains two objects
{ document: {}, root: {} }
root corresponds to the <html> element.
Element have a limited set of methods to what's available in a browser. If you find something important missing here, please let us know.
Element properties¶
nodeName¶
Name of element such as a,div,span, etc
nodeType¶
DOM3 node type, see https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType for a list
children¶
Return all children of the node (excluding text, comment)
textContent¶
Return textual contents of node.
attributes¶
Return array of attributes for the element. The array object also have the method getNamedItem() that can be used to obtain a specific attribute by name.
Element methods¶
getElementById(id)¶
Return first element found with the given id
getElementByClassName(names)¶
Return array of elements matching the given class(es). Names is a string representing the list of class names to match; class names are separated by white space.
getElementByTagName(name)¶
Return array of elements matching the given tag names.
Example¶
For example how to use this, see the svtplay plugin: https://github.com/andoma/showtime-plugin-svtplay/blob/master/svtplay.js