Commit 2f5b0346 by 高源

isExist方法的验证逻辑完善

parent e4986790
......@@ -51,7 +51,7 @@
<script>
import VXETable, { t } from 'vxe-table'
// import { getTableData } from '../sdk'
import { cell, checkTableFromXpaths } from '../sdk-table'
import { cell, checkTableFromXpaths, table } from '../sdk-table'
export default {
name: 'collectionTable',
components: {},
......@@ -80,21 +80,29 @@ export default {
methods: {
getTable () {
let _this = this
console.log(_this.sdkXPath)
_this.checkTableFrom = checkTableFromXpaths(_this.sdkXPath)
console.log(_this.checkTableFrom)
_this.tableData = _this.checkTableFrom.tableData
console.log(_this.tableData)
this.tableColumn = []
for (let i = 0; i < _this.tableData.length; i++) {
let objs = {}
objs.field = ('content' + i) + '.content'
objs.title = '请填写标题'
objs.width = 145
_this.tableColumn.push(objs)
let _table = new table()
for (let i = 0; i < _this.Xpaths.length; i++) {
_table.newCell(_this.Xpaths[i])
}
_this.isSelectTable = false
_table.verify()
console.log(_table)
// console.log(_this.table)
//----------------------
// console.log(_this.sdkXPath)
// _this.checkTableFrom = checkTableFromXpaths(_this.sdkXPath)
// console.log(_this.checkTableFrom)
// _this.tableData = _this.checkTableFrom.tableData
// console.log(_this.tableData)
// this.tableColumn = []
// for (let i = 0; i < _this.tableData.length; i++) {
// let objs = {}
// objs.field = ('content' + i) + '.content'
// objs.title = '请填写标题'
// objs.width = 145
// _this.tableColumn.push(objs)
// }
// _this.isSelectTable = false
return
//表格展示配置
......
/**
* 定义表格对象
*/
function table() {
function table () {
/**表格名称,当该表格获取完数据后,应该存储的对象名 */
this.name = "";
/**表格所在的XPath,由验证表格功能具体生成,如果为null,说明验证不成功 */
......@@ -16,8 +16,8 @@ function table() {
*/
this.newCell = function (xpath) {
_isVerified = false;//加入了
let cell = new cell(xpath);
this.cells.push(cell);
let celld = new cell(xpath);
this.cells.push(celld);
}
/**
* 根据传入的cell进行验证,如果验证成功,xpath自动变化
......@@ -39,13 +39,14 @@ function table() {
} else { break; }//如果发现不同的,直接就可以退出了
}
}
console.log(tableXpath)
if (tableXpath.length < 1) return _isVerified;//如果没有共同父节点,返回false
while (tableXpath.length > 0) {
this.cells.forEach(cell => cell.updateRelativePath(tableXpath));//先对所有的Cell更新下相对路径
if (this.cells.every(cell => cell.isExist())) break;//如果每个cell都存在,成功
if (this.cells.every(cell => cell.isExist(cell))) break;//如果每个cell都存在,成功
tableXpath.pop();//向上一级找
if (tableXpath.length == 0) return null
}
if (tableXpath.length < 1) return _isVerified;//如果没有共同父节点,返回false
this.cells.forEach(cell => cell.confirm());//对所有单元格进行确认
......@@ -67,7 +68,7 @@ function table() {
* @param {string} type 元素取值类型,文本,链接,元素路径(点击动作用)
* @param {string} title 标题信息
*/
function cell (xpath, name="", type="", title="") {
function cell (xpath, name = "", type = "", title = "") {
/**当前单元格的绝对路径,存入Action时有用 */
this.xpath = xpath;
/**当前单元格存储数据时的属性名 */
......@@ -87,34 +88,57 @@ function cell (xpath, name="", type="", title="") {
/**当前XPath的数组结构 */
let _xpathArray = this.xpath.split('/');
/**当前表路径的数组结构 */
let _tableXpathArray = null;
this._tableXpathArray = null;
/**当前单元格的相对路径的数组结构 */
let _relativeXpathArray = null;
this._relativeXpathArray = null;
/**
* 根据父路径更新当前的相对路径
* @param {string[]} parentXpath 父路径
*/
this.updateRelativePath = function (parentXpath) {
_tableXpathArray = parentXpath;//其实这边严谨的来说要验证一下XPath是否真的有相同的根
_relativeXpathArray = _xpathArray.slice(_tableXpathArray.length);
this._tableXpathArray = parentXpath;//其实这边严谨的来说要验证一下XPath是否真的有相同的根
this._relativeXpathArray = _xpathArray.slice(this._tableXpathArray.length);
// this.relativeXpath = _relativeXpathArray
// this.tableXpath = _tableXpathArray
// console.log(this._relativeXpathArray)
}
/**
* 单元格是否存在
* @returns {boolean}
*/
this.isExist = function () {
this.isExist = function (cell) {
console.log(cell)
let existCount = 0;
let _tableXpathArray = [...cell._tableXpathArray] //深拷贝,值修改不影响原数组
let _relativeXpathArray = [...cell._relativeXpathArray]//深拷贝,值修改不影响原数组
// 移除最后一个元素的下标
if (_tableXpathArray.length > 0) {
let lastElement = _tableXpathArray[_tableXpathArray.length - 1];
let lastElementWithoutIndex = lastElement.split('[')[0];
_tableXpathArray[_tableXpathArray.length - 1] = lastElementWithoutIndex;
}
_tableXpathArray = _tableXpathArray.join('/')
_relativeXpathArray = _relativeXpathArray.join('/')
//table[0]+relativeXpath,可以不存在,因为可能是表头,1,2
for (let i = 0; i < 3; i++) {
let isGetElement = elementExists(_tableXpathArray + '[' + (i + 1) + ']' + '/' + _relativeXpathArray)
console.log(isGetElement)
if (isGetElement) {
existCount = i
} else {
return false
}
}
return existCount>1;
return existCount > 1;
}
/**当所有单元格验证成功后,由表发起确认动作 */
this.confirm = function () {
this.tableXpath = _tableXpathArray.join("/");
this.relativePath = _relativeXpathArray.join("/");
this.tableXpath = this._tableXpathArray.join("/");
this.relativePath = this._relativeXpathArray.join("/");
}
/**
* 根据索引获取值
......@@ -272,32 +296,9 @@ function incrementLastIndex (xpath) {
* 检查XPath是否指向一个存在的元素,如果存在就返回,当父节点的最后一个下标换成第二行的下标时,组合同样的相对路径,向上查找相同的父节点并更新cell对应的父节点,保证当前选中是表结构
*/
function elementExists (xpath, cells) {
let documentResult = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
if (!!documentResult.singleNodeValue) {
// 如果找到元素,返回 true
return true;
} else {
// 没找到元素,开始逐级向上检查
let parts = xpath.split('/');
// 移除最后一个节点,逐级向上检查
while (parts.length > 1) {
parts.pop();
// reducedXPath 这个路径以绝对xpath向上查找
let reducedXPath = parts.join('/');
documentResult = document.evaluate(reducedXPath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
if (!!documentResult.singleNodeValue) {
// 如果在上一级找到了元素,返回 true
let xpathArray = reducedXPath.split('/');
// 移除数组中的空字符串元素,这通常出现在字符串的开头
cells.forEach(o => o.updateRelativePath(xpathArray));//更新每个cell的相对路径和父节点
return true;
}
}
// 如果遍历完所有节点还是没找到,返回 false
return false;
}
function elementExists (xpath) {
let result = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
return !!result.singleNodeValue;
}
/**
* // 公共部分与拆解部分组合查询 找出当前节点下所有符合条件的表数据
......@@ -438,4 +439,4 @@ function getTableData (resultElement) {
return null
}
}
export { cell, checkTableFromXpaths }
\ No newline at end of file
export { cell, checkTableFromXpaths, table }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment