diff --git a/.eslintignore b/.eslintignore index 9f7712124..fcba99d7a 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,3 +1,40 @@ *.min.js +.git/ +.github/ +_mydocs/ +_releases/ +Assets/ +CliClient/build +CliClient/locales +CliClient/node_modules +CliClient/tests-build +CliClient/tests/enex_to_md +CliClient/tests/html_to_md +CliClient/tests/logs +CliClient/tests/support +CliClient/tests/sync +CliClient/tests/tmp +Clipper/joplin-webclipper/content_scripts/JSDOMParser.js +Clipper/joplin-webclipper/content_scripts/Readability-readerable.js +Clipper/joplin-webclipper/content_scripts/Readability.js +Clipper/joplin-webclipper/dist +Clipper/joplin-webclipper/icons +Clipper/joplin-webclipper/popup/build +Clipper/joplin-webclipper/popup/node_modules +docs/ +ElectronClient/app/dist +ElectronClient/app/lib +ElectronClient/app/lib/vendor/sjcl-rn.js +ElectronClient/app/lib/vendor/sjcl.js +ElectronClient/app/locales +ElectronClient/app/node_modules highlight.pack.js -ReactNativeClient/lib/vendor/ \ No newline at end of file +node_modules/ +ReactNativeClient/android +ReactNativeClient/ios +ReactNativeClient/lib/vendor/ +ReactNativeClient/locales +ReactNativeClient/node_modules +readme/ +Tools/node_modules +Tools/PortableAppsLauncher \ No newline at end of file diff --git a/.eslintrc.js b/.eslintrc.js index 352709e43..30c87e4c8 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -4,30 +4,50 @@ module.exports = { 'es6': true, 'node': true, }, - 'extends': ['eslint:recommended', 'prettier'], + 'extends': ['eslint:recommended'], 'globals': { 'Atomics': 'readonly', - 'SharedArrayBuffer': 'readonly' + 'SharedArrayBuffer': 'readonly', + + // Jasmine variables + 'expect': 'readonly', + 'describe': 'readonly', + 'it': 'readonly', + 'beforeEach': 'readonly', + 'jasmine': 'readonly', + + // React Native variables + '__DEV__': 'readonly', + + // Clipper variables + 'browserSupportsPromises_': true, + 'chrome': 'readonly', + 'browser': 'readonly', }, 'parserOptions': { 'ecmaVersion': 2018, "ecmaFeatures": { "jsx": true, }, + "sourceType": "module", }, 'rules': { "react/jsx-uses-react": "error", "react/jsx-uses-vars": "error", - "no-unused-vars": ["error", { "argsIgnorePattern": "event|reject|resolve|prevState|snapshot|prevProps" }], + // Ignore all unused function arguments, because in some + // case they are kept to indicate the function signature. + "no-unused-vars": ["error", { "argsIgnorePattern": ".*" }], "no-constant-condition": 0, "no-prototype-builtins": 0, - "prettier/prettier": "error", - // Uncomment this to automatically remove unused requires: - // "autofix/no-unused-vars": "error", + "space-in-parens": ["error", "never"], + "semi": ["error", "always"], + "eol-last": ["error", "always"], + "quotes": ["error", "single"], + "indent": ["error", "tab"], + "comma-dangle": ["error", "always-multiline"], + "no-trailing-spaces": "error", }, "plugins": [ "react", - "prettier", - "autofix", ], }; \ No newline at end of file diff --git a/.prettierignore b/.prettierignore deleted file mode 100644 index 9f7712124..000000000 --- a/.prettierignore +++ /dev/null @@ -1,3 +0,0 @@ -*.min.js -highlight.pack.js -ReactNativeClient/lib/vendor/ \ No newline at end of file diff --git a/.prettierrc.json b/.prettierrc.json deleted file mode 100644 index a3c93e608..000000000 --- a/.prettierrc.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "useTabs": true, - "singleQuote": true, - "printWidth": 1000, - "semi": true, - "trailingComma": "es5", - "endOfLine": "auto" -} \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index e96a7a0c3..5b1a8a657 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -40,10 +40,7 @@ Building the apps is relatively easy - please [see the build instructions](https ## Coding style -There are only two rules, but not following them means the pull request will not be accepted (it can be accepted once the issues are fixed): - -- **Please use tabs, NOT spaces.** -- **Please do not add or remove optional characters, such as spaces or colons.** Please setup your editor so that it only changes what you are working on and is not making automated changes elsewhere. The reason for this is that small white space changes make diff hard to read and can cause needless conflicts. +Coding style is enforced by a pre-commit hook that runs eslint. This hook is installed whenever running `npm install` on any of the application directory. If for some reason the pre-commit hook didn't get installed, you can manually install it by running `npm install` at the root of the repository. ## Unit tests diff --git a/CliClient/app/ResourceServer.js b/CliClient/app/ResourceServer.js index c7425d039..c17d6e6c0 100644 --- a/CliClient/app/ResourceServer.js +++ b/CliClient/app/ResourceServer.js @@ -1,14 +1,11 @@ -const { _ } = require('lib/locale.js'); const { Logger } = require('lib/logger.js'); -const Resource = require('lib/models/Resource.js'); const { netUtils } = require('lib/net-utils.js'); -const http = require("http"); -const urlParser = require("url"); +const http = require('http'); +const urlParser = require('url'); const enableServerDestroy = require('server-destroy'); class ResourceServer { - constructor() { this.server_ = null; this.logger_ = new Logger(); @@ -40,7 +37,7 @@ class ResourceServer { async start() { this.port_ = await netUtils.findAvailablePort([9167, 9267, 8167, 8267]); - if (!this.port_) { + if (!this.port_) { this.logger().error('Could not find available port to start resource server. Please report the error at https://github.com/laurent22/joplin'); return; } @@ -48,11 +45,10 @@ class ResourceServer { this.server_ = http.createServer(); this.server_.on('request', async (request, response) => { - - const writeResponse = (message) => { + const writeResponse = message => { response.write(message); response.end(); - } + }; const url = urlParser.parse(request.url, true); let resourceId = url.pathname.split('/'); @@ -69,6 +65,7 @@ class ResourceServer { if (!done) throw new Error('Unhandled resource: ' + resourceId); } catch (error) { response.setHeader('Content-Type', 'text/plain'); + // eslint-disable-next-line require-atomic-updates response.statusCode = 400; response.write(error.message); } @@ -76,7 +73,7 @@ class ResourceServer { response.end(); }); - this.server_.on('error', (error) => { + this.server_.on('error', error => { this.logger().error('Resource server:', error); }); @@ -91,7 +88,6 @@ class ResourceServer { if (this.server_) this.server_.destroy(); this.server_ = null; } - } -module.exports = ResourceServer; \ No newline at end of file +module.exports = ResourceServer; diff --git a/CliClient/app/app-gui.js b/CliClient/app/app-gui.js index f11b43eaf..d1d245359 100644 --- a/CliClient/app/app-gui.js +++ b/CliClient/app/app-gui.js @@ -5,13 +5,12 @@ const Tag = require('lib/models/Tag.js'); const BaseModel = require('lib/BaseModel.js'); const Note = require('lib/models/Note.js'); const Resource = require('lib/models/Resource.js'); -const { cliUtils } = require('./cli-utils.js'); const { reducer, defaultState } = require('lib/reducer.js'); const { splitCommandString } = require('lib/string-utils.js'); const { reg } = require('lib/registry.js'); const { _ } = require('lib/locale.js'); const Entities = require('html-entities').AllHtmlEntities; -const htmlentities = (new Entities()).encode; +const htmlentities = new Entities().encode; const chalk = require('chalk'); const tk = require('terminal-kit'); @@ -20,12 +19,10 @@ const Renderer = require('tkwidgets/framework/Renderer.js'); const DecryptionWorker = require('lib/services/DecryptionWorker'); const BaseWidget = require('tkwidgets/BaseWidget.js'); -const ListWidget = require('tkwidgets/ListWidget.js'); const TextWidget = require('tkwidgets/TextWidget.js'); const HLayoutWidget = require('tkwidgets/HLayoutWidget.js'); const VLayoutWidget = require('tkwidgets/VLayoutWidget.js'); const ReduxRootWidget = require('tkwidgets/ReduxRootWidget.js'); -const RootWidget = require('tkwidgets/RootWidget.js'); const WindowWidget = require('tkwidgets/WindowWidget.js'); const NoteWidget = require('./gui/NoteWidget.js'); @@ -37,7 +34,6 @@ const StatusBarWidget = require('./gui/StatusBarWidget.js'); const ConsoleWidget = require('./gui/ConsoleWidget.js'); class AppGui { - constructor(app, store, keymap) { try { this.app_ = app; @@ -50,12 +46,12 @@ class AppGui { // Some keys are directly handled by the tkwidget framework // so they need to be remapped in a different way. this.tkWidgetKeys_ = { - 'focus_next': 'TAB', - 'focus_previous': 'SHIFT_TAB', - 'move_up': 'UP', - 'move_down': 'DOWN', - 'page_down': 'PAGE_DOWN', - 'page_up': 'PAGE_UP', + focus_next: 'TAB', + focus_previous: 'SHIFT_TAB', + move_up: 'UP', + move_down: 'DOWN', + page_down: 'PAGE_DOWN', + page_up: 'PAGE_UP', }; this.renderer_ = null; @@ -64,7 +60,7 @@ class AppGui { this.renderer_ = new Renderer(this.term(), this.rootWidget_); - this.app_.on('modelAction', async (event) => { + this.app_.on('modelAction', async event => { await this.handleModelAction(event.action); }); @@ -134,7 +130,7 @@ class AppGui { }; folderList.name = 'folderList'; folderList.vStretch = true; - folderList.on('currentItemChange', async (event) => { + folderList.on('currentItemChange', async event => { const item = folderList.currentItem; if (item === '-') { @@ -169,7 +165,7 @@ class AppGui { }); } }); - this.rootWidget_.connect(folderList, (state) => { + this.rootWidget_.connect(folderList, state => { return { selectedFolderId: state.selectedFolderId, selectedTagId: state.selectedTagId, @@ -196,7 +192,7 @@ class AppGui { id: note ? note.id : null, }); }); - this.rootWidget_.connect(noteList, (state) => { + this.rootWidget_.connect(noteList, state => { return { selectedNoteId: state.selectedNoteIds.length ? state.selectedNoteIds[0] : null, items: state.notes, @@ -210,7 +206,7 @@ class AppGui { borderBottomWidth: 1, borderLeftWidth: 1, }; - this.rootWidget_.connect(noteText, (state) => { + this.rootWidget_.connect(noteText, state => { return { noteId: state.selectedNoteIds.length ? state.selectedNoteIds[0] : null, notes: state.notes, @@ -225,7 +221,7 @@ class AppGui { borderLeftWidth: 1, borderRightWidth: 1, }; - this.rootWidget_.connect(noteMetadata, (state) => { + this.rootWidget_.connect(noteMetadata, state => { return { noteId: state.selectedNoteIds.length ? state.selectedNoteIds[0] : null }; }); noteMetadata.hide(); @@ -292,7 +288,7 @@ class AppGui { if (!cmd) return; const isConfigPassword = cmd.indexOf('config ') >= 0 && cmd.indexOf('password') >= 0; if (isConfigPassword) return; - this.stdout(chalk.cyan.bold('> ' + cmd)); + this.stdout(chalk.cyan.bold('> ' + cmd)); } setupKeymap(keymap) { @@ -408,7 +404,7 @@ class AppGui { activeListItem() { const widget = this.widget('mainWindow').focusedWidget; if (!widget) return null; - + if (widget.name == 'noteList' || widget.name == 'folderList') { return widget.currentItem; } @@ -430,18 +426,14 @@ class AppGui { } async processFunctionCommand(cmd) { - if (cmd === 'activate') { - const w = this.widget('mainWindow').focusedWidget; if (w.name === 'folderList') { this.widget('noteList').focus(); } else if (w.name === 'noteList' || w.name === 'noteText') { this.processPromptCommand('edit $n'); } - } else if (cmd === 'delete') { - if (this.widget('folderList').hasFocus) { const item = this.widget('folderList').selectedJoplinItem; @@ -462,9 +454,7 @@ class AppGui { } else { this.stdout(_('Please select the note or notebook to be deleted first.')); } - } else if (cmd === 'toggle_console') { - if (!this.consoleIsShown()) { this.showConsole(); this.minimizeConsole(); @@ -475,22 +465,15 @@ class AppGui { this.maximizeConsole(); } } - } else if (cmd === 'toggle_metadata') { - this.toggleNoteMetadata(); - } else if (cmd === 'enter_command_line_mode') { - const cmd = await this.widget('statusBar').prompt(); if (!cmd) return; this.addCommandToConsole(cmd); - await this.processPromptCommand(cmd); - + await this.processPromptCommand(cmd); } else { - throw new Error('Unknown command: ' + cmd); - } } @@ -501,7 +484,7 @@ class AppGui { // this.logger().debug('Got command: ' + cmd); - try { + try { let note = this.widget('noteList').currentItem; let folder = this.widget('folderList').currentItem; let args = splitCommandString(cmd); @@ -511,7 +494,7 @@ class AppGui { args[i] = note ? note.id : ''; } else if (args[i] == '$b') { args[i] = folder ? folder.id : ''; - } else if (args[i] == '$c') { + } else if (args[i] == '$c') { const item = this.activeListItem(); args[i] = item ? item.id : ''; } @@ -523,7 +506,7 @@ class AppGui { } this.widget('console').scrollBottom(); - + // Invalidate so that the screen is redrawn in case inputting a command has moved // the GUI up (in particular due to autocompletion), it's moved back to the right position. this.widget('root').invalidate(); @@ -603,7 +586,7 @@ class AppGui { async setupResourceServer() { const linkStyle = chalk.blue.underline; const noteTextWidget = this.widget('noteText'); - const resourceIdRegex = /^:\/[a-f0-9]+$/i + const resourceIdRegex = /^:\/[a-f0-9]+$/i; const noteLinks = {}; const hasProtocol = function(s, protocols) { @@ -613,7 +596,7 @@ class AppGui { if (s.indexOf(protocols[i] + '://') === 0) return true; } return false; - } + }; // By default, before the server is started, only the regular // URLs appear in blue. @@ -637,7 +620,7 @@ class AppGui { const link = noteLinks[path]; if (link.type === 'url') { - response.writeHead(302, { 'Location': link.url }); + response.writeHead(302, { Location: link.url }); return true; } @@ -650,11 +633,13 @@ class AppGui { if (item.mime) response.setHeader('Content-Type', item.mime); response.write(await Resource.content(item)); } else if (item.type_ === BaseModel.TYPE_NOTE) { - const html = [` + const html = [ + `
- `]; + `, + ]; html.push('' + htmlentities(item.title) + '\n\n' + htmlentities(item.body) + ''); html.push(''); response.write(html.join('')); @@ -679,7 +664,7 @@ class AppGui { noteLinks[index] = { type: 'item', id: url.substr(2), - }; + }; } else if (hasProtocol(url, ['http', 'https', 'file', 'ftp'])) { noteLinks[index] = { type: 'url', @@ -711,7 +696,6 @@ class AppGui { term.grabInput(); term.on('key', async (name, matches, data) => { - // ------------------------------------------------------------------------- // Handle special shortcuts // ------------------------------------------------------------------------- @@ -729,13 +713,13 @@ class AppGui { return; } - if (name === 'CTRL_C' ) { + if (name === 'CTRL_C') { const cmd = this.app().currentCommand(); if (!cmd || !cmd.cancellable() || this.commandCancelCalled_) { this.stdout(_('Press Ctrl+D or type "exit" to exit the application')); } else { this.commandCancelCalled_ = true; - await cmd.cancel() + await cmd.cancel(); this.commandCancelCalled_ = false; } return; @@ -744,8 +728,8 @@ class AppGui { // ------------------------------------------------------------------------- // Build up current shortcut // ------------------------------------------------------------------------- - - const now = (new Date()).getTime(); + + const now = new Date().getTime(); if (now - this.lastShortcutKeyTime_ > 800 || this.isSpecialKey(name)) { this.currentShortcutKeys_ = [name]; @@ -813,7 +797,6 @@ class AppGui { process.exit(1); }); } - } AppGui.INPUT_MODE_NORMAL = 1; diff --git a/CliClient/app/app.js b/CliClient/app/app.js index bc5134638..8883029b3 100644 --- a/CliClient/app/app.js +++ b/CliClient/app/app.js @@ -1,10 +1,5 @@ const { BaseApplication } = require('lib/BaseApplication'); -const { createStore, applyMiddleware } = require('redux'); -const { reducer, defaultState } = require('lib/reducer.js'); -const { JoplinDatabase } = require('lib/joplin-database.js'); -const { Database } = require('lib/database.js'); const { FoldersScreenUtils } = require('lib/folders-screen-utils.js'); -const { DatabaseDriverNode } = require('lib/database-driver-node.js'); const ResourceService = require('lib/services/ResourceService'); const BaseModel = require('lib/BaseModel.js'); const Folder = require('lib/models/Folder.js'); @@ -12,21 +7,15 @@ const BaseItem = require('lib/models/BaseItem.js'); const Note = require('lib/models/Note.js'); const Tag = require('lib/models/Tag.js'); const Setting = require('lib/models/Setting.js'); -const { Logger } = require('lib/logger.js'); -const { sprintf } = require('sprintf-js'); const { reg } = require('lib/registry.js'); const { fileExtension } = require('lib/path-utils.js'); -const { shim } = require('lib/shim.js'); -const { _, setLocale, defaultLocale, closestSupportedLocale } = require('lib/locale.js'); -const os = require('os'); +const { _ } = require('lib/locale.js'); const fs = require('fs-extra'); const { cliUtils } = require('./cli-utils.js'); const Cache = require('lib/Cache'); -const WelcomeUtils = require('lib/WelcomeUtils'); const RevisionService = require('lib/services/RevisionService'); class Application extends BaseApplication { - constructor() { super(); @@ -75,7 +64,7 @@ class Application extends BaseApplication { // const response = await cliUtils.promptMcq(msg, answers); // if (!response) return null; - return output[response - 1]; + // return output[response - 1]; } else { return output.length ? output[0] : null; } @@ -97,10 +86,12 @@ class Application extends BaseApplication { const parent = options.parent ? options.parent : app().currentFolder(); const ItemClass = BaseItem.itemClass(type); - if (type == BaseModel.TYPE_NOTE && pattern.indexOf('*') >= 0) { // Handle it as pattern + if (type == BaseModel.TYPE_NOTE && pattern.indexOf('*') >= 0) { + // Handle it as pattern if (!parent) throw new Error(_('No notebook selected.')); return await Note.previews(parent.id, { titlePattern: pattern }); - } else { // Single item + } else { + // Single item let item = null; if (type == BaseModel.TYPE_NOTE) { if (!parent) throw new Error(_('No notebook has been specified.')); @@ -126,15 +117,15 @@ class Application extends BaseApplication { } setupCommand(cmd) { - cmd.setStdout((text) => { + cmd.setStdout(text => { return this.stdout(text); }); - cmd.setDispatcher((action) => { + cmd.setDispatcher(action => { if (this.store()) { return this.store().dispatch(action); } else { - return (action) => {}; + return action => {}; } }); @@ -185,9 +176,9 @@ class Application extends BaseApplication { commands(uiType = null) { if (!this.allCommandsLoaded_) { - fs.readdirSync(__dirname).forEach((path) => { + fs.readdirSync(__dirname).forEach(path => { if (path.indexOf('command-') !== 0) return; - const ext = fileExtension(path) + const ext = fileExtension(path); if (ext != 'js') return; let CommandClass = require('./' + path); @@ -276,19 +267,27 @@ class Application extends BaseApplication { dummyGui() { return { - isDummy: () => { return true; }, - prompt: (initialText = '', promptString = '', options = null) => { return cliUtils.prompt(initialText, promptString, options); }, + isDummy: () => { + return true; + }, + prompt: (initialText = '', promptString = '', options = null) => { + return cliUtils.prompt(initialText, promptString, options); + }, showConsole: () => {}, maximizeConsole: () => {}, - stdout: (text) => { console.info(text); }, - fullScreen: (b=true) => {}, + stdout: text => { + console.info(text); + }, + fullScreen: (b = true) => {}, exit: () => {}, - showModalOverlay: (text) => {}, + showModalOverlay: text => {}, hideModalOverlay: () => {}, - stdoutMaxWidth: () => { return 100; }, + stdoutMaxWidth: () => { + return 100; + }, forceRender: () => {}, termSaveState: () => {}, - termRestoreState: (state) => {}, + termRestoreState: state => {}, }; } @@ -300,7 +299,7 @@ class Application extends BaseApplication { let outException = null; try { - if (this.gui().isDummy() && !this.activeCommand_.supportsUi('cli')) throw new Error(_('The command "%s" is only available in GUI mode', this.activeCommand_.name())); + if (this.gui().isDummy() && !this.activeCommand_.supportsUi('cli')) throw new Error(_('The command "%s" is only available in GUI mode', this.activeCommand_.name())); const cmdArgs = cliUtils.makeCommandArgs(this.activeCommand_, argv); await this.activeCommand_.action(cmdArgs); } catch (error) { @@ -316,24 +315,24 @@ class Application extends BaseApplication { async loadKeymaps() { const defaultKeyMap = [ - { "keys": [":"], "type": "function", "command": "enter_command_line_mode" }, - { "keys": ["TAB"], "type": "function", "command": "focus_next" }, - { "keys": ["SHIFT_TAB"], "type": "function", "command": "focus_previous" }, - { "keys": ["UP"], "type": "function", "command": "move_up" }, - { "keys": ["DOWN"], "type": "function", "command": "move_down" }, - { "keys": ["PAGE_UP"], "type": "function", "command": "page_up" }, - { "keys": ["PAGE_DOWN"], "type": "function", "command": "page_down" }, - { "keys": ["ENTER"], "type": "function", "command": "activate" }, - { "keys": ["DELETE", "BACKSPACE"], "type": "function", "command": "delete" }, - { "keys": [" "], "command": "todo toggle $n" }, - { "keys": ["tc"], "type": "function", "command": "toggle_console" }, - { "keys": ["tm"], "type": "function", "command": "toggle_metadata" }, - { "keys": ["/"], "type": "prompt", "command": "search \"\"", "cursorPosition": -2 }, - { "keys": ["mn"], "type": "prompt", "command": "mknote \"\"", "cursorPosition": -2 }, - { "keys": ["mt"], "type": "prompt", "command": "mktodo \"\"", "cursorPosition": -2 }, - { "keys": ["mb"], "type": "prompt", "command": "mkbook \"\"", "cursorPosition": -2 }, - { "keys": ["yn"], "type": "prompt", "command": "cp $n \"\"", "cursorPosition": -2 }, - { "keys": ["dn"], "type": "prompt", "command": "mv $n \"\"", "cursorPosition": -2 } + { keys: [':'], type: 'function', command: 'enter_command_line_mode' }, + { keys: ['TAB'], type: 'function', command: 'focus_next' }, + { keys: ['SHIFT_TAB'], type: 'function', command: 'focus_previous' }, + { keys: ['UP'], type: 'function', command: 'move_up' }, + { keys: ['DOWN'], type: 'function', command: 'move_down' }, + { keys: ['PAGE_UP'], type: 'function', command: 'page_up' }, + { keys: ['PAGE_DOWN'], type: 'function', command: 'page_down' }, + { keys: ['ENTER'], type: 'function', command: 'activate' }, + { keys: ['DELETE', 'BACKSPACE'], type: 'function', command: 'delete' }, + { keys: [' '], command: 'todo toggle $n' }, + { keys: ['tc'], type: 'function', command: 'toggle_console' }, + { keys: ['tm'], type: 'function', command: 'toggle_metadata' }, + { keys: ['/'], type: 'prompt', command: 'search ""', cursorPosition: -2 }, + { keys: ['mn'], type: 'prompt', command: 'mknote ""', cursorPosition: -2 }, + { keys: ['mt'], type: 'prompt', command: 'mktodo ""', cursorPosition: -2 }, + { keys: ['mb'], type: 'prompt', command: 'mkbook ""', cursorPosition: -2 }, + { keys: ['yn'], type: 'prompt', command: 'cp $n ""', cursorPosition: -2 }, + { keys: ['dn'], type: 'prompt', command: 'mv $n ""', cursorPosition: -2 }, ]; // Filter the keymap item by command so that items in keymap.json can override @@ -341,7 +340,7 @@ class Application extends BaseApplication { const itemsByCommand = {}; for (let i = 0; i < defaultKeyMap.length; i++) { - itemsByCommand[defaultKeyMap[i].command] = defaultKeyMap[i] + itemsByCommand[defaultKeyMap[i].command] = defaultKeyMap[i]; } const filePath = Setting.value('profileDir') + '/keymap.json'; @@ -374,7 +373,7 @@ class Application extends BaseApplication { async start(argv) { argv = await super.start(argv); - cliUtils.setStdout((object) => { + cliUtils.setStdout(object => { return this.stdout(object); }); @@ -401,7 +400,8 @@ class Application extends BaseApplication { // Need to call exit() explicitely, otherwise Node wait for any timeout to complete // https://stackoverflow.com/questions/18050095 process.exit(0); - } else { // Otherwise open the GUI + } else { + // Otherwise open the GUI this.initRedux(); const keymap = await this.loadKeymaps(); @@ -421,7 +421,7 @@ class Application extends BaseApplication { const tags = await Tag.allWithNotes(); ResourceService.runInBackground(); - + RevisionService.instance().runInBackground(); this.dispatch({ @@ -435,7 +435,6 @@ class Application extends BaseApplication { }); } } - } let application_ = null; @@ -446,4 +445,4 @@ function app() { return application_; } -module.exports = { app }; \ No newline at end of file +module.exports = { app }; diff --git a/CliClient/app/autocompletion.js b/CliClient/app/autocompletion.js index 20464127e..99edb885b 100644 --- a/CliClient/app/autocompletion.js +++ b/CliClient/app/autocompletion.js @@ -14,11 +14,11 @@ async function handleAutocompletionPromise(line) { //should look for commmands it could be if (words.length == 1) { if (names.indexOf(words[0]) === -1) { - let x = names.filter((n) => n.indexOf(words[0]) === 0); + let x = names.filter(n => n.indexOf(words[0]) === 0); if (x.length === 1) { return x[0] + ' '; } - return x.length > 0 ? x.map((a) => a + ' ') : line; + return x.length > 0 ? x.map(a => a + ' ') : line; } else { return line; } @@ -34,9 +34,9 @@ async function handleAutocompletionPromise(line) { let next = words.length > 1 ? words[words.length - 1] : ''; let l = []; if (next[0] === '-') { - for (let i = 0; i
.
* Whitespace between
elements are ignored. For example:
*
abc
block.
- var replaced = false;
+ // Whether 2 or more
elements have been found and replaced with a
+ //
block.
+ var replaced = false;
- // If we find a
chain, remove the
s until we hit another element
- // or non-whitespace. This leaves behind the first
in the chain
- // (which will be replaced with a
later).
- while ((next = this._nextElement(next)) && (next.tagName == "BR")) {
- replaced = true;
- var brSibling = next.nextSibling;
- next.parentNode.removeChild(next);
- next = brSibling;
- }
+ // If we find a
chain, remove the
s until we hit another element
+ // or non-whitespace. This leaves behind the first
in the chain
+ // (which will be replaced with a
later).
+ while ((next = this._nextElement(next)) && (next.tagName == 'BR')) {
+ replaced = true;
+ var brSibling = next.nextSibling;
+ next.parentNode.removeChild(next);
+ next = brSibling;
+ }
- // If we removed a
chain, replace the remaining
with a
. Add - // all sibling nodes as children of the
until we hit another
- // chain.
- if (replaced) {
- var p = this._doc.createElement("p");
- br.parentNode.replaceChild(p, br);
+ // If we removed a
chain, replace the remaining
with a
. Add + // all sibling nodes as children of the
until we hit another
+ // chain.
+ if (replaced) {
+ var p = this._doc.createElement('p');
+ br.parentNode.replaceChild(p, br);
- next = p.nextSibling;
- while (next) {
- // If we've hit another
, we're done adding children to this
.
- if (next.tagName == "BR") {
- var nextElem = this._nextElement(next.nextSibling);
- if (nextElem && nextElem.tagName == "BR")
- break;
- }
+ next = p.nextSibling;
+ while (next) {
+ // If we've hit another
, we're done adding children to this
. + if (next.tagName == 'BR') { + var nextElem = this._nextElement(next.nextSibling); + if (nextElem && nextElem.tagName == 'BR') + break; + } - if (!this._isPhrasingContent(next)) - break; + if (!this._isPhrasingContent(next)) + break; - // Otherwise, make this node a child of the new
. - var sibling = next.nextSibling; - p.appendChild(next); - next = sibling; - } + // Otherwise, make this node a child of the new
. + var sibling = next.nextSibling; + p.appendChild(next); + next = sibling; + } - while (p.lastChild && this._isWhitespace(p.lastChild)) { - p.removeChild(p.lastChild); - } + while (p.lastChild && this._isWhitespace(p.lastChild)) { + p.removeChild(p.lastChild); + } - if (p.parentNode.tagName === "P") - this._setNodeTag(p.parentNode, "DIV"); - } - }); - }, + if (p.parentNode.tagName === 'P') + this._setNodeTag(p.parentNode, 'DIV'); + } + }); + }, - _setNodeTag: function (node, tag) { - this.log("_setNodeTag", node, tag); - if (node.__JSDOMParser__) { - node.localName = tag.toLowerCase(); - node.tagName = tag.toUpperCase(); - return node; - } + _setNodeTag: function (node, tag) { + this.log('_setNodeTag', node, tag); + if (node.__JSDOMParser__) { + node.localName = tag.toLowerCase(); + node.tagName = tag.toUpperCase(); + return node; + } - var replacement = node.ownerDocument.createElement(tag); - while (node.firstChild) { - replacement.appendChild(node.firstChild); - } - node.parentNode.replaceChild(replacement, node); - if (node.readability) - replacement.readability = node.readability; + var replacement = node.ownerDocument.createElement(tag); + while (node.firstChild) { + replacement.appendChild(node.firstChild); + } + node.parentNode.replaceChild(replacement, node); + if (node.readability) + replacement.readability = node.readability; - for (var i = 0; i < node.attributes.length; i++) { - try { - replacement.setAttribute(node.attributes[i].name, node.attributes[i].value); - } catch (ex) { - /* it's possible for setAttribute() to throw if the attribute name + for (var i = 0; i < node.attributes.length; i++) { + try { + replacement.setAttribute(node.attributes[i].name, node.attributes[i].value); + } catch (ex) { + /* it's possible for setAttribute() to throw if the attribute name * isn't a valid XML Name. Such attributes can however be parsed from * source in HTML docs, see https://github.com/whatwg/html/issues/4275, * so we can hit them here and then throw. We don't care about such * attributes so we ignore them. */ - } - } - return replacement; - }, + } + } + return replacement; + }, - /** + /** * Prepare the article node for display. Clean out any inline styles, * iframes, forms, strip extraneous
tags, etc. * * @param Element * @return void **/ - _prepArticle: function(articleContent) { - this._cleanStyles(articleContent); + _prepArticle: function(articleContent) { + this._cleanStyles(articleContent); - // Check for data tables before we continue, to avoid removing items in - // those tables, which will often be isolated even though they're - // visually linked to other content-ful elements (text, images, etc.). - this._markDataTables(articleContent); + // Check for data tables before we continue, to avoid removing items in + // those tables, which will often be isolated even though they're + // visually linked to other content-ful elements (text, images, etc.). + this._markDataTables(articleContent); - this._fixLazyImages(articleContent); + this._fixLazyImages(articleContent); - // Clean out junk from the article content - this._cleanConditionally(articleContent, "form"); - this._cleanConditionally(articleContent, "fieldset"); - this._clean(articleContent, "object"); - this._clean(articleContent, "embed"); - this._clean(articleContent, "h1"); - this._clean(articleContent, "footer"); - this._clean(articleContent, "link"); - this._clean(articleContent, "aside"); + // Clean out junk from the article content + this._cleanConditionally(articleContent, 'form'); + this._cleanConditionally(articleContent, 'fieldset'); + this._clean(articleContent, 'object'); + this._clean(articleContent, 'embed'); + this._clean(articleContent, 'h1'); + this._clean(articleContent, 'footer'); + this._clean(articleContent, 'link'); + this._clean(articleContent, 'aside'); - // Clean out elements with little content that have "share" in their id/class combinations from final top candidates, - // which means we don't remove the top candidates even they have "share". + // Clean out elements with little content that have "share" in their id/class combinations from final top candidates, + // which means we don't remove the top candidates even they have "share". - var shareElementThreshold = this.DEFAULT_CHAR_THRESHOLD; + var shareElementThreshold = this.DEFAULT_CHAR_THRESHOLD; - this._forEachNode(articleContent.children, function (topCandidate) { - this._cleanMatchedNodes(topCandidate, function (node, matchString) { - return /share/.test(matchString) && node.textContent.length < shareElementThreshold; - }); - }); + this._forEachNode(articleContent.children, function (topCandidate) { + this._cleanMatchedNodes(topCandidate, function (node, matchString) { + return /share/.test(matchString) && node.textContent.length < shareElementThreshold; + }); + }); - // If there is only one h2 and its text content substantially equals article title, - // they are probably using it as a header and not a subheader, - // so remove it since we already extract the title separately. - var h2 = articleContent.getElementsByTagName("h2"); - if (h2.length === 1) { - var lengthSimilarRate = (h2[0].textContent.length - this._articleTitle.length) / this._articleTitle.length; - if (Math.abs(lengthSimilarRate) < 0.5) { - var titlesMatch = false; - if (lengthSimilarRate > 0) { - titlesMatch = h2[0].textContent.includes(this._articleTitle); - } else { - titlesMatch = this._articleTitle.includes(h2[0].textContent); - } - if (titlesMatch) { - this._clean(articleContent, "h2"); - } - } - } + // If there is only one h2 and its text content substantially equals article title, + // they are probably using it as a header and not a subheader, + // so remove it since we already extract the title separately. + var h2 = articleContent.getElementsByTagName('h2'); + if (h2.length === 1) { + var lengthSimilarRate = (h2[0].textContent.length - this._articleTitle.length) / this._articleTitle.length; + if (Math.abs(lengthSimilarRate) < 0.5) { + var titlesMatch = false; + if (lengthSimilarRate > 0) { + titlesMatch = h2[0].textContent.includes(this._articleTitle); + } else { + titlesMatch = this._articleTitle.includes(h2[0].textContent); + } + if (titlesMatch) { + this._clean(articleContent, 'h2'); + } + } + } - this._clean(articleContent, "iframe"); - this._clean(articleContent, "input"); - this._clean(articleContent, "textarea"); - this._clean(articleContent, "select"); - this._clean(articleContent, "button"); - this._cleanHeaders(articleContent); + this._clean(articleContent, 'iframe'); + this._clean(articleContent, 'input'); + this._clean(articleContent, 'textarea'); + this._clean(articleContent, 'select'); + this._clean(articleContent, 'button'); + this._cleanHeaders(articleContent); - // Do these last as the previous stuff may have removed junk - // that will affect these - this._cleanConditionally(articleContent, "table"); - this._cleanConditionally(articleContent, "ul"); - this._cleanConditionally(articleContent, "div"); + // Do these last as the previous stuff may have removed junk + // that will affect these + this._cleanConditionally(articleContent, 'table'); + this._cleanConditionally(articleContent, 'ul'); + this._cleanConditionally(articleContent, 'div'); - // Remove extra paragraphs - this._removeNodes(articleContent.getElementsByTagName("p"), function (paragraph) { - var imgCount = paragraph.getElementsByTagName("img").length; - var embedCount = paragraph.getElementsByTagName("embed").length; - var objectCount = paragraph.getElementsByTagName("object").length; - // At this point, nasty iframes have been removed, only remain embedded video ones. - var iframeCount = paragraph.getElementsByTagName("iframe").length; - var totalCount = imgCount + embedCount + objectCount + iframeCount; + // Remove extra paragraphs + this._removeNodes(articleContent.getElementsByTagName('p'), function (paragraph) { + var imgCount = paragraph.getElementsByTagName('img').length; + var embedCount = paragraph.getElementsByTagName('embed').length; + var objectCount = paragraph.getElementsByTagName('object').length; + // At this point, nasty iframes have been removed, only remain embedded video ones. + var iframeCount = paragraph.getElementsByTagName('iframe').length; + var totalCount = imgCount + embedCount + objectCount + iframeCount; - return totalCount === 0 && !this._getInnerText(paragraph, false); - }); + return totalCount === 0 && !this._getInnerText(paragraph, false); + }); - this._forEachNode(this._getAllNodesWithTag(articleContent, ["br"]), function(br) { - var next = this._nextElement(br.nextSibling); - if (next && next.tagName == "P") - br.parentNode.removeChild(br); - }); + this._forEachNode(this._getAllNodesWithTag(articleContent, ['br']), function(br) { + var next = this._nextElement(br.nextSibling); + if (next && next.tagName == 'P') + br.parentNode.removeChild(br); + }); - // Remove single-cell tables - this._forEachNode(this._getAllNodesWithTag(articleContent, ["table"]), function(table) { - var tbody = this._hasSingleTagInsideElement(table, "TBODY") ? table.firstElementChild : table; - if (this._hasSingleTagInsideElement(tbody, "TR")) { - var row = tbody.firstElementChild; - if (this._hasSingleTagInsideElement(row, "TD")) { - var cell = row.firstElementChild; - cell = this._setNodeTag(cell, this._everyNode(cell.childNodes, this._isPhrasingContent) ? "P" : "DIV"); - table.parentNode.replaceChild(cell, table); - } - } - }); - }, + // Remove single-cell tables + this._forEachNode(this._getAllNodesWithTag(articleContent, ['table']), function(table) { + var tbody = this._hasSingleTagInsideElement(table, 'TBODY') ? table.firstElementChild : table; + if (this._hasSingleTagInsideElement(tbody, 'TR')) { + var row = tbody.firstElementChild; + if (this._hasSingleTagInsideElement(row, 'TD')) { + var cell = row.firstElementChild; + cell = this._setNodeTag(cell, this._everyNode(cell.childNodes, this._isPhrasingContent) ? 'P' : 'DIV'); + table.parentNode.replaceChild(cell, table); + } + } + }); + }, - /** + /** * Initialize a node with the readability object. Also checks the * className/id for special names to add to its score. * * @param Element * @return void **/ - _initializeNode: function(node) { - node.readability = {"contentScore": 0}; + _initializeNode: function(node) { + node.readability = {'contentScore': 0}; - switch (node.tagName) { - case "DIV": - node.readability.contentScore += 5; - break; + switch (node.tagName) { + case 'DIV': + node.readability.contentScore += 5; + break; - case "PRE": - case "TD": - case "BLOCKQUOTE": - node.readability.contentScore += 3; - break; + case 'PRE': + case 'TD': + case 'BLOCKQUOTE': + node.readability.contentScore += 3; + break; - case "ADDRESS": - case "OL": - case "UL": - case "DL": - case "DD": - case "DT": - case "LI": - case "FORM": - node.readability.contentScore -= 3; - break; + case 'ADDRESS': + case 'OL': + case 'UL': + case 'DL': + case 'DD': + case 'DT': + case 'LI': + case 'FORM': + node.readability.contentScore -= 3; + break; - case "H1": - case "H2": - case "H3": - case "H4": - case "H5": - case "H6": - case "TH": - node.readability.contentScore -= 5; - break; - } + case 'H1': + case 'H2': + case 'H3': + case 'H4': + case 'H5': + case 'H6': + case 'TH': + node.readability.contentScore -= 5; + break; + } - node.readability.contentScore += this._getClassWeight(node); - }, + node.readability.contentScore += this._getClassWeight(node); + }, - _removeAndGetNext: function(node) { - var nextNode = this._getNextNode(node, true); - node.parentNode.removeChild(node); - return nextNode; - }, + _removeAndGetNext: function(node) { + var nextNode = this._getNextNode(node, true); + node.parentNode.removeChild(node); + return nextNode; + }, - /** + /** * Traverse the DOM from node to node, starting at the node passed in. * Pass true for the second parameter to indicate this node itself * (and its kids) are going away, and we want the next node over. * * Calling this in a loop will traverse the DOM depth-first. */ - _getNextNode: function(node, ignoreSelfAndKids) { - // First check for kids if those aren't being ignored - if (!ignoreSelfAndKids && node.firstElementChild) { - return node.firstElementChild; - } - // Then for siblings... - if (node.nextElementSibling) { - return node.nextElementSibling; - } - // And finally, move up the parent chain *and* find a sibling - // (because this is depth-first traversal, we will have already - // seen the parent nodes themselves). - do { - node = node.parentNode; - } while (node && !node.nextElementSibling); - return node && node.nextElementSibling; - }, + _getNextNode: function(node, ignoreSelfAndKids) { + // First check for kids if those aren't being ignored + if (!ignoreSelfAndKids && node.firstElementChild) { + return node.firstElementChild; + } + // Then for siblings... + if (node.nextElementSibling) { + return node.nextElementSibling; + } + // And finally, move up the parent chain *and* find a sibling + // (because this is depth-first traversal, we will have already + // seen the parent nodes themselves). + do { + node = node.parentNode; + } while (node && !node.nextElementSibling); + return node && node.nextElementSibling; + }, - _checkByline: function(node, matchString) { - if (this._articleByline) { - return false; - } + _checkByline: function(node, matchString) { + if (this._articleByline) { + return false; + } - if (node.getAttribute !== undefined) { - var rel = node.getAttribute("rel"); - var itemprop = node.getAttribute("itemprop"); - } + if (node.getAttribute !== undefined) { + var rel = node.getAttribute('rel'); + var itemprop = node.getAttribute('itemprop'); + } - if ((rel === "author" || (itemprop && itemprop.indexOf("author") !== -1) || this.REGEXPS.byline.test(matchString)) && this._isValidByline(node.textContent)) { - this._articleByline = node.textContent.trim(); - return true; - } + if ((rel === 'author' || (itemprop && itemprop.indexOf('author') !== -1) || this.REGEXPS.byline.test(matchString)) && this._isValidByline(node.textContent)) { + this._articleByline = node.textContent.trim(); + return true; + } - return false; - }, + return false; + }, - _getNodeAncestors: function(node, maxDepth) { - maxDepth = maxDepth || 0; - var i = 0, ancestors = []; - while (node.parentNode) { - ancestors.push(node.parentNode); - if (maxDepth && ++i === maxDepth) - break; - node = node.parentNode; - } - return ancestors; - }, + _getNodeAncestors: function(node, maxDepth) { + maxDepth = maxDepth || 0; + var i = 0, ancestors = []; + while (node.parentNode) { + ancestors.push(node.parentNode); + if (maxDepth && ++i === maxDepth) + break; + node = node.parentNode; + } + return ancestors; + }, - /*** + /*** * grabArticle - Using a variety of metrics (content score, classname, element types), find the content that is * most likely to be the stuff a user wants to read. Then return it wrapped up in a div. * * @param page a document to run upon. Needs to be a full document, complete with body. * @return Element **/ - _grabArticle: function (page) { - this.log("**** grabArticle ****"); - var doc = this._doc; - var isPaging = (page !== null ? true: false); - page = page ? page : this._doc.body; + _grabArticle: function (page) { + this.log('**** grabArticle ****'); + var doc = this._doc; + var isPaging = (page !== null ? true: false); + page = page ? page : this._doc.body; - // We can't grab an article if we don't have a page! - if (!page) { - this.log("No body found in document. Abort."); - return null; - } + // We can't grab an article if we don't have a page! + if (!page) { + this.log('No body found in document. Abort.'); + return null; + } - var pageCacheHtml = page.innerHTML; + var pageCacheHtml = page.innerHTML; - while (true) { - var stripUnlikelyCandidates = this._flagIsActive(this.FLAG_STRIP_UNLIKELYS); + while (true) { + var stripUnlikelyCandidates = this._flagIsActive(this.FLAG_STRIP_UNLIKELYS); - // First, node prepping. Trash nodes that look cruddy (like ones with the - // class name "comment", etc), and turn divs into P tags where they have been - // used inappropriately (as in, where they contain no other block level elements.) - var elementsToScore = []; - var node = this._doc.documentElement; + // First, node prepping. Trash nodes that look cruddy (like ones with the + // class name "comment", etc), and turn divs into P tags where they have been + // used inappropriately (as in, where they contain no other block level elements.) + var elementsToScore = []; + var node = this._doc.documentElement; - while (node) { - var matchString = node.className + " " + node.id; + while (node) { + var matchString = node.className + ' ' + node.id; - if (!this._isProbablyVisible(node)) { - this.log("Removing hidden node - " + matchString); - node = this._removeAndGetNext(node); - continue; - } + if (!this._isProbablyVisible(node)) { + this.log('Removing hidden node - ' + matchString); + node = this._removeAndGetNext(node); + continue; + } - // Check to see if this node is a byline, and remove it if it is. - if (this._checkByline(node, matchString)) { - node = this._removeAndGetNext(node); - continue; - } + // Check to see if this node is a byline, and remove it if it is. + if (this._checkByline(node, matchString)) { + node = this._removeAndGetNext(node); + continue; + } - // Remove unlikely candidates - if (stripUnlikelyCandidates) { - if (this.REGEXPS.unlikelyCandidates.test(matchString) && + // Remove unlikely candidates + if (stripUnlikelyCandidates) { + if (this.REGEXPS.unlikelyCandidates.test(matchString) && !this.REGEXPS.okMaybeItsACandidate.test(matchString) && - !this._hasAncestorTag(node, "table") && - node.tagName !== "BODY" && - node.tagName !== "A") { - this.log("Removing unlikely candidate - " + matchString); - node = this._removeAndGetNext(node); - continue; - } - } + !this._hasAncestorTag(node, 'table') && + node.tagName !== 'BODY' && + node.tagName !== 'A') { + this.log('Removing unlikely candidate - ' + matchString); + node = this._removeAndGetNext(node); + continue; + } + } - // Remove DIV, SECTION, and HEADER nodes without any content(e.g. text, image, video, or iframe). - if ((node.tagName === "DIV" || node.tagName === "SECTION" || node.tagName === "HEADER" || - node.tagName === "H1" || node.tagName === "H2" || node.tagName === "H3" || - node.tagName === "H4" || node.tagName === "H5" || node.tagName === "H6") && + // Remove DIV, SECTION, and HEADER nodes without any content(e.g. text, image, video, or iframe). + if ((node.tagName === 'DIV' || node.tagName === 'SECTION' || node.tagName === 'HEADER' || + node.tagName === 'H1' || node.tagName === 'H2' || node.tagName === 'H3' || + node.tagName === 'H4' || node.tagName === 'H5' || node.tagName === 'H6') && this._isElementWithoutContent(node)) { - node = this._removeAndGetNext(node); - continue; - } + node = this._removeAndGetNext(node); + continue; + } - if (this.DEFAULT_TAGS_TO_SCORE.indexOf(node.tagName) !== -1) { - elementsToScore.push(node); - } + if (this.DEFAULT_TAGS_TO_SCORE.indexOf(node.tagName) !== -1) { + elementsToScore.push(node); + } - // Turn all divs that don't have children block level elements into p's - if (node.tagName === "DIV") { - // Put phrasing content into paragraphs. - var p = null; - var childNode = node.firstChild; - while (childNode) { - var nextSibling = childNode.nextSibling; - if (this._isPhrasingContent(childNode)) { - if (p !== null) { - p.appendChild(childNode); - } else if (!this._isWhitespace(childNode)) { - p = doc.createElement("p"); - node.replaceChild(p, childNode); - p.appendChild(childNode); - } - } else if (p !== null) { - while (p.lastChild && this._isWhitespace(p.lastChild)) { - p.removeChild(p.lastChild); - } - p = null; - } - childNode = nextSibling; - } + // Turn all divs that don't have children block level elements into p's + if (node.tagName === 'DIV') { + // Put phrasing content into paragraphs. + var p = null; + var childNode = node.firstChild; + while (childNode) { + var nextSibling = childNode.nextSibling; + if (this._isPhrasingContent(childNode)) { + if (p !== null) { + p.appendChild(childNode); + } else if (!this._isWhitespace(childNode)) { + p = doc.createElement('p'); + node.replaceChild(p, childNode); + p.appendChild(childNode); + } + } else if (p !== null) { + while (p.lastChild && this._isWhitespace(p.lastChild)) { + p.removeChild(p.lastChild); + } + p = null; + } + childNode = nextSibling; + } - // Sites like http://mobile.slate.com encloses each paragraph with a DIV - // element. DIVs with only a P element inside and no text content can be - // safely converted into plain P elements to avoid confusing the scoring - // algorithm with DIVs with are, in practice, paragraphs. - if (this._hasSingleTagInsideElement(node, "P") && this._getLinkDensity(node) < 0.25) { - var newNode = node.children[0]; - node.parentNode.replaceChild(newNode, node); - node = newNode; - elementsToScore.push(node); - } else if (!this._hasChildBlockElement(node)) { - node = this._setNodeTag(node, "P"); - elementsToScore.push(node); - } - } - node = this._getNextNode(node); - } + // Sites like http://mobile.slate.com encloses each paragraph with a DIV + // element. DIVs with only a P element inside and no text content can be + // safely converted into plain P elements to avoid confusing the scoring + // algorithm with DIVs with are, in practice, paragraphs. + if (this._hasSingleTagInsideElement(node, 'P') && this._getLinkDensity(node) < 0.25) { + var newNode = node.children[0]; + node.parentNode.replaceChild(newNode, node); + node = newNode; + elementsToScore.push(node); + } else if (!this._hasChildBlockElement(node)) { + node = this._setNodeTag(node, 'P'); + elementsToScore.push(node); + } + } + node = this._getNextNode(node); + } - /** + /** * Loop through all paragraphs, and assign a score to them based on how content-y they look. * Then add their score to their parent node. * * A score is determined by things like number of commas, class names, etc. Maybe eventually link density. **/ - var candidates = []; - this._forEachNode(elementsToScore, function(elementToScore) { - if (!elementToScore.parentNode || typeof(elementToScore.parentNode.tagName) === "undefined") - return; + var candidates = []; + this._forEachNode(elementsToScore, function(elementToScore) { + if (!elementToScore.parentNode || typeof(elementToScore.parentNode.tagName) === 'undefined') + return; - // If this paragraph is less than 25 characters, don't even count it. - var innerText = this._getInnerText(elementToScore); - if (innerText.length < 25) - return; + // If this paragraph is less than 25 characters, don't even count it. + var innerText = this._getInnerText(elementToScore); + if (innerText.length < 25) + return; - // Exclude nodes with no ancestor. - var ancestors = this._getNodeAncestors(elementToScore, 3); - if (ancestors.length === 0) - return; + // Exclude nodes with no ancestor. + var ancestors = this._getNodeAncestors(elementToScore, 3); + if (ancestors.length === 0) + return; - var contentScore = 0; + var contentScore = 0; - // Add a point for the paragraph itself as a base. - contentScore += 1; + // Add a point for the paragraph itself as a base. + contentScore += 1; - // Add points for any commas within this paragraph. - contentScore += innerText.split(",").length; + // Add points for any commas within this paragraph. + contentScore += innerText.split(',').length; - // For every 100 characters in this paragraph, add another point. Up to 3 points. - contentScore += Math.min(Math.floor(innerText.length / 100), 3); + // For every 100 characters in this paragraph, add another point. Up to 3 points. + contentScore += Math.min(Math.floor(innerText.length / 100), 3); - // Initialize and score ancestors. - this._forEachNode(ancestors, function(ancestor, level) { - if (!ancestor.tagName || !ancestor.parentNode || typeof(ancestor.parentNode.tagName) === "undefined") - return; + // Initialize and score ancestors. + this._forEachNode(ancestors, function(ancestor, level) { + if (!ancestor.tagName || !ancestor.parentNode || typeof(ancestor.parentNode.tagName) === 'undefined') + return; - if (typeof(ancestor.readability) === "undefined") { - this._initializeNode(ancestor); - candidates.push(ancestor); - } + if (typeof(ancestor.readability) === 'undefined') { + this._initializeNode(ancestor); + candidates.push(ancestor); + } - // Node score divider: - // - parent: 1 (no division) - // - grandparent: 2 - // - great grandparent+: ancestor level * 3 - if (level === 0) - var scoreDivider = 1; - else if (level === 1) - scoreDivider = 2; - else - scoreDivider = level * 3; - ancestor.readability.contentScore += contentScore / scoreDivider; - }); - }); + // Node score divider: + // - parent: 1 (no division) + // - grandparent: 2 + // - great grandparent+: ancestor level * 3 + if (level === 0) + var scoreDivider = 1; + else if (level === 1) + scoreDivider = 2; + else + scoreDivider = level * 3; + ancestor.readability.contentScore += contentScore / scoreDivider; + }); + }); - // After we've calculated scores, loop through all of the possible - // candidate nodes we found and find the one with the highest score. - var topCandidates = []; - for (var c = 0, cl = candidates.length; c < cl; c += 1) { - var candidate = candidates[c]; + // After we've calculated scores, loop through all of the possible + // candidate nodes we found and find the one with the highest score. + var topCandidates = []; + for (var c = 0, cl = candidates.length; c < cl; c += 1) { + var candidate = candidates[c]; - // Scale the final candidates score based on link density. Good content - // should have a relatively small link density (5% or less) and be mostly - // unaffected by this operation. - var candidateScore = candidate.readability.contentScore * (1 - this._getLinkDensity(candidate)); - candidate.readability.contentScore = candidateScore; + // Scale the final candidates score based on link density. Good content + // should have a relatively small link density (5% or less) and be mostly + // unaffected by this operation. + var candidateScore = candidate.readability.contentScore * (1 - this._getLinkDensity(candidate)); + candidate.readability.contentScore = candidateScore; - this.log("Candidate:", candidate, "with score " + candidateScore); + this.log('Candidate:', candidate, 'with score ' + candidateScore); - for (var t = 0; t < this._nbTopCandidates; t++) { - var aTopCandidate = topCandidates[t]; + for (var t = 0; t < this._nbTopCandidates; t++) { + var aTopCandidate = topCandidates[t]; - if (!aTopCandidate || candidateScore > aTopCandidate.readability.contentScore) { - topCandidates.splice(t, 0, candidate); - if (topCandidates.length > this._nbTopCandidates) - topCandidates.pop(); - break; - } - } - } + if (!aTopCandidate || candidateScore > aTopCandidate.readability.contentScore) { + topCandidates.splice(t, 0, candidate); + if (topCandidates.length > this._nbTopCandidates) + topCandidates.pop(); + break; + } + } + } - var topCandidate = topCandidates[0] || null; - var neededToCreateTopCandidate = false; - var parentOfTopCandidate; + var topCandidate = topCandidates[0] || null; + var neededToCreateTopCandidate = false; + var parentOfTopCandidate; - // If we still have no top candidate, just use the body as a last resort. - // We also have to copy the body node so it is something we can modify. - if (topCandidate === null || topCandidate.tagName === "BODY") { - // Move all of the page's children into topCandidate - topCandidate = doc.createElement("DIV"); - neededToCreateTopCandidate = true; - // Move everything (not just elements, also text nodes etc.) into the container - // so we even include text directly in the body: - var kids = page.childNodes; - while (kids.length) { - this.log("Moving child out:", kids[0]); - topCandidate.appendChild(kids[0]); - } + // If we still have no top candidate, just use the body as a last resort. + // We also have to copy the body node so it is something we can modify. + if (topCandidate === null || topCandidate.tagName === 'BODY') { + // Move all of the page's children into topCandidate + topCandidate = doc.createElement('DIV'); + neededToCreateTopCandidate = true; + // Move everything (not just elements, also text nodes etc.) into the container + // so we even include text directly in the body: + var kids = page.childNodes; + while (kids.length) { + this.log('Moving child out:', kids[0]); + topCandidate.appendChild(kids[0]); + } - page.appendChild(topCandidate); + page.appendChild(topCandidate); - this._initializeNode(topCandidate); - } else if (topCandidate) { - // Find a better top candidate node if it contains (at least three) nodes which belong to `topCandidates` array - // and whose scores are quite closed with current `topCandidate` node. - var alternativeCandidateAncestors = []; - for (var i = 1; i < topCandidates.length; i++) { - if (topCandidates[i].readability.contentScore / topCandidate.readability.contentScore >= 0.75) { - alternativeCandidateAncestors.push(this._getNodeAncestors(topCandidates[i])); - } - } - var MINIMUM_TOPCANDIDATES = 3; - if (alternativeCandidateAncestors.length >= MINIMUM_TOPCANDIDATES) { - parentOfTopCandidate = topCandidate.parentNode; - while (parentOfTopCandidate.tagName !== "BODY") { - var listsContainingThisAncestor = 0; - for (var ancestorIndex = 0; ancestorIndex < alternativeCandidateAncestors.length && listsContainingThisAncestor < MINIMUM_TOPCANDIDATES; ancestorIndex++) { - listsContainingThisAncestor += Number(alternativeCandidateAncestors[ancestorIndex].includes(parentOfTopCandidate)); - } - if (listsContainingThisAncestor >= MINIMUM_TOPCANDIDATES) { - topCandidate = parentOfTopCandidate; - break; - } - parentOfTopCandidate = parentOfTopCandidate.parentNode; - } - } - if (!topCandidate.readability) { - this._initializeNode(topCandidate); - } + this._initializeNode(topCandidate); + } else if (topCandidate) { + // Find a better top candidate node if it contains (at least three) nodes which belong to `topCandidates` array + // and whose scores are quite closed with current `topCandidate` node. + var alternativeCandidateAncestors = []; + for (var i = 1; i < topCandidates.length; i++) { + if (topCandidates[i].readability.contentScore / topCandidate.readability.contentScore >= 0.75) { + alternativeCandidateAncestors.push(this._getNodeAncestors(topCandidates[i])); + } + } + var MINIMUM_TOPCANDIDATES = 3; + if (alternativeCandidateAncestors.length >= MINIMUM_TOPCANDIDATES) { + parentOfTopCandidate = topCandidate.parentNode; + while (parentOfTopCandidate.tagName !== 'BODY') { + var listsContainingThisAncestor = 0; + for (var ancestorIndex = 0; ancestorIndex < alternativeCandidateAncestors.length && listsContainingThisAncestor < MINIMUM_TOPCANDIDATES; ancestorIndex++) { + listsContainingThisAncestor += Number(alternativeCandidateAncestors[ancestorIndex].includes(parentOfTopCandidate)); + } + if (listsContainingThisAncestor >= MINIMUM_TOPCANDIDATES) { + topCandidate = parentOfTopCandidate; + break; + } + parentOfTopCandidate = parentOfTopCandidate.parentNode; + } + } + if (!topCandidate.readability) { + this._initializeNode(topCandidate); + } - // Because of our bonus system, parents of candidates might have scores - // themselves. They get half of the node. There won't be nodes with higher - // scores than our topCandidate, but if we see the score going *up* in the first - // few steps up the tree, that's a decent sign that there might be more content - // lurking in other places that we want to unify in. The sibling stuff - // below does some of that - but only if we've looked high enough up the DOM - // tree. - parentOfTopCandidate = topCandidate.parentNode; - var lastScore = topCandidate.readability.contentScore; - // The scores shouldn't get too low. - var scoreThreshold = lastScore / 3; - while (parentOfTopCandidate.tagName !== "BODY") { - if (!parentOfTopCandidate.readability) { - parentOfTopCandidate = parentOfTopCandidate.parentNode; - continue; - } - var parentScore = parentOfTopCandidate.readability.contentScore; - if (parentScore < scoreThreshold) - break; - if (parentScore > lastScore) { - // Alright! We found a better parent to use. - topCandidate = parentOfTopCandidate; - break; - } - lastScore = parentOfTopCandidate.readability.contentScore; - parentOfTopCandidate = parentOfTopCandidate.parentNode; - } + // Because of our bonus system, parents of candidates might have scores + // themselves. They get half of the node. There won't be nodes with higher + // scores than our topCandidate, but if we see the score going *up* in the first + // few steps up the tree, that's a decent sign that there might be more content + // lurking in other places that we want to unify in. The sibling stuff + // below does some of that - but only if we've looked high enough up the DOM + // tree. + parentOfTopCandidate = topCandidate.parentNode; + var lastScore = topCandidate.readability.contentScore; + // The scores shouldn't get too low. + var scoreThreshold = lastScore / 3; + while (parentOfTopCandidate.tagName !== 'BODY') { + if (!parentOfTopCandidate.readability) { + parentOfTopCandidate = parentOfTopCandidate.parentNode; + continue; + } + var parentScore = parentOfTopCandidate.readability.contentScore; + if (parentScore < scoreThreshold) + break; + if (parentScore > lastScore) { + // Alright! We found a better parent to use. + topCandidate = parentOfTopCandidate; + break; + } + lastScore = parentOfTopCandidate.readability.contentScore; + parentOfTopCandidate = parentOfTopCandidate.parentNode; + } - // If the top candidate is the only child, use parent instead. This will help sibling - // joining logic when adjacent content is actually located in parent's sibling node. - parentOfTopCandidate = topCandidate.parentNode; - while (parentOfTopCandidate.tagName != "BODY" && parentOfTopCandidate.children.length == 1) { - topCandidate = parentOfTopCandidate; - parentOfTopCandidate = topCandidate.parentNode; - } - if (!topCandidate.readability) { - this._initializeNode(topCandidate); - } - } + // If the top candidate is the only child, use parent instead. This will help sibling + // joining logic when adjacent content is actually located in parent's sibling node. + parentOfTopCandidate = topCandidate.parentNode; + while (parentOfTopCandidate.tagName != 'BODY' && parentOfTopCandidate.children.length == 1) { + topCandidate = parentOfTopCandidate; + parentOfTopCandidate = topCandidate.parentNode; + } + if (!topCandidate.readability) { + this._initializeNode(topCandidate); + } + } - // Now that we have the top candidate, look through its siblings for content - // that might also be related. Things like preambles, content split by ads - // that we removed, etc. - var articleContent = doc.createElement("DIV"); - if (isPaging) - articleContent.id = "readability-content"; + // Now that we have the top candidate, look through its siblings for content + // that might also be related. Things like preambles, content split by ads + // that we removed, etc. + var articleContent = doc.createElement('DIV'); + if (isPaging) + articleContent.id = 'readability-content'; - var siblingScoreThreshold = Math.max(10, topCandidate.readability.contentScore * 0.2); - // Keep potential top candidate's parent node to try to get text direction of it later. - parentOfTopCandidate = topCandidate.parentNode; - var siblings = parentOfTopCandidate.children; + var siblingScoreThreshold = Math.max(10, topCandidate.readability.contentScore * 0.2); + // Keep potential top candidate's parent node to try to get text direction of it later. + parentOfTopCandidate = topCandidate.parentNode; + var siblings = parentOfTopCandidate.children; - for (var s = 0, sl = siblings.length; s < sl; s++) { - var sibling = siblings[s]; - var append = false; + for (var s = 0, sl = siblings.length; s < sl; s++) { + var sibling = siblings[s]; + var append = false; - this.log("Looking at sibling node:", sibling, sibling.readability ? ("with score " + sibling.readability.contentScore) : ""); - this.log("Sibling has score", sibling.readability ? sibling.readability.contentScore : "Unknown"); + this.log('Looking at sibling node:', sibling, sibling.readability ? ('with score ' + sibling.readability.contentScore) : ''); + this.log('Sibling has score', sibling.readability ? sibling.readability.contentScore : 'Unknown'); - if (sibling === topCandidate) { - append = true; - } else { - var contentBonus = 0; + if (sibling === topCandidate) { + append = true; + } else { + var contentBonus = 0; - // Give a bonus if sibling nodes and top candidates have the example same classname - if (sibling.className === topCandidate.className && topCandidate.className !== "") - contentBonus += topCandidate.readability.contentScore * 0.2; + // Give a bonus if sibling nodes and top candidates have the example same classname + if (sibling.className === topCandidate.className && topCandidate.className !== '') + contentBonus += topCandidate.readability.contentScore * 0.2; - if (sibling.readability && + if (sibling.readability && ((sibling.readability.contentScore + contentBonus) >= siblingScoreThreshold)) { - append = true; - } else if (sibling.nodeName === "P") { - var linkDensity = this._getLinkDensity(sibling); - var nodeContent = this._getInnerText(sibling); - var nodeLength = nodeContent.length; + append = true; + } else if (sibling.nodeName === 'P') { + var linkDensity = this._getLinkDensity(sibling); + var nodeContent = this._getInnerText(sibling); + var nodeLength = nodeContent.length; - if (nodeLength > 80 && linkDensity < 0.25) { - append = true; - } else if (nodeLength < 80 && nodeLength > 0 && linkDensity === 0 && + if (nodeLength > 80 && linkDensity < 0.25) { + append = true; + } else if (nodeLength < 80 && nodeLength > 0 && linkDensity === 0 && nodeContent.search(/\.( |$)/) !== -1) { - append = true; - } - } - } + append = true; + } + } + } - if (append) { - this.log("Appending node:", sibling); + if (append) { + this.log('Appending node:', sibling); - if (this.ALTER_TO_DIV_EXCEPTIONS.indexOf(sibling.nodeName) === -1) { - // We have a node that isn't a common block level element, like a form or td tag. - // Turn it into a div so it doesn't get filtered out later by accident. - this.log("Altering sibling:", sibling, "to div."); + if (this.ALTER_TO_DIV_EXCEPTIONS.indexOf(sibling.nodeName) === -1) { + // We have a node that isn't a common block level element, like a form or td tag. + // Turn it into a div so it doesn't get filtered out later by accident. + this.log('Altering sibling:', sibling, 'to div.'); - sibling = this._setNodeTag(sibling, "DIV"); - } + sibling = this._setNodeTag(sibling, 'DIV'); + } - articleContent.appendChild(sibling); - // siblings is a reference to the children array, and - // sibling is removed from the array when we call appendChild(). - // As a result, we must revisit this index since the nodes - // have been shifted. - s -= 1; - sl -= 1; - } - } + articleContent.appendChild(sibling); + // siblings is a reference to the children array, and + // sibling is removed from the array when we call appendChild(). + // As a result, we must revisit this index since the nodes + // have been shifted. + s -= 1; + sl -= 1; + } + } - if (this._debug) - this.log("Article content pre-prep: " + articleContent.innerHTML); - // So we have all of the content that we need. Now we clean it up for presentation. - this._prepArticle(articleContent); - if (this._debug) - this.log("Article content post-prep: " + articleContent.innerHTML); + if (this._debug) + this.log('Article content pre-prep: ' + articleContent.innerHTML); + // So we have all of the content that we need. Now we clean it up for presentation. + this._prepArticle(articleContent); + if (this._debug) + this.log('Article content post-prep: ' + articleContent.innerHTML); - if (neededToCreateTopCandidate) { - // We already created a fake div thing, and there wouldn't have been any siblings left - // for the previous loop, so there's no point trying to create a new div, and then - // move all the children over. Just assign IDs and class names here. No need to append - // because that already happened anyway. - topCandidate.id = "readability-page-1"; - topCandidate.className = "page"; - } else { - var div = doc.createElement("DIV"); - div.id = "readability-page-1"; - div.className = "page"; - var children = articleContent.childNodes; - while (children.length) { - div.appendChild(children[0]); - } - articleContent.appendChild(div); - } + if (neededToCreateTopCandidate) { + // We already created a fake div thing, and there wouldn't have been any siblings left + // for the previous loop, so there's no point trying to create a new div, and then + // move all the children over. Just assign IDs and class names here. No need to append + // because that already happened anyway. + topCandidate.id = 'readability-page-1'; + topCandidate.className = 'page'; + } else { + var div = doc.createElement('DIV'); + div.id = 'readability-page-1'; + div.className = 'page'; + var children = articleContent.childNodes; + while (children.length) { + div.appendChild(children[0]); + } + articleContent.appendChild(div); + } - if (this._debug) - this.log("Article content after paging: " + articleContent.innerHTML); + if (this._debug) + this.log('Article content after paging: ' + articleContent.innerHTML); - var parseSuccessful = true; + var parseSuccessful = true; - // Now that we've gone through the full algorithm, check to see if - // we got any meaningful content. If we didn't, we may need to re-run - // grabArticle with different flags set. This gives us a higher likelihood of - // finding the content, and the sieve approach gives us a higher likelihood of - // finding the -right- content. - var textLength = this._getInnerText(articleContent, true).length; - if (textLength < this._charThreshold) { - parseSuccessful = false; - page.innerHTML = pageCacheHtml; + // Now that we've gone through the full algorithm, check to see if + // we got any meaningful content. If we didn't, we may need to re-run + // grabArticle with different flags set. This gives us a higher likelihood of + // finding the content, and the sieve approach gives us a higher likelihood of + // finding the -right- content. + var textLength = this._getInnerText(articleContent, true).length; + if (textLength < this._charThreshold) { + parseSuccessful = false; + page.innerHTML = pageCacheHtml; - if (this._flagIsActive(this.FLAG_STRIP_UNLIKELYS)) { - this._removeFlag(this.FLAG_STRIP_UNLIKELYS); - this._attempts.push({articleContent: articleContent, textLength: textLength}); - } else if (this._flagIsActive(this.FLAG_WEIGHT_CLASSES)) { - this._removeFlag(this.FLAG_WEIGHT_CLASSES); - this._attempts.push({articleContent: articleContent, textLength: textLength}); - } else if (this._flagIsActive(this.FLAG_CLEAN_CONDITIONALLY)) { - this._removeFlag(this.FLAG_CLEAN_CONDITIONALLY); - this._attempts.push({articleContent: articleContent, textLength: textLength}); - } else { - this._attempts.push({articleContent: articleContent, textLength: textLength}); - // No luck after removing flags, just return the longest text we found during the different loops - this._attempts.sort(function (a, b) { - return b.textLength - a.textLength; - }); + if (this._flagIsActive(this.FLAG_STRIP_UNLIKELYS)) { + this._removeFlag(this.FLAG_STRIP_UNLIKELYS); + this._attempts.push({articleContent: articleContent, textLength: textLength}); + } else if (this._flagIsActive(this.FLAG_WEIGHT_CLASSES)) { + this._removeFlag(this.FLAG_WEIGHT_CLASSES); + this._attempts.push({articleContent: articleContent, textLength: textLength}); + } else if (this._flagIsActive(this.FLAG_CLEAN_CONDITIONALLY)) { + this._removeFlag(this.FLAG_CLEAN_CONDITIONALLY); + this._attempts.push({articleContent: articleContent, textLength: textLength}); + } else { + this._attempts.push({articleContent: articleContent, textLength: textLength}); + // No luck after removing flags, just return the longest text we found during the different loops + this._attempts.sort(function (a, b) { + return b.textLength - a.textLength; + }); - // But first check if we actually have something - if (!this._attempts[0].textLength) { - return null; - } + // But first check if we actually have something + if (!this._attempts[0].textLength) { + return null; + } - articleContent = this._attempts[0].articleContent; - parseSuccessful = true; - } - } + articleContent = this._attempts[0].articleContent; + parseSuccessful = true; + } + } - if (parseSuccessful) { - // Find out text direction from ancestors of final top candidate. - var ancestors = [parentOfTopCandidate, topCandidate].concat(this._getNodeAncestors(parentOfTopCandidate)); - this._someNode(ancestors, function(ancestor) { - if (!ancestor.tagName) - return false; - var articleDir = ancestor.getAttribute("dir"); - if (articleDir) { - this._articleDir = articleDir; - return true; - } - return false; - }); - return articleContent; - } - } - }, + if (parseSuccessful) { + // Find out text direction from ancestors of final top candidate. + var ancestors = [parentOfTopCandidate, topCandidate].concat(this._getNodeAncestors(parentOfTopCandidate)); + this._someNode(ancestors, function(ancestor) { + if (!ancestor.tagName) + return false; + var articleDir = ancestor.getAttribute('dir'); + if (articleDir) { + this._articleDir = articleDir; + return true; + } + return false; + }); + return articleContent; + } + } + }, - /** + /** * Check whether the input string could be a byline. * This verifies that the input is a string, and that the length * is less than 100 chars. @@ -1204,112 +1204,112 @@ Readability.prototype = { * @param possibleByline {string} - a string to check whether its a byline. * @return Boolean - whether the input string is a byline. */ - _isValidByline: function(byline) { - if (typeof byline == "string" || byline instanceof String) { - byline = byline.trim(); - return (byline.length > 0) && (byline.length < 100); - } - return false; - }, + _isValidByline: function(byline) { + if (typeof byline == 'string' || byline instanceof String) { + byline = byline.trim(); + return (byline.length > 0) && (byline.length < 100); + } + return false; + }, - /** + /** * Attempts to get excerpt and byline metadata for the article. * * @return Object with optional "excerpt" and "byline" properties */ - _getArticleMetadata: function() { - var metadata = {}; - var values = {}; - var metaElements = this._doc.getElementsByTagName("meta"); + _getArticleMetadata: function() { + var metadata = {}; + var values = {}; + var metaElements = this._doc.getElementsByTagName('meta'); - // property is a space-separated list of values - var propertyPattern = /\s*(dc|dcterm|og|twitter)\s*:\s*(author|creator|description|title|site_name)\s*/gi; + // property is a space-separated list of values + var propertyPattern = /\s*(dc|dcterm|og|twitter)\s*:\s*(author|creator|description|title|site_name)\s*/gi; - // name is a single value - var namePattern = /^\s*(?:(dc|dcterm|og|twitter|weibo:(article|webpage))\s*[\.:]\s*)?(author|creator|description|title|site_name)\s*$/i; + // name is a single value + var namePattern = /^\s*(?:(dc|dcterm|og|twitter|weibo:(article|webpage))\s*[\.:]\s*)?(author|creator|description|title|site_name)\s*$/i; - // Find description tags. - this._forEachNode(metaElements, function(element) { - var elementName = element.getAttribute("name"); - var elementProperty = element.getAttribute("property"); - var content = element.getAttribute("content"); - if (!content) { - return; - } - var matches = null; - var name = null; + // Find description tags. + this._forEachNode(metaElements, function(element) { + var elementName = element.getAttribute('name'); + var elementProperty = element.getAttribute('property'); + var content = element.getAttribute('content'); + if (!content) { + return; + } + var matches = null; + var name = null; - if (elementProperty) { - matches = elementProperty.match(propertyPattern); - if (matches) { - for (var i = matches.length - 1; i >= 0; i--) { - // Convert to lowercase, and remove any whitespace - // so we can match below. - name = matches[i].toLowerCase().replace(/\s/g, ""); - // multiple authors - values[name] = content.trim(); - } - } - } - if (!matches && elementName && namePattern.test(elementName)) { - name = elementName; - if (content) { - // Convert to lowercase, remove any whitespace, and convert dots - // to colons so we can match below. - name = name.toLowerCase().replace(/\s/g, "").replace(/\./g, ":"); - values[name] = content.trim(); - } - } - }); + if (elementProperty) { + matches = elementProperty.match(propertyPattern); + if (matches) { + for (var i = matches.length - 1; i >= 0; i--) { + // Convert to lowercase, and remove any whitespace + // so we can match below. + name = matches[i].toLowerCase().replace(/\s/g, ''); + // multiple authors + values[name] = content.trim(); + } + } + } + if (!matches && elementName && namePattern.test(elementName)) { + name = elementName; + if (content) { + // Convert to lowercase, remove any whitespace, and convert dots + // to colons so we can match below. + name = name.toLowerCase().replace(/\s/g, '').replace(/\./g, ':'); + values[name] = content.trim(); + } + } + }); - // get title - metadata.title = values["dc:title"] || - values["dcterm:title"] || - values["og:title"] || - values["weibo:article:title"] || - values["weibo:webpage:title"] || - values["title"] || - values["twitter:title"]; + // get title + metadata.title = values['dc:title'] || + values['dcterm:title'] || + values['og:title'] || + values['weibo:article:title'] || + values['weibo:webpage:title'] || + values['title'] || + values['twitter:title']; - if (!metadata.title) { - metadata.title = this._getArticleTitle(); - } + if (!metadata.title) { + metadata.title = this._getArticleTitle(); + } - // get author - metadata.byline = values["dc:creator"] || - values["dcterm:creator"] || - values["author"]; + // get author + metadata.byline = values['dc:creator'] || + values['dcterm:creator'] || + values['author']; - // get description - metadata.excerpt = values["dc:description"] || - values["dcterm:description"] || - values["og:description"] || - values["weibo:article:description"] || - values["weibo:webpage:description"] || - values["description"] || - values["twitter:description"]; + // get description + metadata.excerpt = values['dc:description'] || + values['dcterm:description'] || + values['og:description'] || + values['weibo:article:description'] || + values['weibo:webpage:description'] || + values['description'] || + values['twitter:description']; - // get site name - metadata.siteName = values["og:site_name"]; + // get site name + metadata.siteName = values['og:site_name']; - return metadata; - }, + return metadata; + }, - /** + /** * Removes script tags from the document. * * @param Element **/ - _removeScripts: function(doc) { - this._removeNodes(doc.getElementsByTagName("script"), function(scriptNode) { - scriptNode.nodeValue = ""; - scriptNode.removeAttribute("src"); - return true; - }); - this._removeNodes(doc.getElementsByTagName("noscript")); - }, + _removeScripts: function(doc) { + this._removeNodes(doc.getElementsByTagName('script'), function(scriptNode) { + scriptNode.nodeValue = ''; + scriptNode.removeAttribute('src'); + return true; + }); + this._removeNodes(doc.getElementsByTagName('noscript')); + }, - /** + /** * Check if this node has only whitespace and a single element with given tag * Returns false if the DIV node contains non-empty text nodes * or if it contains no element with given tag or more than 1 element. @@ -1317,54 +1317,54 @@ Readability.prototype = { * @param Element * @param string tag of child element **/ - _hasSingleTagInsideElement: function(element, tag) { - // There should be exactly 1 element child with given tag - if (element.children.length != 1 || element.children[0].tagName !== tag) { - return false; - } + _hasSingleTagInsideElement: function(element, tag) { + // There should be exactly 1 element child with given tag + if (element.children.length != 1 || element.children[0].tagName !== tag) { + return false; + } - // And there should be no text nodes with real content - return !this._someNode(element.childNodes, function(node) { - return node.nodeType === this.TEXT_NODE && + // And there should be no text nodes with real content + return !this._someNode(element.childNodes, function(node) { + return node.nodeType === this.TEXT_NODE && this.REGEXPS.hasContent.test(node.textContent); - }); - }, + }); + }, - _isElementWithoutContent: function(node) { - return node.nodeType === this.ELEMENT_NODE && + _isElementWithoutContent: function(node) { + return node.nodeType === this.ELEMENT_NODE && node.textContent.trim().length == 0 && (node.children.length == 0 || - node.children.length == node.getElementsByTagName("br").length + node.getElementsByTagName("hr").length); - }, + node.children.length == node.getElementsByTagName('br').length + node.getElementsByTagName('hr').length); + }, - /** + /** * Determine whether element has any children block level elements. * * @param Element */ - _hasChildBlockElement: function (element) { - return this._someNode(element.childNodes, function(node) { - return this.DIV_TO_P_ELEMS.indexOf(node.tagName) !== -1 || + _hasChildBlockElement: function (element) { + return this._someNode(element.childNodes, function(node) { + return this.DIV_TO_P_ELEMS.indexOf(node.tagName) !== -1 || this._hasChildBlockElement(node); - }); - }, + }); + }, - /*** + /*** * Determine if a node qualifies as phrasing content. * https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Content_categories#Phrasing_content **/ - _isPhrasingContent: function(node) { - return node.nodeType === this.TEXT_NODE || this.PHRASING_ELEMS.indexOf(node.tagName) !== -1 || - ((node.tagName === "A" || node.tagName === "DEL" || node.tagName === "INS") && + _isPhrasingContent: function(node) { + return node.nodeType === this.TEXT_NODE || this.PHRASING_ELEMS.indexOf(node.tagName) !== -1 || + ((node.tagName === 'A' || node.tagName === 'DEL' || node.tagName === 'INS') && this._everyNode(node.childNodes, this._isPhrasingContent)); - }, + }, - _isWhitespace: function(node) { - return (node.nodeType === this.TEXT_NODE && node.textContent.trim().length === 0) || - (node.nodeType === this.ELEMENT_NODE && node.tagName === "BR"); - }, + _isWhitespace: function(node) { + return (node.nodeType === this.TEXT_NODE && node.textContent.trim().length === 0) || + (node.nodeType === this.ELEMENT_NODE && node.tagName === 'BR'); + }, - /** + /** * Get the inner text of a node - cross browser compatibly. * This also strips out any excess whitespace to be found. * @@ -1372,113 +1372,113 @@ Readability.prototype = { * @param Boolean normalizeSpaces (default: true) * @return string **/ - _getInnerText: function(e, normalizeSpaces) { - normalizeSpaces = (typeof normalizeSpaces === "undefined") ? true : normalizeSpaces; - var textContent = e.textContent.trim(); + _getInnerText: function(e, normalizeSpaces) { + normalizeSpaces = (typeof normalizeSpaces === 'undefined') ? true : normalizeSpaces; + var textContent = e.textContent.trim(); - if (normalizeSpaces) { - return textContent.replace(this.REGEXPS.normalize, " "); - } - return textContent; - }, + if (normalizeSpaces) { + return textContent.replace(this.REGEXPS.normalize, ' '); + } + return textContent; + }, - /** + /** * Get the number of times a string s appears in the node e. * * @param Element * @param string - what to split on. Default is "," * @return number (integer) **/ - _getCharCount: function(e, s) { - s = s || ","; - return this._getInnerText(e).split(s).length - 1; - }, + _getCharCount: function(e, s) { + s = s || ','; + return this._getInnerText(e).split(s).length - 1; + }, - /** + /** * Remove the style attribute on every e and under. * TODO: Test if getElementsByTagName(*) is faster. * * @param Element * @return void **/ - _cleanStyles: function(e) { - if (!e || e.tagName.toLowerCase() === "svg") - return; + _cleanStyles: function(e) { + if (!e || e.tagName.toLowerCase() === 'svg') + return; - // Remove `style` and deprecated presentational attributes - for (var i = 0; i < this.PRESENTATIONAL_ATTRIBUTES.length; i++) { - e.removeAttribute(this.PRESENTATIONAL_ATTRIBUTES[i]); - } + // Remove `style` and deprecated presentational attributes + for (var i = 0; i < this.PRESENTATIONAL_ATTRIBUTES.length; i++) { + e.removeAttribute(this.PRESENTATIONAL_ATTRIBUTES[i]); + } - if (this.DEPRECATED_SIZE_ATTRIBUTE_ELEMS.indexOf(e.tagName) !== -1) { - e.removeAttribute("width"); - e.removeAttribute("height"); - } + if (this.DEPRECATED_SIZE_ATTRIBUTE_ELEMS.indexOf(e.tagName) !== -1) { + e.removeAttribute('width'); + e.removeAttribute('height'); + } - var cur = e.firstElementChild; - while (cur !== null) { - this._cleanStyles(cur); - cur = cur.nextElementSibling; - } - }, + var cur = e.firstElementChild; + while (cur !== null) { + this._cleanStyles(cur); + cur = cur.nextElementSibling; + } + }, - /** + /** * Get the density of links as a percentage of the content * This is the amount of text that is inside a link divided by the total text in the node. * * @param Element * @return number (float) **/ - _getLinkDensity: function(element) { - var textLength = this._getInnerText(element).length; - if (textLength === 0) - return 0; + _getLinkDensity: function(element) { + var textLength = this._getInnerText(element).length; + if (textLength === 0) + return 0; - var linkLength = 0; + var linkLength = 0; - // XXX implement _reduceNodeList? - this._forEachNode(element.getElementsByTagName("a"), function(linkNode) { - linkLength += this._getInnerText(linkNode).length; - }); + // XXX implement _reduceNodeList? + this._forEachNode(element.getElementsByTagName('a'), function(linkNode) { + linkLength += this._getInnerText(linkNode).length; + }); - return linkLength / textLength; - }, + return linkLength / textLength; + }, - /** + /** * Get an elements class/id weight. Uses regular expressions to tell if this * element looks good or bad. * * @param Element * @return number (Integer) **/ - _getClassWeight: function(e) { - if (!this._flagIsActive(this.FLAG_WEIGHT_CLASSES)) - return 0; + _getClassWeight: function(e) { + if (!this._flagIsActive(this.FLAG_WEIGHT_CLASSES)) + return 0; - var weight = 0; + var weight = 0; - // Look for a special classname - if (typeof(e.className) === "string" && e.className !== "") { - if (this.REGEXPS.negative.test(e.className)) - weight -= 25; + // Look for a special classname + if (typeof(e.className) === 'string' && e.className !== '') { + if (this.REGEXPS.negative.test(e.className)) + weight -= 25; - if (this.REGEXPS.positive.test(e.className)) - weight += 25; - } + if (this.REGEXPS.positive.test(e.className)) + weight += 25; + } - // Look for a special ID - if (typeof(e.id) === "string" && e.id !== "") { - if (this.REGEXPS.negative.test(e.id)) - weight -= 25; + // Look for a special ID + if (typeof(e.id) === 'string' && e.id !== '') { + if (this.REGEXPS.negative.test(e.id)) + weight -= 25; - if (this.REGEXPS.positive.test(e.id)) - weight += 25; - } + if (this.REGEXPS.positive.test(e.id)) + weight += 25; + } - return weight; - }, + return weight; + }, - /** + /** * Clean a node of all elements of type "tag". * (Unless it's a youtube/vimeo video. People love movies.) * @@ -1486,30 +1486,30 @@ Readability.prototype = { * @param string tag to clean * @return void **/ - _clean: function(e, tag) { - var isEmbed = ["object", "embed", "iframe"].indexOf(tag) !== -1; + _clean: function(e, tag) { + var isEmbed = ['object', 'embed', 'iframe'].indexOf(tag) !== -1; - this._removeNodes(e.getElementsByTagName(tag), function(element) { - // Allow youtube and vimeo videos through as people usually want to see those. - if (isEmbed) { - // First, check the elements attributes to see if any of them contain youtube or vimeo - for (var i = 0; i < element.attributes.length; i++) { - if (this.REGEXPS.videos.test(element.attributes[i].value)) { - return false; - } - } + this._removeNodes(e.getElementsByTagName(tag), function(element) { + // Allow youtube and vimeo videos through as people usually want to see those. + if (isEmbed) { + // First, check the elements attributes to see if any of them contain youtube or vimeo + for (var i = 0; i < element.attributes.length; i++) { + if (this.REGEXPS.videos.test(element.attributes[i].value)) { + return false; + } + } - // For embed with