Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import ElementType from 'domelementtype'
import cssSelect from 'css-select'
import DomUtils from 'domutils'
import attributes from 'css-select/lib/attributes'
import Parser from 'htmlparser2/lib/Parser'
import domSerializer from './_domSerializer'
// monkey patching css-select/lib/attributes to reflect difference in how classes are stored
// Note: in XNode classes are stored in a Set instead of a string
const _elementRule = attributes.rules.element
attributes.rules.element = function(next, data) {
if (data.name === 'class') {
let value = data.value
if (/\s/.test(value)) return function() { return false }
return function clazz(elem) {
let classes = elem.classes
return classes && classes.has(value) && next(elem)
}
} else {
return _elementRule(next, data)
}
}
Parser.prototype.oncdata = function(value){
this._updatePosition(1);
if(this._options.xmlMode){
import ElementType from 'domelementtype'
import cssSelect from 'css-select'
import DomUtils from 'domutils'
import attributes from 'css-select/lib/attributes'
import Parser from 'htmlparser2/lib/Parser'
import domSerializer from './_domSerializer'
// monkey patching css-select/lib/attributes to reflect difference in how classes are stored
// Note: in XNode classes are stored in a Set instead of a string
const _elementRule = attributes.rules.element
attributes.rules.element = function(next, data) {
if (data.name === 'class') {
let value = data.value
if (/\s/.test(value)) return function() { return false }
return function clazz(elem) {
let classes = elem.classes
return classes && classes.has(value) && next(elem)
}
} else {
return _elementRule(next, data)
}
}
Parser.prototype.oncdata = function(value){
this._updatePosition(1);
request(options, function (error, response, body) {
if (null !== error || 200 != response.statusCode)
{
return reject({error:error,response:response,body:body});
}
let list = [];
const dom = htmlparser2.parseDOM(body);
const trList = CSSselect.selectAll('table tbody tr', dom);
_.forEach(trList, (tr, trIndex) => {
let tdList = CSSselect.selectAll('td', tr);
// not enough column ?
if (mapping.length != tdList.length)
{
// no date (ie single column No data was found for the selected time period. )
if (1 == tdList.length && 1 == trList.length)
{
return false;
}
logger.warn("Could not parse Coin Market Cap historical data for '%s' (row #%d) : got %d columns instead of %d", currency, trIndex, tdList.length, mapping.length);
return false;
}
let entry = {};
_.forEach(tdList, (td, tdIndex) => {
entry[mapping[tdIndex]] = 0 != tdIndex ? self._parseFloat(td) : self._parseDate(td);
_.forEach(trList, (tr, trIndex) => {
let tdList = CSSselect.selectAll('td', tr);
// not enough column ?
if (mapping.length != tdList.length)
{
// no date (ie single column No data was found for the selected time period. )
if (1 == tdList.length && 1 == trList.length)
{
return false;
}
logger.warn("Could not parse Coin Market Cap historical data for '%s' (row #%d) : got %d columns instead of %d", currency, trIndex, tdList.length, mapping.length);
return false;
}
let entry = {};
_.forEach(tdList, (td, tdIndex) => {
entry[mapping[tdIndex]] = 0 != tdIndex ? self._parseFloat(td) : self._parseDate(td);
});
// coin market cap will return newest first
numberOfCiters: 0,
scholarId: null,
}, refractoryPeriod);
break;
case SCHOLAR_REQUEST_TYPE_CITERS:
dispatchAndEcho({type: RESOLVE_SCHOLAR_CITERS_REQUEST}, refractoryPeriod);
break;
default:
console.error(`Unexpected Scholar request type ${state.scholar.requests[0].type}`);
dispatchAndEcho(null, refractoryPeriod);
break;
}
} else {
switch (state.scholar.requests[0].type) {
case SCHOLAR_REQUEST_TYPE_INITIALIZE: {
const publications = cssSelect(publicationsQuery, dom);
const candidates = [];
for (const publication of publications) {
const titleCandidates = cssSelect(titleFromPublicationQuery, publication);
if (
titleCandidates.length === 0
|| !titleCandidates[0].children
|| titleCandidates[0].children.length === 0
|| titleCandidates[0].children[0].type !== 'text'
) {
continue;
}
for (let citersLinkCandidate of cssSelect(citersLinkFromPublicationQuery, publication)) {
if (
citersLinkCandidate.children
&& citersLinkCandidate.children.length > 0
&& citersLinkCandidate.children[0].type === 'text'
break;
case SCHOLAR_REQUEST_TYPE_CITERS:
dispatchAndEcho({type: RESOLVE_SCHOLAR_CITERS_REQUEST}, refractoryPeriod);
break;
default:
console.error(`Unexpected Scholar request type ${state.scholar.requests[0].type}`);
dispatchAndEcho(null, refractoryPeriod);
break;
}
} else {
switch (state.scholar.requests[0].type) {
case SCHOLAR_REQUEST_TYPE_INITIALIZE: {
const publications = cssSelect(publicationsQuery, dom);
const candidates = [];
for (const publication of publications) {
const titleCandidates = cssSelect(titleFromPublicationQuery, publication);
if (
titleCandidates.length === 0
|| !titleCandidates[0].children
|| titleCandidates[0].children.length === 0
|| titleCandidates[0].children[0].type !== 'text'
) {
continue;
}
for (let citersLinkCandidate of cssSelect(citersLinkFromPublicationQuery, publication)) {
if (
citersLinkCandidate.children
&& citersLinkCandidate.children.length > 0
&& citersLinkCandidate.children[0].type === 'text'
&& citersLinkCandidate.attribs
&& citersLinkCandidate.attribs.href
) {
parent.content.forEach(elem => {
if (is(proxy(elem), rule.selector)) {
const style = elem.hasAttr('style') ? elem.attr('style').value.split(';') : []
for (let key in rule.declarations) {
style.push(`${key}: ${rule.declarations[key]};`)
}
elem.addAttr({
name: 'style',
local: 'style',
value: style.join(' '),
prefix: ''
})
}
if (!elem.isEmpty()) {
walk(elem, rule)
}
})
}
public querySelectorAll(selector: string): HTMLElement[] {
if (!CACHED_CSS_SELECTORS.has(selector)) {
CACHED_CSS_SELECTORS.set(selector, cssSelect.compile(selector));
}
const matches: any[] = cssSelect(
CACHED_CSS_SELECTORS.get(selector) as cssSelect.CompiledQuery,
this._document.children
);
const result = matches.map((element) => {
return new HTMLElement(element, this);
});
return result;
}
function matcherFromSelector(declarations, selector) {
const match = CSSselect.compile(selector);
const specificity = calculateSpecificity(selector);
const properties = declarationsToProperties(declarations, specificity);
return {
match(element) {
return match(element) ? properties : null;
},
specificity
};
}
}
export function unblockScholar() {
return {type: UNBLOCK_SCHOLAR};
}
export function changeRecaptchaVisibility() {
return {type: CHANGE_RECAPTCHA_VISIBILITY};
}
export function scholarDisconnect() {
return {type: SCHOLAR_DISCONNECT};
}
const firstCategoryRecaptchaQuery = cssSelect.compile('#gs_captcha_c');
const secondCategoryRecaptchaQuery = cssSelect.compile('.g-recaptcha');
const titleQuery = cssSelect.compile('head title');
const publicationsQuery = cssSelect.compile('.gs_r.gs_or.gs_scl .gs_ri');
const titleFromPublicationQuery = cssSelect.compile('.gs_rt a');
const citersLinkFromPublicationQuery = cssSelect.compile('.gs_fl a');
const metadataFromPublicationQuery = cssSelect.compile('.gs_a');
export function resolveHtml(url, text) {
return (dispatch, getState) => {
const dispatchAndEcho = (action, refractoryPeriod) => {
if (action != null) {
const now = Date.now();
dispatch({
...action,
beginOfRefractoryPeriod: now,
endOfRefractoryPeriod: now + refractoryPeriod,
});