Skip to content

Commit

Permalink
Merge pull request #91 from AtomLinter/steelbrain/remove-create-element
Browse files Browse the repository at this point in the history
Remove createElement API
  • Loading branch information
steelbrain committed Jan 4, 2016
2 parents fd38371 + dfaf076 commit aeb1e4e
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 84 deletions.
8 changes: 0 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,9 @@ class Helpers{
static findCachedAsync(directory: Strng, names: String | Array<string>): Promise<?String>
static tempFile<T>(fileName:String, fileContents:String, Callback:Function<T>):Promise<T>
static tempFiles<T>(filesNames:Array<{name: String, contents: String}>, callback:Function<T>):Promise<T>
static createElement(tagName: string): HTMLElement
}
```

#### Explanation for createElement

Linter accepts `HTMLElement`s in the `html` message property. To show the same message on more
than one DOM Elements, it clones the element. It's a limitation of HTMLElements that they
lose all the events on clone. If you create your element using `Helpers.createElement` however
It'll make sure the children inherit the events from the parent.

#### License

This project is licensed under the terms of MIT License, see the LICENSE file for more info
32 changes: 3 additions & 29 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ exports.FindCache = undefined;
exports.exec = exec;
exports.execNode = execNode;
exports.rangeFromLineNumber = rangeFromLineNumber;
exports.createElement = createElement;
exports.findAsync = findAsync;
exports.findCachedAsync = findCachedAsync;
exports.find = find;
Expand Down Expand Up @@ -36,7 +35,6 @@ var _consistentPath = require('consistent-path');
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

let XRegExp = null;
const EventsCache = new WeakMap();
const FindCache = exports.FindCache = new Map();

// TODO: Remove this when electron upgrades node
Expand Down Expand Up @@ -178,32 +176,6 @@ function rangeFromLineNumber(textEditor, lineNumber, colStart) {
return [[lineNumber, colStart], [lineNumber, lineLength]];
}

function createElement(name) {
if (typeof name !== 'string') {
throw new Error('Invalid or no `Element name` provided');
}

const element = document.createElement(name);

element.addEventListener = function (name, callback) {
EventsCache.get(element).push({ name: name, callback: callback });
Element.prototype.addEventListener.call(this, name, callback);
};
element.cloneNode = function (deep) {
const newElement = Element.prototype.cloneNode.call(this, deep);
EventsCache.get(element).forEach(function (_ref2) {
let name = _ref2.name;
let callback = _ref2.callback;

newElement.addEventListener(name, callback);
});
return newElement;
};

EventsCache.set(element, []);
return element;
}

function findAsync(directory, name) {
validate_find(directory, name);
const names = name instanceof Array ? name : [name];
Expand Down Expand Up @@ -242,12 +214,13 @@ function findAsync(directory, name) {
}

function findCachedAsync(directory, name) {
validate_find(directory, name);
const names = name instanceof Array ? name : [name];
const cacheKey = directory + ':' + names.join(',');

if (FindCache.has(cacheKey)) {
const cachedFilePath = FindCache.get(cacheKey);
return new Promise(function (resolve, reject) {
return new Promise(function (resolve) {
FS.access(cachedFilePath, FS.R_OK, function (error) {
if (error) {
FindCache.delete(cacheKey);
Expand Down Expand Up @@ -288,6 +261,7 @@ function find(directory, name) {
}

function findCached(directory, name) {
validate_find(directory, name);
const names = name instanceof Array ? name : [name];
const cacheKey = directory + ':' + names.join(',');

Expand Down
22 changes: 0 additions & 22 deletions spec/helper-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -271,25 +271,3 @@ describe 'linter helpers', ->
return filePaths
).then (result) ->
expect(result.length).toBe(2)

describe '::createElement', ->
it 'works', ->
clicked = false
clickListener = -> clicked = true

el = helpers.createElement('div')
el.innerHTML = 'Some HTML'
expect(el.innerHTML).toBe('Some HTML')
el.appendChild(document.createElement('div'))
expect(el.children.length).toBe(1)

el.addEventListener('click', clickListener)

expect(clicked).toBe(false)
el.dispatchEvent(new MouseEvent('click'))
expect(clicked).toBe(true)

clicked = false
clonedEl = el.cloneNode(true)
clonedEl.dispatchEvent(new MouseEvent('click'))
expect(clicked).toBe(true)
28 changes: 3 additions & 25 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import * as TMP from 'tmp'
import {getPath} from 'consistent-path'

let XRegExp = null
const EventsCache = new WeakMap()
export const FindCache = new Map()

// TODO: Remove this when electron upgrades node
Expand Down Expand Up @@ -145,29 +144,6 @@ export function rangeFromLineNumber(textEditor, lineNumber, colStart) {
]
}

export function createElement(name) {
if (typeof name !== 'string') {
throw new Error('Invalid or no `Element name` provided')
}

const element = document.createElement(name)

element.addEventListener = function(name, callback) {
EventsCache.get(element).push({name, callback})
Element.prototype.addEventListener.call(this, name, callback)
}
element.cloneNode = function(deep) {
const newElement = Element.prototype.cloneNode.call(this, deep)
EventsCache.get(element).forEach(function({name, callback}) {
newElement.addEventListener(name, callback)
})
return newElement
}

EventsCache.set(element, [])
return element
}

export function findAsync(directory, name) {
validate_find(directory, name)
const names = name instanceof Array ? name : [name]
Expand Down Expand Up @@ -206,12 +182,13 @@ export function findAsync(directory, name) {
}

export function findCachedAsync(directory, name) {
validate_find(directory, name)
const names = name instanceof Array ? name : [name]
const cacheKey = directory + ':' + names.join(',')

if (FindCache.has(cacheKey)) {
const cachedFilePath = FindCache.get(cacheKey)
return new Promise(function(resolve, reject) {
return new Promise(function(resolve) {
FS.access(cachedFilePath, FS.R_OK, function(error) {
if (error) {
FindCache.delete(cacheKey)
Expand Down Expand Up @@ -252,6 +229,7 @@ export function find(directory, name) {
}

export function findCached(directory, name) {
validate_find(directory, name)
const names = name instanceof Array ? name : [name]
const cacheKey = directory + ':' + names.join(',')

Expand Down

0 comments on commit aeb1e4e

Please sign in to comment.