Skip to content

Commit

Permalink
Properly prevents solving tags inside another tag
Browse files Browse the repository at this point in the history
Fixes #29
Properlly fixes #14
Fixes #28
  • Loading branch information
surdu committed Jul 5, 2018
1 parent 3719bc4 commit 2d14b10
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 56 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},

"globals": {
"android": false,
"atom": false,
"com": false
},

Expand Down
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ language: objective-c

install:
- nvm install node
- npm i -g eslint

script:
- eslint **/*.js
- npm test
- 'curl -s https://raw.githubusercontent.com/atom/ci/master/build-package.sh | sh'
110 changes: 59 additions & 51 deletions lib/selector-solver.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,62 +90,70 @@ class SelectorSolver {
handleCommand(event) {
const editor = event.currentTarget.getModel();

if (this.isHTML(editor.getTitle(), editor.getRootScopeDescriptor())) {
const cursorPosition = editor.getCursorBufferPosition();
const textOnCurrentLine = editor.lineTextForBufferRow(cursorPosition.row);
const textBeforeCursor = textOnCurrentLine.substring(0, cursorPosition.column);
const expandBlockTags = atom.config.get('selector-to-tag.expandBlockTags');

// make sure the text before the cursor is a valid selector
// only preceded by spaces
const match = /^\s*([\w.#-_:]*)$/.exec(textBeforeCursor);

if (!match || match.length < 1) {
return;
}
if (!this.isHTML(editor.getTitle(), editor.getRootScopeDescriptor())) {
event.abortKeyBinding();
return;
}

const cursorPosition = editor.getCursorBufferPosition();
const textBeforeCursor = editor.getTextInBufferRange([[0, 0], cursorPosition]);

const lastClosingBracketIndex = textBeforeCursor.lastIndexOf(">");
const lastOpeningBracketIndex = textBeforeCursor.lastIndexOf("<");
const isInsideTag = lastOpeningBracketIndex > lastClosingBracketIndex;

if (isInsideTag) {
event.abortKeyBinding();
return;
}

const match = /\s*([\w.#-_:]*)$/.exec(textBeforeCursor);

if (!match || match.length < 1) {
event.abortKeyBinding();
return;
}

const selector = match[1];
const solvedElement = this.solveSelector(selector,
atom.config.get('selector-to-tag.solveTagsOnly'),
atom.config.get('selector-to-tag.closeSelfclosingTags'),
expandBlockTags,
atom.config.get('selector-to-tag.blockTags'));

if (solvedElement) {
const selectorRange = [[cursorPosition.row, cursorPosition.column - selector.length],
cursorPosition];

if (expandBlockTags && solvedElement.isBlock) {
editor.transact(function () {
editor.setTextInBufferRange(selectorRange, solvedElement.string);
editor.selectUp();
editor.getLastSelection().autoIndentSelectedRows();
editor.clearSelections();
editor.moveToEndOfLine();
});
}
else {
editor.setTextInBufferRange(selectorRange, solvedElement.string);

const newCursorPosition = editor.getCursorScreenPosition();

if (solvedElement.isSelfClosing) {
newCursorPosition.column -= atom.config.get('selector-to-tag.closeSelfclosingTags') ? 2 : 1;
editor.setCursorScreenPosition(newCursorPosition);
}
else {
newCursorPosition.column -= solvedElement.element.tagName.length + 3;
editor.setCursorScreenPosition(newCursorPosition);
}
}
const expandBlockTags = atom.config.get('selector-to-tag.expandBlockTags');

const selector = match[1];
const solvedElement = this.solveSelector(selector,
atom.config.get('selector-to-tag.solveTagsOnly'),
atom.config.get('selector-to-tag.closeSelfclosingTags'),
expandBlockTags,
atom.config.get('selector-to-tag.blockTags'));

if (!solvedElement) {
event.abortKeyBinding();
return;
}

const selectorRange = [[cursorPosition.row, cursorPosition.column - selector.length],
cursorPosition];

if (expandBlockTags && solvedElement.isBlock) {
editor.transact(function () {
editor.setTextInBufferRange(selectorRange, solvedElement.string);
editor.selectUp();
editor.getLastSelection().autoIndentSelectedRows();
editor.clearSelections();
editor.moveToEndOfLine();
});
}
else {
editor.setTextInBufferRange(selectorRange, solvedElement.string);

const newCursorPosition = editor.getCursorScreenPosition();

if (solvedElement.isSelfClosing) {
newCursorPosition.column -= atom.config.get('selector-to-tag.closeSelfclosingTags') ? 2 : 1;
editor.setCursorScreenPosition(newCursorPosition);
}
else {
event.abortKeyBinding();
newCursorPosition.column -= solvedElement.element.tagName.length + 3;
editor.setCursorScreenPosition(newCursorPosition);
}
}
else {
event.abortKeyBinding();
}
}
}

Expand Down
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@
"engines": {
"atom": ">0.50.0"
},
"scripts": {
"test": "npx eslint **/*.js"
},
"keywords": [
"html",
"css",
"web development",
"solve tags"
]
],
"dependencies": {
"eslint": "^5.0.1"
}
}
2 changes: 1 addition & 1 deletion spec/selector-solver-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ describe("selector-to-tab", function () {
testSelector("View", '<View></View>');
});

it("should not solve unless a full valid selector precedes", function() {
it("should not solve tags if inside another tag", function() {
editor.setText('<input type="button" name="name" value="">');
editor.setCursorScreenPosition([0, 19]);
atom.commands.dispatch(editorView, 'selector-to-tag:solve-selector');
Expand Down

0 comments on commit 2d14b10

Please sign in to comment.